Reverse proxy Google Tag Manager with Nginx

Reverse proxy Google Tag Manager with Nginx

unblock the function

Just set up the Google Analytics 4 with Google Tag Manager, however, both of them are been blocked by AdBlockers or DNS filters. A self-hosted Ackee helps to record the web traffics, but duplicate the same function with more traffic loads is not worthing. Therefore, I’m trying to reverse proxy the GTM and GA4 with Nginx.

Just a sample configuration here (only works for GA4), comments below to make it better.

set $gtm 'www.googletagmanager.com';
set $ga  'analytics.google.com';

location /gtm.js {
    sub_filter $gtm $server_name;
    sub_filter_types *;
    sub_filter_once off;

    proxy_set_header Accept-Encoding '';
    proxy_pass https://$gtm/gtm.js$is_args$args;
    proxy_redirect off;

    # cache on server
    proxy_cache ZONE0;
    proxy_cache_key $host$uri$is_args$args;
    proxy_cache_valid 200 304 60m;

    # cache in client browser
    expires 1h;
}

location /gtag/js {
    sub_filter $gtm $server_name;
    sub_filter $ga  $server_name;
    sub_filter_types *;
    sub_filter_once off;

    proxy_set_header Accept-Encoding '';

    proxy_pass https://$gtm/gtag/js$is_args$args;
    proxy_redirect off;
    proxy_cache ZONE0;
    proxy_cache_key $host$uri$is_args$args;
    proxy_cache_valid 200 304 60m;

    expires 1h;
}

location /g/collect {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_pass https://$ga$uri$is_args$args&uip=$remote_addr;
    proxy_redirect off;
    # proxy_cache ZONE0;
    # proxy_cache_key $host$uri$is_args$args;
    # proxy_cache_valid 200 304 60m;
    proxy_cache off;

    expires epoch; # browser no-cache control
}

If you want to reverse proxy the Universal Analytics, see the reference gist: https://gist.github.com/fhferreira/14015373d49b6543601ec5b23f169089.

Although the above snippet have set uip=$remote_addr, the geolocation is still not correct in Google Analytic’s report…

What I’m actually implemented on this site is reverse proxy Baidu Tongji… 😂, much easy than Google’s system.

Ads by Google

林宏

Frank Lin

Hey, there! This is Frank Lin (@flinhong), one of the 1.4 billion 🇨🇳. This 'inDev. Journal' site holds the exploration of my quirky thoughts and random adventures through life. Hope you enjoy reading and perusing my posts.

YOU MAY ALSO LIKE

Using Liquid in Jekyll - Live with Demos

Web Notes

2016.08.20

Using Liquid in Jekyll - Live with Demos

Liquid is a simple templating language that Jekyll uses to process pages on your site. With Liquid you can output an modify variables, have logic statements inside your pages and loop over content.

Setup an IKEv2 server with StrongSwan

Tutorials

2020.01.09

Setup an IKEv2 server with StrongSwan

IKEv2, or Internet Key Exchange v2, is a protocol that allows for direct IPSec tunneling between two points. In IKEv2 implementations, IPSec provides encryption for the network traffic. IKEv2 is natively supported on some platforms (OS X 10.11+, iOS 9.1+, and Windows 10) with no additional applications necessary, and it handles client hiccups quite smoothly.