mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
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
This commit is contained in:
parent
e1917740af
commit
ebfca61ac4
8 changed files with 67 additions and 132 deletions
|
@ -44,15 +44,9 @@ type Disqus struct {
|
||||||
type GoogleAnalytics struct {
|
type GoogleAnalytics struct {
|
||||||
Service `mapstructure:",squash"`
|
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
|
// Enabling this will make the GA templates respect the
|
||||||
// "Do Not Track" HTTP header. See https://www.paulfurley.com/google-analytics-dnt/.
|
// "Do Not Track" HTTP header. See https://www.paulfurley.com/google-analytics-dnt/.
|
||||||
RespectDoNotTrack bool
|
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.
|
// Instagram holds the privacy configuration settings related to the Instagram shortcode.
|
||||||
|
|
|
@ -33,8 +33,6 @@ disable = true
|
||||||
[privacy.googleAnalytics]
|
[privacy.googleAnalytics]
|
||||||
disable = true
|
disable = true
|
||||||
respectDoNotTrack = true
|
respectDoNotTrack = true
|
||||||
anonymizeIP = true
|
|
||||||
useSessionStorage = true
|
|
||||||
[privacy.instagram]
|
[privacy.instagram]
|
||||||
disable = true
|
disable = true
|
||||||
simple = true
|
simple = true
|
||||||
|
@ -60,8 +58,7 @@ simple = true
|
||||||
|
|
||||||
got := []bool{
|
got := []bool{
|
||||||
pc.Disqus.Disable, pc.GoogleAnalytics.Disable,
|
pc.Disqus.Disable, pc.GoogleAnalytics.Disable,
|
||||||
pc.GoogleAnalytics.RespectDoNotTrack, pc.GoogleAnalytics.AnonymizeIP,
|
pc.GoogleAnalytics.RespectDoNotTrack, pc.Instagram.Disable,
|
||||||
pc.GoogleAnalytics.UseSessionStorage, pc.Instagram.Disable,
|
|
||||||
pc.Instagram.Simple, pc.Twitter.Disable, pc.Twitter.EnableDNT,
|
pc.Instagram.Simple, pc.Twitter.Disable, pc.Twitter.EnableDNT,
|
||||||
pc.Twitter.Simple, pc.Vimeo.Disable, pc.Vimeo.EnableDNT, pc.Vimeo.Simple,
|
pc.Twitter.Simple, pc.Vimeo.Disable, pc.Vimeo.EnableDNT, pc.Vimeo.Simple,
|
||||||
pc.YouTube.PrivacyEnhanced, pc.YouTube.Disable,
|
pc.YouTube.PrivacyEnhanced, pc.YouTube.Disable,
|
||||||
|
|
|
@ -1557,10 +1557,8 @@ config:
|
||||||
disqus:
|
disqus:
|
||||||
disable: false
|
disable: false
|
||||||
googleAnalytics:
|
googleAnalytics:
|
||||||
anonymizeIP: false
|
|
||||||
disable: false
|
disable: false
|
||||||
respectDoNotTrack: false
|
respectDoNotTrack: false
|
||||||
useSessionStorage: false
|
|
||||||
instagram:
|
instagram:
|
||||||
disable: false
|
disable: false
|
||||||
simple: false
|
simple: false
|
||||||
|
|
|
@ -15,8 +15,6 @@ package hugolib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
qt "github.com/frankban/quicktest"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestInternalTemplatesImage(t *testing.T) {
|
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')",
|
|
||||||
"<script async src='https://www.google-analytics.com/analytics.js'>")
|
|
||||||
|
|
||||||
// Disqus
|
|
||||||
b.AssertFileContent("public/index.html", "\"disqus_shortname\" + '.disqus.com/embed.js';")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestEmbeddedPaginationTemplate(t *testing.T) {
|
func TestEmbeddedPaginationTemplate(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|
|
@ -258,7 +258,6 @@ id = "UA-ga_id"
|
||||||
disable = false
|
disable = false
|
||||||
[privacy.googleAnalytics]
|
[privacy.googleAnalytics]
|
||||||
respectDoNotTrack = true
|
respectDoNotTrack = true
|
||||||
anonymizeIP = true
|
|
||||||
[privacy.instagram]
|
[privacy.instagram]
|
||||||
simple = true
|
simple = true
|
||||||
[privacy.twitter]
|
[privacy.twitter]
|
||||||
|
|
|
@ -1,51 +1,22 @@
|
||||||
{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
|
{{ if not site.Config.Privacy.GoogleAnalytics.Disable }}
|
||||||
{{- if not $pc.Disable }}{{ with .Site.Config.Services.GoogleAnalytics.ID -}}
|
{{ with site.Config.Services.GoogleAnalytics.ID }}
|
||||||
{{ if hasPrefix . "G-"}}
|
{{ if strings.HasPrefix (lower .) "ua-" }}
|
||||||
<script async src="https://www.googletagmanager.com/gtag/js?id={{ . }}"></script>
|
{{ 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." }}
|
||||||
<script>
|
{{ else }}
|
||||||
{{ template "__ga_js_set_doNotTrack" $ }}
|
<script async src="https://www.googletagmanager.com/gtag/js?id={{ . }}"></script>
|
||||||
if (!doNotTrack) {
|
<script>
|
||||||
window.dataLayer = window.dataLayer || [];
|
var doNotTrack = false;
|
||||||
function gtag(){dataLayer.push(arguments);}
|
if ({{ site.Config.Privacy.GoogleAnalytics.RespectDoNotTrack }}) {
|
||||||
gtag('js', new Date());
|
var dnt = (navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack);
|
||||||
gtag('config', '{{ . }}', { 'anonymize_ip': {{- $pc.AnonymizeIP -}} });
|
var doNotTrack = (dnt == "1" || dnt == "yes");
|
||||||
}
|
}
|
||||||
</script>
|
if (!doNotTrack) {
|
||||||
{{ else if hasPrefix . "UA-" }}
|
window.dataLayer = window.dataLayer || [];
|
||||||
<script>
|
function gtag(){dataLayer.push(arguments);}
|
||||||
{{ template "__ga_js_set_doNotTrack" $ }}
|
gtag('js', new Date());
|
||||||
if (!doNotTrack) {
|
gtag('config', '{{ . }}');
|
||||||
(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),
|
</script>
|
||||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
{{ end }}
|
||||||
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
|
{{ end }}
|
||||||
{{- if $pc.UseSessionStorage }}
|
{{ end }}
|
||||||
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 -}}
|
|
||||||
|
|
|
@ -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 -}}
|
|
|
@ -210,3 +210,47 @@ var a = §§{{.Title }}§§;
|
||||||
// This used to fail, but not in >= Hugo 0.121.0.
|
// This used to fail, but not in >= Hugo 0.121.0.
|
||||||
b.Assert(err, qt.IsNil)
|
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';`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue