mirror of
https://github.com/JonasunderscoreJones/wiki.jonasjones.dev.git
synced 2025-10-22 13:59:19 +02:00
added analytics tracking
This commit is contained in:
parent
7ce5548143
commit
9a61598b1e
3 changed files with 339 additions and 338 deletions
627
package-lock.json
generated
627
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -1,5 +1,21 @@
|
|||
<script>
|
||||
import NavbarList from "./NavbarList.svelte";
|
||||
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 searchterm = "";
|
||||
|
||||
|
|
34
src/lib/components/analytics.js
Normal file
34
src/lib/components/analytics.js
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue