Google analytics proxy worker

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

feel free to submit fixes or anything to the repo -

Google Analytics shield - Cloudflare worker

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*collect endpoint
there is other urls which not proxied right now(which will get blocked), in my case I dont need them, like:

How to install:

  • change UrlPrefix and EndPointScramble Parameters to random string, a common ads\analytics related words will get blocked
  • make your worker route to url matches*
  • change your site google analytics script from to /UrlPrefix.js

** (change UrlPrefix to the value you used in the worker parameter)


  • 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


  • log failed requests
  • proxy all the urls needed for analytics(?)
  • support the new gtag.js script
  • auto scrambled the endpoint from time to time

This is cool! Looking closer at logs in general I’m seeing a pretty large discrepancy in a lot of cases.

1 Like

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. :slight_smile:

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:

  1. unless your site is in top 1000 or something I dont think anyone will invest the time to create rules just for you
  2. its so easy to change the url to bypass the block(I could in theory change the urls automatically from the worker)
1 Like

for those that want to use it only with users that block you its pretty easy.

just make a js with one line in it. Call it analytics.js

`var someVeriable = true;`

then in your header call that file before you normal analytics.js call.

<script type="text/javascript" src="???/analytics.js"></script>

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.

	var otherVeriable = '';
	if( window.someVeriable === undefined ){
		otherVeriable = '/ UrlPrefix.js';
	document.write('<script type="text/javascript" async src="' + otherVeriable + '"></script>');

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.

1 Like

How would you configure this to work with GTM?