Custom Domains for google analytics - Only routes for zones active on your account

Dear All,

I am new to the workers and I am seeking for advice, support. In the last days I have been trying to deploy a google analitycs visitor tracking via workers. I am doing this because I am using a CDN implemented by Cloudflare enterprise and my personal Cloudflare account is pointing to that one. In this configuration my Cloudflare account cannot track traffic for example via Zaraz.

I have implemented via worker the below code:

addEventListener(‘fetch’, (event) => {
return event.respondWith(handleRequest(event));
});

const DOMAIN = ‘https://SUB-DOMAIN.DOMAIN.COM/routerpath’;
const COLLECT_PATH = ‘SUB-DOMAIN’;
const JS_URL = 'https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXX;

const G_DOMAIN = /https://[a-z"+\s]*.google-analytics.com/g;
const G_COLLECT_PATH = ‘g/collect’;
async function handleRequest(event) {
const url = event.request.url;
if (url.match(${DOMAIN}/a.js)) {
const requestJs = await (await fetch(JS_URL)).text();
const jsText = requestJs.replaceAll(G_DOMAIN, DOMAIN).replaceAll(G_COLLECT_PATH, COLLECT_PATH);
return new Response(jsText, {
status: 200,
statusText: ‘OK’,
headers: {
‘Content-Type’: ‘application/javascript’,
},
});
} else if (url.match(${DOMAIN}/${COLLECT_PATH})) {
const newReq = await readRequest(event.request);
event.waitUntil(fetch(newReq));
}
return new Response(null, {
status: 204,
statusText: ‘No Content’,
});
}
async function readRequest(request) {
const { url, headers } = request;
const body = await request.text();
const ga_url = url.replace(${DOMAIN}/${COLLECT_PATH}, https://www.google-analytics.com/${G_COLLECT_PATH});
const nq = {
method: ‘POST’,
headers: {
Host: ‘www.google-analytics.com’,
Origin: headers.get(‘origin’),
‘Cache-Control’: ‘max-age=0’,
‘User-Agent’: headers.get(‘user-agent’),
Accept: headers.get(‘accept’),
‘Accept-Language’: headers.get(‘accept-language’),
‘Content-Type’: headers.get(‘content-type’) || ‘text/plain’,
Referer: headers.get(‘referer’),
},
body: body,
};
return new Request(ga_url, nq);
}

I tried to setup a trigger by creating a custom domain. I was planning to use as custom trigger SUB-DOMAIN.DOMAIN.COM however, when I try to type in the domain field i get an error message saying Only routes for zones active on your Cloudflare account can be configured.

Could you please tell me why and how to solve it?

Please note that I am in the worker and pages tab at global level. To my account it is associate a website and related DNS settings for the relevant domain that i want to use in the worker.

Thanks in advance for the help.

F.

Hi there,

I see that you have opened ticket #3275545 to CF support and being attended.

This issue occurs due to a conflict in verifying the ownership of subdomain. The error message suggests that the subdomain belongs to a different account (as a custom hostname). Therefore, you were not able to add it as a route on your account due to it being active on a different account.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.