added analytics tracking

This commit is contained in:
J-onasJones 2024-08-18 05:29:33 +02:00
parent 441e3d5dec
commit af799015a7
3 changed files with 610 additions and 411 deletions

985
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,6 @@
<script>
import { onMount } from "svelte";
import { recordRequest } from "./analytics";
let releases = [];
let searchArtist = "";
@ -71,6 +72,7 @@
}
onMount(async () => {
recordRequest();
try {
const response = await fetch('https://cdn.jonasjones.dev/api/kcomebacks/rkpop_data.json');
//const response = await fetch('/rkpop_data.json')

34
src/routes/analytics.js Normal file
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);
}
}