Compare commits

...

2 commits

Author SHA1 Message Date
5fb0303145
Merge pull request #12 from JonasunderscoreJones/analytics
Added analytics tracking
2024-08-18 04:34:31 +02:00
6b1d0c46da added analytics tracking 2024-08-18 04:30:26 +02:00
2 changed files with 51 additions and 0 deletions

View file

@ -1,4 +1,21 @@
<script>
import { onMount } from 'svelte';
import { afterNavigate } from '$app/navigation';
import { recordRequest } from './analytics';
// Function to handle navigation events
function handleNavigation() {
recordRequest();
}
// Call the analytics function on initial page load and after each navigation
onMount(() => {
handleNavigation();
afterNavigate(() => {
handleNavigation();
});
});
let showMenu = false;
function toggleMenu() {

View file

@ -0,0 +1,34 @@
// Function to record the request with analytics
export async function recordRequest() {
const analyticsData = {
timestamp: Date.now(),
domain: window.location.hostname,
method: 'GET', // Assuming the request method is GET; adjust as necessary
path: window.location.pathname,
};
console.log('Recording request:', analyticsData);
const ANALYTICS_URL = 'https://analytics.jonasjones.dev/requests/record/ipunknown';
const ANALYTICS_API_KEY = import.meta.env.VITE_ANALYTICS_API_KEY;
try {
const response = await fetch(ANALYTICS_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': ANALYTICS_API_KEY,
},
body: JSON.stringify(analyticsData),
});
if (response.ok) {
console.log('Request recorded successfully');
} else {
console.error('Failed to record request:', response.status, await response.text());
}
} catch (error) {
console.error('Error recording request:', error);
}
}