vercel / analytics

Privacy-friendly, real-time traffic insights
https://vercel.com/analytics
Mozilla Public License 2.0
416 stars 24 forks source link

Use process.env.ASSET_PREFIX when sending tracking data #63 #139

Closed officert closed 4 months ago

officert commented 4 months ago

I'm using Vercel analytics with NextJS. However, our sites are behind a cloudflare proxy, where clients can point any path they want off their domain to our NextJS app. Ex:

https://foobar.com/shop -> proxy to our NextJS app

Our NextJS then rewrites the incoming urls to map them to the folder structure of our NextJS app.

When using analytics the request for the Javascript file always points to https://foobar.com/_vercel/analytics/script.js, which returns a 404 because we don't control that path on their domain.

I was able to get around this by using the HTML version, and our process.env.ASSET_PATH like this:

<Script src={join(process.env.ASSET_PREFIX, '_vercel/analytics/script.js')} />

so that the script is served up from our Vercel provided domain name.

This works, however, all the tracking events are still pointing to https://foobar.com/_vercel.

Is it possible to have both the script tag and the tracking event use the process.env.ASSET_PATH if one is set? Or at least have a way to configure where analytics send the tracking events?

tobiaslins commented 4 months ago

Hey! It should also be possible to set the params scriptSrc and endpoint in the latest versions https://github.com/vercel/analytics/blob/main/packages/web/src/types.ts#L24-L25