aboutsummaryrefslogtreecommitdiffhomepage
path: root/tpl
diff options
context:
space:
mode:
authorJoe Mooring <[email protected]>2024-03-23 13:07:00 -0700
committerBjørn Erik Pedersen <[email protected]>2024-03-26 15:40:51 +0100
commitebfca61ac4d4b62b3e4a50477826a85c06b44552 (patch)
tree2d82e184bcb6071f0edbad45672b4f98593a9a2b /tpl
parente1917740afe3fd08d1948947e25eff55e2c10d98 (diff)
downloadhugo-ebfca61ac4d4b62b3e4a50477826a85c06b44552.tar.gz
hugo-ebfca61ac4d4b62b3e4a50477826a85c06b44552.zip
tpl/tplimpl: Update Google Analytics template and config
Google Analytics 4 (GA4) replaced Google Universal Analytics (UA) effective 1 July 2023. See https://support.google.com/analytics/answer/11583528. Changes: - Update tpl/tplimpl/embedded/templates/google_analytics.html - Remove tpl/tplimpl/embedded/templates/google_analytics_async.html - Remove extraneous config settings Closes #11802 Closes #10093
Diffstat (limited to 'tpl')
-rw-r--r--tpl/tplimpl/embedded/templates/google_analytics.html73
-rw-r--r--tpl/tplimpl/embedded/templates/google_analytics_async.html29
-rw-r--r--tpl/tplimpl/tplimpl_integration_test.go44
3 files changed, 66 insertions, 80 deletions
diff --git a/tpl/tplimpl/embedded/templates/google_analytics.html b/tpl/tplimpl/embedded/templates/google_analytics.html
index c9a12e8e9..b8930d4bd 100644
--- a/tpl/tplimpl/embedded/templates/google_analytics.html
+++ b/tpl/tplimpl/embedded/templates/google_analytics.html
@@ -1,51 +1,22 @@
-{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
-{{- if not $pc.Disable }}{{ with .Site.Config.Services.GoogleAnalytics.ID -}}
-{{ 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>
-{{ template "__ga_js_set_doNotTrack" $ }}
-if (!doNotTrack) {
- (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','https://www.google-analytics.com/analytics.js','ga');
- {{- if $pc.UseSessionStorage }}
- if (window.sessionStorage) {
- var GA_SESSION_STORAGE_KEY = 'ga:clientId';
- ga('create', '{{ . }}', {
- 'storage': 'none',
- 'clientId': sessionStorage.getItem(GA_SESSION_STORAGE_KEY)
- });
- ga(function(tracker) {
- sessionStorage.setItem(GA_SESSION_STORAGE_KEY, tracker.get('clientId'));
- });
- }
- {{ else }}
- ga('create', '{{ . }}', 'auto');
- {{ end -}}
- {{ if $pc.AnonymizeIP }}ga('set', 'anonymizeIp', true);{{ end }}
- ga('send', 'pageview');
-}
-</script>
-{{- 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 -}}
-var doNotTrack = false;
-{{- else -}}
-var dnt = (navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack);
-var doNotTrack = (dnt == "1" || dnt == "yes");
-{{- end -}}
-{{- end -}}
+{{ if not site.Config.Privacy.GoogleAnalytics.Disable }}
+ {{ with site.Config.Services.GoogleAnalytics.ID }}
+ {{ if strings.HasPrefix (lower .) "ua-" }}
+ {{ warnf "Google Analytics 4 (GA4) replaced Google Universal Analytics (UA) effective 1 July 2023. See https://support.google.com/analytics/answer/11583528. Create a GA4 property and data stream, then replace the Google Analytics ID in your site configuration with the new value." }}
+ {{ else }}
+ <script async src="https://www.googletagmanager.com/gtag/js?id={{ . }}"></script>
+ <script>
+ var doNotTrack = false;
+ if ({{ site.Config.Privacy.GoogleAnalytics.RespectDoNotTrack }}) {
+ var dnt = (navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack);
+ var doNotTrack = (dnt == "1" || dnt == "yes");
+ }
+ if (!doNotTrack) {
+ window.dataLayer = window.dataLayer || [];
+ function gtag(){dataLayer.push(arguments);}
+ gtag('js', new Date());
+ gtag('config', '{{ . }}');
+ }
+ </script>
+ {{ end }}
+ {{ end }}
+{{ end }}
diff --git a/tpl/tplimpl/embedded/templates/google_analytics_async.html b/tpl/tplimpl/embedded/templates/google_analytics_async.html
deleted file mode 100644
index 93ecb753d..000000000
--- a/tpl/tplimpl/embedded/templates/google_analytics_async.html
+++ /dev/null
@@ -1,29 +0,0 @@
-{{ warnf "_internal/google_analytics_async.html is no longer supported by Google and will be removed in a future version of Hugo" }}
-{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
-{{- if not $pc.Disable -}}
-{{ with .Site.Config.Services.GoogleAnalytics.ID }}
-<script>
-{{ template "__ga_js_set_doNotTrack" $ }}
-if (!doNotTrack) {
- window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
- {{- if $pc.UseSessionStorage }}
- if (window.sessionStorage) {
- var GA_SESSION_STORAGE_KEY = 'ga:clientId';
- ga('create', '{{ . }}', {
- 'storage': 'none',
- 'clientId': sessionStorage.getItem(GA_SESSION_STORAGE_KEY)
- });
- ga(function(tracker) {
- sessionStorage.setItem(GA_SESSION_STORAGE_KEY, tracker.get('clientId'));
- });
- }
- {{ else }}
- ga('create', '{{ . }}', 'auto');
- {{ end -}}
- {{ if $pc.AnonymizeIP }}ga('set', 'anonymizeIp', true);{{ end }}
- ga('send', 'pageview');
-}
-</script>
-<script async src='https://www.google-analytics.com/analytics.js'></script>
-{{ end }}
-{{- end -}}
diff --git a/tpl/tplimpl/tplimpl_integration_test.go b/tpl/tplimpl/tplimpl_integration_test.go
index 1b2cffce6..abda3af29 100644
--- a/tpl/tplimpl/tplimpl_integration_test.go
+++ b/tpl/tplimpl/tplimpl_integration_test.go
@@ -210,3 +210,47 @@ var a = §§{{.Title }}§§;
// This used to fail, but not in >= Hugo 0.121.0.
b.Assert(err, qt.IsNil)
}
+
+func TestGoogleAnalyticsTemplate(t *testing.T) {
+ t.Parallel()
+
+ files := `
+-- hugo.toml --
+disableKinds = ['page','section','rss','sitemap','taxonomy','term']
+[privacy.googleAnalytics]
+disable = false
+respectDoNotTrack = true
+[services.googleAnalytics]
+id = 'G-0123456789'
+-- layouts/index.html --
+{{ template "_internal/google_analytics.html" . }}
+`
+
+ b := hugolib.Test(t, files)
+
+ b.AssertFileContent("public/index.html",
+ `<script async src="https://www.googletagmanager.com/gtag/js?id=G-0123456789"></script>`,
+ `var dnt = (navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack);`,
+ )
+}
+
+func TestDisqusTemplate(t *testing.T) {
+ t.Parallel()
+
+ files := `
+-- hugo.toml --
+disableKinds = ['page','section','rss','sitemap','taxonomy','term']
+[services.disqus]
+shortname = 'foo'
+[privacy.disqus]
+disable = false
+-- layouts/index.html --
+{{ template "_internal/disqus.html" . }}
+`
+
+ b := hugolib.Test(t, files)
+
+ b.AssertFileContent("public/index.html",
+ `s.src = '//' + "foo" + '.disqus.com/embed.js';`,
+ )
+}