Hi a lot of users and browsers block google analytics script and it only going to get worse, depends on the country and site it can be anywhere around 5 to 30 percent of users
I created a simple worker to proxy all the google analytics requests to prevent them being blocked
I tested it on 2 sites and couple of the big ad blocking extensions it works good so far
simple Cloudflare worker to protect google analytics from being blocked by ad blockers(it can be anywhere around 5 to 30 percent of the users)
It works by proxy the google analytics script file and endpoint url.
right now it support only the analytics.js tracking and the https://www.google-analytics.com/*collect endpoint
there is other urls which not proxied right now(which will get blocked), in my case I dont need them, like:
change UrlPrefix and EndPointScramble Parameters to random string, a common ads\analytics related words will get blocked
make your worker route to url matches http://example.com/UrlPrefix*
change your site google analytics script from https://www.google-analytics.com/analytics.js to /UrlPrefix.js
** (change UrlPrefix to the value you used in the worker parameter)
Warnings:
pay attention to the gdpr law
if you have millions of requests I am not 100% sure its good idea to use it as google can rate limit the Cloudflare worker ips, as the ips are shared between all Cloudflare accounts so its good idea to reconsider it\ask google about it
make sure to rescramble the urls from time to time
the blocking trend is getting really big now, for example Firefox and some adblocker block trackers by default now and the ones that dont make it 1 button click to block them
This looks interesting, thanks for making and sharing it!
I do have a question if you don’t mind.
I looked at the code but don’t understand it good enough to see to see if you still get visitor data like country, time on page, pages per session, and so on. Or does it solely capture pageviews?
Have you investigated just sending the data directly to Google using the measurement API?
The client never needs to execute the js, scrambling is not needed, and it cannot be blocked. I have not looked in detail, but I have being thinking for a while that Workers are a better place to fire calls to any analytics provider than relying on the client to do this.
yes, the worker just proxy everything as before with the visitor ip
I tested it for pageviews and events, it can(and probably should) be used as fallback technique to dynamically load it in case the user blocked tracking
if you use google analytics just for pageviews so yes you dont really need the js part, but google analytics offer so much more that most of it cannot work without js(like events reporting)
I dont really afraid of blockers blocking this because:
unless your site is in top 1000 or something I dont think anyone will invest the time to create rules just for you
its so easy to change the url to bypass the block(I could in theory change the urls automatically from the worker)
after you call that use this simple if statement to switch to the proxy when needed.
Make sure to use <!-- or html will try to render your js and mess it up.
This has the value that it wont use your worker limit up for those you don’t need to.
I am sure there is a better way to hide it but I am not experienced enough with workers yet to check for blocker use in the worker that would not result in calling the worker at least twice for every view. Any suggestions for that would be helpful to me. For now this is a quick and dirty solution.