1
0
Fork 0
mirror of https://github.com/dillonzq/LoveIt.git synced 2025-03-13 11:18:15 +01:00

feat(analytics): update Google Analytics ()

This commit is contained in:
Dillon 2025-03-09 19:34:40 +08:00 committed by GitHub
parent a8be674413
commit c4baf9ee7d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 71 additions and 54 deletions
exampleSite
content/posts/theme-documentation-basics
hugo.toml
hugo.toml
layouts/partials/plugin

View file

@ -725,12 +725,15 @@ ignoreErrors = ["error-remote-getjson", "error-missing-instagram-accesstoken"]
# {{< version 0.2.0 >}} Analytics config
[params.analytics]
enable = false
# {{< version 0.3.1 deleted >}}
enable = true
# Google Analytics
[params.analytics.google]
id = ""
# whether to anonymize IP
# {{< version 0.3.1 deleted >}} whether to anonymize IP
anonymizeIP = true
# {{< version 0.3.1 >}} whether to respect the browsers “do not track” setting
respectDoNotTrack = false
# Fathom Analytics
[params.analytics.fathom]
id = ""
@ -810,7 +813,7 @@ ignoreErrors = ["error-remote-getjson", "error-missing-instagram-accesstoken"]
# {{< link "https://gohugo.io/about/hugo-and-gdpr/" "Privacy config" >}}
[privacy]
# {{< version 0.2.0 deleted >}} privacy of the Google Analytics (replaced by params.analytics.google)
# {{< version 0.3.1 changed >}} privacy of the Google Analytics (can also be configured in params.analytics.google)
[privacy.googleAnalytics]
# ...
[privacy.twitter]

View file

@ -727,12 +727,15 @@ ignoreErrors = ["error-remote-getjson", "error-missing-instagram-accesstoken"]
# {{< version 0.2.0 >}} 网站分析配置
[params.analytics]
enable = false
# {{< version 0.3.1 deleted >}}
enable = true
# Google Analytics
[params.analytics.google]
id = ""
# 是否匿名化用户 IP
# {{< version 0.3.1 deleted >}} 是否匿名化用户 IP
anonymizeIP = true
# {{< version 0.3.1 >}} 是否遵循浏览器的 “Do Not Track” 设置
respectDoNotTrack = false
# Fathom Analytics
[params.analytics.fathom]
id = ""
@ -812,7 +815,7 @@ ignoreErrors = ["error-remote-getjson", "error-missing-instagram-accesstoken"]
# {{< link "https://gohugo.io/about/hugo-and-gdpr/" "隐私信息配置" >}}
[privacy]
# {{< version 0.2.0 deleted >}} Google Analytics 相关隐私 (被 params.analytics.google 替代)
# {{< version 0.3.1 changed >}} Google Analytics 相关隐私设置 (也能在 params.analytics.google 配置)
[privacy.googleAnalytics]
# ...
[privacy.twitter]

View file

@ -658,13 +658,12 @@ ignoreErrors = ["error-remote-getjson", "error-missing-instagram-accesstoken"]
# Analytics config
# 网站分析配置
[params.analytics]
enable = false
# Google Analytics
[params.analytics.google]
id = ""
# whether to anonymize IP
# 是否匿名化用户 IP
anonymizeIP = true
# whether to respect the browsers “do not track” setting
# 是否遵循浏览器的 “Do Not Track” 设置
respectDoNotTrack = false
# Fathom Analytics
[params.analytics.fathom]
id = ""
@ -760,11 +759,11 @@ ignoreErrors = ["error-remote-getjson", "error-missing-instagram-accesstoken"]
# posts = ":year/:month/:filename"
posts = ":filename"
# Privacy config (https://gohugo.io/about/hugo-and-gdpr/)
# 隐私信息配置 (https://gohugo.io/about/hugo-and-gdpr/)
# Privacy config (https://gohugo.io/configuration/privacy/)
# 隐私信息配置 (https://gohugo.io/configuration/privacy/)
[privacy]
# privacy of the Google Analytics (replaced by params.analytics.google)
# Google Analytics 相关隐私 (被 params.analytics.google 替代)
# privacy of the Google Analytics (can also be configured in params.analytics.google)
# Google Analytics 相关隐私设置 (也能在 params.analytics.google 配置)
[privacy.googleAnalytics]
# ...
[privacy.twitter]

View file

@ -578,13 +578,12 @@
# Analytics config
# 网站分析配置
[params.analytics]
enable = false
# Google Analytics
[params.analytics.google]
id = ""
# whether to anonymize IP
# 是否匿名化用户 IP
anonymizeIP = true
# whether to respect the browsers “do not track” setting
# 是否遵循浏览器的 “Do Not Track” 设置
respectDoNotTrack = false
# Fathom Analytics
[params.analytics.fathom]
id = ""

View file

@ -1,43 +1,56 @@
{{- $analytics := .Scratch.Get "analytics" | default dict -}}
{{- if $analytics.enable -}}
{{- /* Google Analytics */ -}}
{{- with $analytics.google.id -}}
{{- /* Google Analytics */ -}}
{{- $google := $analytics.google.id | default site.Config.Services.GoogleAnalytics.ID -}}
{{- with $google -}}
{{- 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 -}}
{{- $respectDoNotTrack := $analytics.google.respectDoNotTrack | default site.Config.Privacy.GoogleAnalytics.RespectDoNotTrack -}}
<script>
window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}gtag('js', new Date());
gtag('config', '{{ . }}'{{ if $analytics.google.anonymizeIP }}, { 'anonymize_ip': true }{{ end }});
var doNotTrack = false;
if ({{ $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>
{{- printf "https://www.googletagmanager.com/gtag/js?id=%v" . | dict "Async" true "Source" | partial "plugin/script.html" -}}
{{- end -}}
{{- /* Fathom Analytics */ -}}
{{- with $analytics.fathom.id -}}
<script>
window.fathom=window.fathom||function(){(fathom.q=fathom.q||[]).push(arguments);};
fathom('set', 'siteId', '{{ . }}');
fathom('trackPageview');
</script>
{{- dict "Source" ($analytics.fathom.server | default "cdn.usefathom.com" | printf "https://%v/tracker.js") "Async" true "Attr" "id=fathom-script" | partial "plugin/script.html" -}}
{{- end -}}
{{- /* Plausible Analytics */ -}}
{{- with $analytics.plausible.dataDomain -}}
{{- dict "Source" "https://plausible.io/js/plausible.js" "Async" true "Defer" true "Attr" ($analytics.plausible.dataDomain | printf `data-domain="%v"`) | partial "plugin/script.html" -}}
{{- end -}}
{{- /* Yandex Metrica */ -}}
{{- with $analytics.yandexMetrica.id -}}
<script>
(function(m,e,t,r,i,k,a){m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)};
m[i].l=1*new Date();k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)})
(window, document, "script", "https://mc.yandex.ru/metrika/tag.js", "ym");
ym({{ . }}, "init", {
clickmap:true,
trackLinks:true,
accurateTrackBounce:true
});
</script>
<noscript><div><img src="https://mc.yandex.ru/watch/{{ . }}" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
{{- end -}}
{{- end -}}
{{- /* Fathom Analytics */ -}}
{{- with $analytics.fathom.id -}}
<script>
window.fathom=window.fathom||function(){(fathom.q=fathom.q||[]).push(arguments);};
fathom('set', 'siteId', '{{ . }}');
fathom('trackPageview');
</script>
{{- dict "Source" ($analytics.fathom.server | default "cdn.usefathom.com" | printf "https://%v/tracker.js") "Async" true "Attr" "id=fathom-script" | partial "plugin/script.html" -}}
{{- end -}}
{{- /* Plausible Analytics */ -}}
{{- with $analytics.plausible.dataDomain -}}
{{- dict "Source" "https://plausible.io/js/plausible.js" "Async" true "Defer" true "Attr" ($analytics.plausible.dataDomain | printf `data-domain="%v"`) | partial "plugin/script.html" -}}
{{- end -}}
{{- /* Yandex Metrica */ -}}
{{- with $analytics.yandexMetrica.id -}}
<script>
(function(m,e,t,r,i,k,a){m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)};
m[i].l=1*new Date();k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)})
(window, document, "script", "https://mc.yandex.ru/metrika/tag.js", "ym");
ym({{ . }}, "init", {
clickmap:true,
trackLinks:true,
accurateTrackBounce:true
});
</script>
<noscript><div><img src="https://mc.yandex.ru/watch/{{ . }}" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
{{- end -}}