mirror of
https://github.com/JonasunderscoreJones/api.jonasjones.dev.git
synced 2025-10-22 19:59:17 +02:00
added analytics tracking
This commit is contained in:
parent
9d8987e464
commit
34a4b77990
2 changed files with 32 additions and 2 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
||||||
/node_modules/
|
/node_modules/
|
||||||
|
.dev.vars
|
||||||
|
|
33
src/index.js
33
src/index.js
|
@ -1,8 +1,37 @@
|
||||||
addEventListener('fetch', event => {
|
addEventListener('fetch', event => {
|
||||||
event.respondWith(handleRequest(event.request));
|
event.respondWith(handleRequest(event));
|
||||||
});
|
});
|
||||||
|
|
||||||
async function handleRequest(request) {
|
async function recordRequest(request) {
|
||||||
|
|
||||||
|
const analyticsData = {
|
||||||
|
timestamp: Date.now(),
|
||||||
|
domain: new URL(request.url).hostname,
|
||||||
|
method: request.method,
|
||||||
|
path: new URL(request.url).pathname,
|
||||||
|
ipcountry: request.cf.country,
|
||||||
|
}
|
||||||
|
const ANALYTICS_URL = 'https://analytics.jonasjones.dev/requests/record';
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleRequest(event) {
|
||||||
|
event.waitUntil(recordRequest(event.request));
|
||||||
|
const request = event.request;
|
||||||
const { pathname, search } = new URL(request.url);
|
const { pathname, search } = new URL(request.url);
|
||||||
|
|
||||||
// List of built-in paths that should not trigger an error
|
// List of built-in paths that should not trigger an error
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue