Skip to content

Instantly share code, notes, and snippets.

@akhy
Forked from jirutka/analytics.conf
Created April 27, 2016 08:12
Show Gist options
  • Save akhy/48d45ec67e1c1fc5bc2fdd5b98028ab8 to your computer and use it in GitHub Desktop.
Save akhy/48d45ec67e1c1fc5bc2fdd5b98028ab8 to your computer and use it in GitHub Desktop.
Add Google Analytics tracking code to HTML via nginx
#
# Add Google Analytics tracking code to HTML response
#
# Usage:
# set $tracking_id 'UA-12345678-9';
# include incl/analytics.conf;
#
# It needs nginx compiled with option --with-http_sub_module.
# Uses optimized GA code from: http://mathiasbynens.be/notes/async-analytics-snippet
#
sub_filter '</head>' '<script type="text/javascript">var _gaq=[["_setAccount","$tracking_id"],["_trackPageview"]];(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];g.async=1;g.src="//www.google-analytics.com/ga.js";s.parentNode.insertBefore(g,s)})(document,"script")</script></head>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment