Created
November 12, 2016 18:49
-
-
Save willscott/1a1f2f9a0cb3ae357a891b9a059a100d to your computer and use it in GitHub Desktop.
Google Analytics Proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is cool. Is it possible to achieve this without
access_by_lua_block
or needing the Lua module