Geert Jan Sloos
CDCloudflare Developers
•Created by Geert Jan Sloos on 7/6/2023 in #pages-help
Show visitor count on my cloudflare page
Thanks it works!!!! you have a donation page somewhere 🙂 ?
12 replies
CDCloudflare Developers
•Created by Geert Jan Sloos on 7/6/2023 in #pages-help
Show visitor count on my cloudflare page
My code in worker.js:
addEventListener("fetch", (event) => {
event.respondWith(
handleRequest(event.request).catch(
(err) => new Response(err.stack, { status: 500 })
)
);
});
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const today = new Date().toISOString().split('T')[0];
const query =
query {
viewer {
zones(filter: { zoneTag: "XXX" }) {
httpRequests1dGroups(filter: { date: "${today}" }, limit: 1) {
uniq {
uniques
}
}
}
}
}
;
const data = JSON.stringify({ query });
const response = await fetch('https://api.cloudflare.com/client/v4/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer XXXXXXX'
},
body: data
});
if (!response.ok) {
throw new Error('Error occurred while fetching data');
}
const result = await response.json();
const uniques = result.data.viewer.zones[0].httpRequests1dGroups[0].uniq.uniques || 0;
return new Response(
${uniques}, {
headers: { 'Content-Type': 'text/plain' },
});
}
12 replies
CDCloudflare Developers
•Created by Geert Jan Sloos on 7/6/2023 in #pages-help
Show visitor count on my cloudflare page
12 replies
CDCloudflare Developers
•Created by Geert Jan Sloos on 7/6/2023 in #pages-help
Show visitor count on my cloudflare page
12 replies
CDCloudflare Developers
•Created by Geert Jan Sloos on 7/6/2023 in #pages-help
Show visitor count on my cloudflare page
But seems not to work, I also found this: https://developers.cloudflare.com/workers/examples/return-json/
Maybe I can use https://live.lofi.radio/worker.js ?
12 replies
CDCloudflare Developers
•Created by Geert Jan Sloos on 7/6/2023 in #pages-help
Show visitor count on my cloudflare page
Thanks for your reply I added this to my cloudflare worker:
// Reference: https://developers.cloudflare.com/workers/examples/cors-header-proxy
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS",
"Access-Control-Max-Age": "86400",
}
12 replies