Prismjs

Configuring Google-Analytics in Nginx

Reason

There are several significant differences between configuring Google-Analytics on the Nginx side and loading Google-Analytics in HTML,

  1. eliminate network problems between users and Google Analytics, especially in China (although it has been resolved to Google’s servers in Beijing)
  2. Prevent software like adblock from blocking
  3. improve page load speed

Configuration method

Add the following to the nginx configuration server block ``conf userid on. userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid; userid_name cid userid_name cid. userid_domain moonagic.com; userid_name cid; userid_domain moonagic.com; userid_path /. userid_name cid; userid_domain moonagic.com; userid_path /. userid_expires userid_name cid; userid_domain moonagic.com; userid_path /; userid_expires max. if ($http_accept_language ~* ‘^(; if ($http_accept_language ~* ‘^(. +?) ,’) { set $first_language $1; }

location @tracker { resolver 8.8.8.8 ipv6=off; # You need to set dns, otherwise it will not resolve. If your machine does not support ipv6, you need to disable ipv6. internal; proxy_method GET; proxy_pass https://www.google-analytics.com/collect?v=1&tid=UA-**-*&$uid_set$uid_got&t=pageview&dh=$host&dp=$request_uri&uip= $remote_addr&dr=$http_referer&ul=$first_language&z=$msec; proxy_set_header User-Agent $http_user_agent; proxy_pass_request_headers off. proxy_pass_request_headers off. proxy_pass_request_headers off. proxy_pass_request_body off. proxy_pass_request_headers off. proxy_pass_request_headers off. proxy_pass_request_body off. proxy_pass_request_headers off. proxy_pass_request_headers off. proxy_pass_request_headers off. proxy_pass_request_body off. proxy_pass_request_headers off. proxy_pass_request_headers off. proxy_pass_request_headers off. proxy_pass_request_headers off. proxy_pass_request_headers off. proxy_pass_request_headers off. proxy_pass_request_headers off. proxy_pass_request_headers off. proxy_pass_request_headers off. proxy_pass_request_headers off. proxy_pass_request_headers proxy_pass_request_body off; }


and when pointing to root or bypass, add the
``conf
location / {
    root /content/path/;
    post_action @tracker;
    ...
}

``conf location / { proxy_pass http://127.0.0.1:8081; post_action @tracker; … }

#### Postscript
This configuration will be recorded after access to a variety of files, including js, css, xml such that we actually do not need to access the statistics.  
The solution is simple, use Nginx configuration to filter out access to these static resources.  
For example, you can use ``conf
``conf
location ~ . * \\. (css|js|ico|jpg|woff|png|txt|xml|ttf)$ {
    root /content/path/;
    root /content/path/; or
    proxy_pass http://127.0.0.1:8081;
}