Add support for Google Analytics v4
authorDaniel Atwood <daniel@atwood.io>
Wed, 3 Mar 2021 12:30:06 +0000 (07:30 -0500)
committerGitHub <noreply@github.com>
Wed, 3 Mar 2021 12:30:06 +0000 (13:30 +0100)
docs/content/en/about/hugo-and-gdpr.md
docs/content/en/templates/internal.md
hugolib/embedded_templates_test.go
hugolib/testhelpers_test.go
tpl/tplimpl/embedded/templates.autogen.go
tpl/tplimpl/embedded/templates/google_analytics.html

index df0234a3b71a1862a16cbe7d6a02e78e03cd3750..269c5d4325515acfcde9c3d662280e85c8b7b687 100644 (file)
@@ -92,6 +92,9 @@ respectDoNotTrack
 useSessionStorage
 : Enabling this will disable the use of Cookies and use Session Storage to Store the GA Client ID.
 
+{{% warning %}}
+`useSessionStorage` is not supported when using Google Analytics v4 (gtag.js).
+{{% /warning %}}
 ### Instagram
 
 simple
index e3d7e09c3be6b8d9ed7f4720c77f81adf8609f63..13d951aa1415465513f85359bbf6dccfbd26158e 100644 (file)
@@ -27,14 +27,20 @@ While the following internal templates are called similar to partials, they do *
 
 ## Google Analytics
 
-Hugo ships with internal templates for Google Analytics tracking, including both synchronous and asynchronous tracking codes.
+Hugo ships with internal templates for Google Analytics tracking, including both synchronous and asynchronous tracking codes. As well as support for both v3 and v4 of Google Analytics.
 
 ### Configure Google Analytics
 
 Provide your tracking id in your configuration file:
 
+**Google Analytics v3 (analytics.js)**
 {{< code-toggle file="config" >}}
-googleAnalytics = "UA-123-45"
+googleAnalytics = "UA-PROPERTY_ID"
+{{</ code-toggle >}}
+
+**Google Analytics v4 (gtag.js)**
+{{< code-toggle file="config" >}}
+googleAnalytics = "G-MEASUREMENT_ID"
 {{</ code-toggle >}}
 
 ### Use the Google Analytics Template
@@ -50,6 +56,8 @@ You can then include the Google Analytics internal template:
 {{ template "_internal/google_analytics_async.html" . }}
 ```
 
+When using Google Analytics v4 use `_internal/google_analytics.html`.
+
 A `.Site.GoogleAnalytics` variable is also exposed from the config.
 
 ## Disqus
index ab0b7b97fe055665eb40b6b1f251fbb61bfab3fe..c62d59b1de73c6ccd03b2cec4e2cccd34001d5c7 100644 (file)
@@ -110,7 +110,7 @@ Disqus:
        // Gheck GA regular and async
        b.AssertFileContent("public/index.html",
                "'anonymizeIp', true",
-               "'script','https://www.google-analytics.com/analytics.js','ga');\n\tga('create', 'ga_id', 'auto')",
+               "'script','https://www.google-analytics.com/analytics.js','ga');\n\tga('create', 'UA-ga_id', 'auto')",
                "<script async src='https://www.google-analytics.com/analytics.js'>")
 
        // Disqus
index 9e005d767a19e3d395ca65454a2bc3abb22d7652..59ae8458331117b794db2cb368d271343e23fe48 100644 (file)
@@ -249,7 +249,7 @@ const commonConfigSections = `
 [services.disqus]
 shortname = "disqus_shortname"
 [services.googleAnalytics]
-id = "ga_id"
+id = "UA-ga_id"
 
 [privacy]
 [privacy.disqus]
index 99adf292f1da6c2c5b4ae09f73c5a1324324373f..1391e89566b4150b8f36d66c07f5a7ee8168c69a 100644 (file)
@@ -121,8 +121,19 @@ var EmbeddedTemplates = [][2]string{
 <a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>{{end}}
 {{- end -}}`},
        {`google_analytics.html`, `{{- $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) {
@@ -148,8 +159,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 -}}
index 97588113e399d5cb26c8695e33f5e4321c27371b..f518b150c1ad6802365290a8756cf96376fc109c 100644 (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 -}}