-
-
Save paivaric/211ca15afd48c5686226f5f747539e8b to your computer and use it in GitHub Desktop.
server { | |
listen 80; | |
server_name your.domain.com; | |
location = /analytics.js { | |
# you have to compile nginx with http://nginx.org/en/docs/http/ngx_http_sub_module.html (this is not default) | |
# and http://nginx.org/en/docs/http/ngx_http_proxy_module.html (it's a default module) | |
proxy_set_header Accept-Encoding ""; | |
sub_filter 'www.google-analytics.com' 'your.domain.com'; | |
sub_filter_types *; | |
sub_filter_once off; | |
proxy_pass https://www.google-analytics.com/analytics.js; | |
break; | |
} | |
location / { | |
proxy_pass http://www.google-analytics.com/; | |
} | |
} |
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ | |
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), | |
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) | |
})(window,document,'script','//your.domain.com/analytics.js','ga'); | |
# after this run your regular ga code | |
ga('create', 'UA-XXXXXXX-Y', 'auto'); | |
ga('set', 'userId', 'your logged user id'); | |
ga('send', 'pageview'); |
Hi @paivaric,
I checked my configuration several times; I'm pretty sure I didn't make any mistake in following your instructions.
However, I'm getting a lot of these errors:
upstream timed out (110: Connection timed out) while connecting to upstream,
Does it happen to you too?
Note that I'm not using the userId, nor Cloudflare.
Regards,
Benoit
Nevermind, I found the answer.
For some reason, IPv6 is not working correctly on my box.
Nginx timed out when it used the IPv6 address of www.google-analytics.com
.
The solution was to add the following line to the server
block:
resolver 8.8.8.8 ipv6=off;
And wait a long time (I would say 10m) until the cache expires.
I found a mistake in the NGINX configuration shown at the top.
It should not be:
proxy_pass http://www.google-analytics.com/;
but it should be:
proxy_pass http://www.google-analytics.com;
It's crucial to remove the trailing slash; otherwise, NGINX drops all the query parameters.
Some adblockers are strict and even block urls like '/r/collect/'
Sot he trick is to replace sub_filter 'r/collect' 'rrr/upstreamer';
then add a block of code
location /rrr/upstreamer/{
proxy_set_header Accept-Encoding "";
sub_filter 'www.google-analytics.com' 'subcode.example.com';
sub_filter_types *;
sub_filter_once off;
proxy_pass https://www.google-analytics.com/r/collect;
break;
}
Please keep in mind if people are going out of their way to block GA, they probably really do not want their data being collected by Google
if you are using cloudflare use real ip module to get the ip and set it in proxy query string
http://nginx.org/en/docs/http/ngx_http_realip_module.html
add this lines inside "server" block:
and then change the proxy_pass to:
proxy_pass http://www.google-analytics.com/$uri$is_args$args&uip=$remote_addr;