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 - https://github.com/boynet/cf-GoogleAnalytics-shield-worker

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

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 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

todo:

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

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)