google_analytics.html (1854B)
1 {{- $pc := .Site.Config.Privacy.GoogleAnalytics -}} 2 {{- if not $pc.Disable }}{{ with .Site.GoogleAnalytics -}} 3 {{ if hasPrefix . "G-"}} 4 <script async src="https://www.googletagmanager.com/gtag/js?id={{ . }}"></script> 5 <script> 6 {{ template "__ga_js_set_doNotTrack" $ }} 7 if (!doNotTrack) { 8 window.dataLayer = window.dataLayer || []; 9 function gtag(){dataLayer.push(arguments);} 10 gtag('js', new Date()); 11 gtag('config', '{{ . }}', { 'anonymize_ip': {{- $pc.AnonymizeIP -}} }); 12 } 13 </script> 14 {{ else if hasPrefix . "UA-" }} 15 <script type="application/javascript"> 16 {{ template "__ga_js_set_doNotTrack" $ }} 17 if (!doNotTrack) { 18 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ 19 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), 20 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) 21 })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); 22 {{- if $pc.UseSessionStorage }} 23 if (window.sessionStorage) { 24 var GA_SESSION_STORAGE_KEY = 'ga:clientId'; 25 ga('create', '{{ . }}', { 26 'storage': 'none', 27 'clientId': sessionStorage.getItem(GA_SESSION_STORAGE_KEY) 28 }); 29 ga(function(tracker) { 30 sessionStorage.setItem(GA_SESSION_STORAGE_KEY, tracker.get('clientId')); 31 }); 32 } 33 {{ else }} 34 ga('create', '{{ . }}', 'auto'); 35 {{ end -}} 36 {{ if $pc.AnonymizeIP }}ga('set', 'anonymizeIp', true);{{ end }} 37 ga('send', 'pageview'); 38 } 39 </script> 40 {{- end -}} 41 {{- end }}{{ end -}} 42 43 {{- define "__ga_js_set_doNotTrack" -}}{{/* This is also used in the async version. */}} 44 {{- $pc := .Site.Config.Privacy.GoogleAnalytics -}} 45 {{- if not $pc.RespectDoNotTrack -}} 46 var doNotTrack = false; 47 {{- else -}} 48 var dnt = (navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack); 49 var doNotTrack = (dnt == "1" || dnt == "yes"); 50 {{- end -}} 51 {{- end -}}