Include Google Analytics Tracking Code in NGINX Server Block

Article By Digamber Rawat on
Hello, I am going to teach you how you can include Google Analytics Proxy on Nginx server block. This small tutorial will help you to proxy any particular stylesheet, script and any file from your own domain. Below is the example in which I will proxy Google analytics on NGINX server block.

Proxy Google Analytics With NGINX Server

# Google Analytics Proxy
rewrite ^/ga.js$ /ga/ last;
location /ga/ {

After that check whether your nginx server block is working fine or not. Hit the below command to check any error.

sudo nginx -t

If your test is passed and not getting any error, then enter the below command to restart the nginx server.

sudo service nginx reload

Given above code rewrites the ga.js filename to the /ga/pseudo-directory. It will allow using the proxy_pass directive to get the file from Google.

By following this process, I will get the full control of the file which is being served.

This method can be used with any file but be aware that every file will increase the extra load on the server. Implementing a caching layer will be an added advantage.

Digamber Rawat

Feel free to contact me, If you are looking for a freelance full stack Developer, with the following skills: Mongo DB, Express JS, Angualr 2+, Node JS, Loopback JS, Firebase, Ionic Framework and WordPress. I also offer remote contracting service to the clients across the globe. Hire Me