Skip to content

Instantly share code, notes, and snippets.

@willscott
Created November 12, 2016 18:49
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willscott/1a1f2f9a0cb3ae357a891b9a059a100d to your computer and use it in GitHub Desktop.
Save willscott/1a1f2f9a0cb3ae357a891b9a059a100d to your computer and use it in GitHub Desktop.
Google Analytics Proxy
location = /analytics.js {
proxy_hide_header Alt-Svc;
resolver 8.8.8.8 ipv6=off;
proxy_pass https://www.google-analytics.com/analytics.js;
break;
}
location = /analytics {
access_by_lua_block {
ngx.req.read_body()
local body = ngx.req.get_body_data()
local uip = ngx.var.remote_addr
local h = ngx.req.get_headers()
local ua = h["User-Agent"]
if (ua == nil) then
ua = "unset"
end
ngx.req.set_body_data(body .. "&uip=" .. uip .. "&ua=" .. ua)
}
proxy_hide_header Alt-Svc;
resolver 8.8.8.8 ipv6=off;
proxy_pass https://www.google-analytics.com/collect;
break;
}
(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','" . "/analytics.js','ga');
ga('create', 'UA-xxxxxx-x', 'domain.com');
ga(function(u) {u.set('sendHitTask', function(model) {
var xhr = new XMLHttpRequest();
xhr.open('POST', '/analytics', true);
xhr.send(model.get('hitPayload'));
});});
ga('send', 'pageview');
@jvgreenaway
Copy link

This is cool. Is it possible to achieve this without access_by_lua_block or needing the Lua module

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment