Add support for Google Analytics v4

This commit is contained in:
Daniel Atwood
2021-03-03 07:30:06 -05:00
committed by GitHub
parent 782c79ae61
commit ba16a14c6e
6 changed files with 45 additions and 10 deletions

View File

@@ -1,6 +1,17 @@
{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
{{- if not $pc.Disable -}}
{{ with .Site.GoogleAnalytics }}
{{- if not $pc.Disable }}{{ with .Site.GoogleAnalytics -}}
{{ if hasPrefix . "G-"}}
<script async src="https://www.googletagmanager.com/gtag/js?id={{ . }}"></script>
<script>
{{ template "__ga_js_set_doNotTrack" $ }}
if (!doNotTrack) {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '{{ . }}', { 'anonymize_ip': {{- $pc.AnonymizeIP -}} });
}
</script>
{{ else if hasPrefix . "UA-" }}
<script type="application/javascript">
{{ template "__ga_js_set_doNotTrack" $ }}
if (!doNotTrack) {
@@ -26,8 +37,9 @@ if (!doNotTrack) {
ga('send', 'pageview');
}
</script>
{{ end }}
{{- end -}}
{{- end }}{{ end -}}
{{- define "__ga_js_set_doNotTrack" -}}{{/* This is also used in the async version. */}}
{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
{{- if not $pc.RespectDoNotTrack -}}