June 26, 2019 · Web

Using Google Analytics while by-pass Ad-block

Is the ad-block blocked alot traffics from your site and you cannot use GA to get the correct result?

Try the following by pass nginx proxy script, it may help you.

server {

        set_real_ip_from 103.21.244.0/22;
        set_real_ip_from 103.22.200.0/22;
        set_real_ip_from 103.31.4.0/22;
        set_real_ip_from 104.16.0.0/12;
        set_real_ip_from 108.162.192.0/18;
        set_real_ip_from 131.0.72.0/22;
        set_real_ip_from 141.101.64.0/18;
        set_real_ip_from 162.158.0.0/15;
        set_real_ip_from 172.64.0.0/13;
        set_real_ip_from 173.245.48.0/20;
        set_real_ip_from 188.114.96.0/20;
        set_real_ip_from 190.93.240.0/20;
        set_real_ip_from 197.234.240.0/22;
        set_real_ip_from 198.41.128.0/17;
        set_real_ip_from 2400:cb00::/32;
        set_real_ip_from 2606:4700::/32;
        set_real_ip_from 2803:f800::/32;
        set_real_ip_from 2405:b500::/32;
        set_real_ip_from 2405:8100::/32;
        set_real_ip_from 2c0f:f248::/32;
        set_real_ip_from 2a06:98c0::/29;

# use any of the following two
        real_ip_header CF-Connecting-IP;

        server_name analytics.xxx.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'  'analytics.xxx.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/;
                #proxy_pass http://www.google-analytics.com/$uri$is_args$args&uip=$remote_addr;
        }

        listen 443 ssl; # managed by Certbot
                ssl_certificate /etc/letsencrypt/live/analytics.xxx.com/fullchain.pem; # managed by Certbot
                ssl_certificate_key /etc/letsencrypt/live/analytics.xxx.com/privkey.pem; # managed by Certbot
                include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
                ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}


server {
        if ($host = analytics.xxx.com) {
                return 301 https://$host$request_uri;
        } # managed by Certbot


        listen 80;
        server_name analytics.xxx.com;
        return 404; # managed by Certbot


}