From ebfca61ac4d4b62b3e4a50477826a85c06b44552 Mon Sep 17 00:00:00 2001 From: Joe Mooring Date: Sat, 23 Mar 2024 13:07:00 -0700 Subject: [PATCH] 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 --- config/privacy/privacyConfig.go | 6 -- config/privacy/privacyConfig_test.go | 5 +- docs/data/docs.yaml | 2 - hugolib/embedded_templates_test.go | 39 ---------- hugolib/testhelpers_test.go | 1 - .../embedded/templates/google_analytics.html | 73 ++++++------------- .../templates/google_analytics_async.html | 29 -------- tpl/tplimpl/tplimpl_integration_test.go | 44 +++++++++++ 8 files changed, 67 insertions(+), 132 deletions(-) delete mode 100644 tpl/tplimpl/embedded/templates/google_analytics_async.html diff --git a/config/privacy/privacyConfig.go b/config/privacy/privacyConfig.go index a36046364..8880b1036 100644 --- a/config/privacy/privacyConfig.go +++ b/config/privacy/privacyConfig.go @@ -44,15 +44,9 @@ type Disqus struct { type GoogleAnalytics struct { Service `mapstructure:",squash"` - // Enabling this will disable the use of Cookies and use Session Storage to Store the GA Client ID. - UseSessionStorage bool - // Enabling this will make the GA templates respect the // "Do Not Track" HTTP header. See https://www.paulfurley.com/google-analytics-dnt/. RespectDoNotTrack bool - - // Enabling this will make it so the users' IP addresses are anonymized within Google Analytics. - AnonymizeIP bool } // Instagram holds the privacy configuration settings related to the Instagram shortcode. diff --git a/config/privacy/privacyConfig_test.go b/config/privacy/privacyConfig_test.go index c17ce713d..bff627f48 100644 --- a/config/privacy/privacyConfig_test.go +++ b/config/privacy/privacyConfig_test.go @@ -33,8 +33,6 @@ disable = true [privacy.googleAnalytics] disable = true respectDoNotTrack = true -anonymizeIP = true -useSessionStorage = true [privacy.instagram] disable = true simple = true @@ -60,8 +58,7 @@ simple = true got := []bool{ pc.Disqus.Disable, pc.GoogleAnalytics.Disable, - pc.GoogleAnalytics.RespectDoNotTrack, pc.GoogleAnalytics.AnonymizeIP, - pc.GoogleAnalytics.UseSessionStorage, pc.Instagram.Disable, + pc.GoogleAnalytics.RespectDoNotTrack, pc.Instagram.Disable, pc.Instagram.Simple, pc.Twitter.Disable, pc.Twitter.EnableDNT, pc.Twitter.Simple, pc.Vimeo.Disable, pc.Vimeo.EnableDNT, pc.Vimeo.Simple, pc.YouTube.PrivacyEnhanced, pc.YouTube.Disable, diff --git a/docs/data/docs.yaml b/docs/data/docs.yaml index ad47ac54d..f9b5731f2 100644 --- a/docs/data/docs.yaml +++ b/docs/data/docs.yaml @@ -1557,10 +1557,8 @@ config: disqus: disable: false googleAnalytics: - anonymizeIP: false disable: false respectDoNotTrack: false - useSessionStorage: false instagram: disable: false simple: false diff --git a/hugolib/embedded_templates_test.go b/hugolib/embedded_templates_test.go index a3b09e369..c409b0fbd 100644 --- a/hugolib/embedded_templates_test.go +++ b/hugolib/embedded_templates_test.go @@ -15,8 +15,6 @@ package hugolib import ( "testing" - - qt "github.com/frankban/quicktest" ) func TestInternalTemplatesImage(t *testing.T) { @@ -97,43 +95,6 @@ title: My Site `) } -// Just some simple test of the embedded templates to avoid -// https://github.com/gohugoio/hugo/issues/4757 and similar. -func TestEmbeddedTemplates(t *testing.T) { - t.Parallel() - - c := qt.New(t) - c.Assert(true, qt.Equals, true) - - home := []string{"index.html", ` -GA: -{{ template "_internal/google_analytics.html" . }} - -GA async: - -{{ template "_internal/google_analytics_async.html" . }} - -Disqus: - -{{ template "_internal/disqus.html" . }} - -`} - - b := newTestSitesBuilder(t) - b.WithSimpleConfigFile().WithTemplatesAdded(home...) - - b.Build(BuildCfg{}) - - // Gheck GA regular and async - b.AssertFileContent("public/index.html", - "'anonymizeIp', true", - "'script','https://www.google-analytics.com/analytics.js','ga');\n\tga('create', 'UA-ga_id', 'auto')", - " - -{{ else if hasPrefix . "UA-" }} - -{{- 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 }} + + + {{ 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 }} - - -{{ 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", + ``, + `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';`, + ) +}