mirror of
https://github.com/JonasunderscoreJones/rss.jonasjones.dev.git
synced 2025-10-22 19:49:18 +02:00
added analytics tracking
This commit is contained in:
parent
4577983233
commit
109cfbaba5
1 changed files with 31 additions and 2 deletions
33
src/index.js
33
src/index.js
|
@ -1,5 +1,5 @@
|
||||||
addEventListener('fetch', event => {
|
addEventListener('fetch', event => {
|
||||||
event.respondWith(handleRequest(event.request, event.env));
|
event.respondWith(handleRequest(event));
|
||||||
});
|
});
|
||||||
|
|
||||||
const headersCORS = {
|
const headersCORS = {
|
||||||
|
@ -8,7 +8,36 @@ const headersCORS = {
|
||||||
'Access-Control-Allow-Headers': 'Content-Type, x-Custom-Auth-Key',
|
'Access-Control-Allow-Headers': 'Content-Type, x-Custom-Auth-Key',
|
||||||
};
|
};
|
||||||
|
|
||||||
async function handleRequest(request, env) {
|
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 url = new URL(request.url);
|
const url = new URL(request.url);
|
||||||
|
|
||||||
if (request.method === 'OPTIONS') {
|
if (request.method === 'OPTIONS') {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue