Skip to content

Instantly share code, notes, and snippets.

@frank-borkin
Created October 30, 2019 18:50
Show Gist options
  • Save frank-borkin/8141db320cc88c5b487a468adcee9392 to your computer and use it in GitHub Desktop.
Save frank-borkin/8141db320cc88c5b487a468adcee9392 to your computer and use it in GitHub Desktop.
nginx logs to google analytics
# send logs for all traffic to google analytics
#
# in server block:
# set $google_analytics_id "UA-THECORRECT-ID";
# include /etc/nginx/google_analytics;
#
# in location blocks:
# post_action @ga;
userid on;
userid_name cid;
userid_expires 2y;
#userid is contained in uid_got or uid_set, depending on if it's already been set
#also the cookie variable name is included (e.g. cid=ASDF), so no need to specify it in the query params
set $cid $uid_got$uid_set;
location @ga {
internal;
resolver 8.8.8.8 ipv6=off;
proxy_ignore_client_abort on;
proxy_next_upstream timeout;
proxy_connect_timeout 1s;
set $ga_api "https://www.google-analytics.com";
proxy_set_header Host "www.google-analytics.com";
proxy_pass $ga_api/collect?v=1&dl=$scheme://$host$request_uri&tid=$google_analytics_id&$cid&uip=$remote_addr&t=pageview&cm1=$body_bytes_sent&ds=nginx&dr=$http_referer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment