Fix google analytics feature (#989)

* fix: Fix google analytics

* fix: Change respectDoNotTrack in GA html template

---------

Co-authored-by: Emruz Hossain <hossainemruz@gmail.com>
This commit is contained in:
JY Hsu 2024-11-27 07:59:35 +08:00 committed by GitHub
parent 515b7eb3bf
commit 3be5a58a7c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 23 additions and 3 deletions

View file

@ -4,9 +4,7 @@
{{ with .services }} {{ with .services }}
<!-- Google Analytics --> <!-- Google Analytics -->
{{ with .google }} {{ with .google }}
{{ $privacyConfig:= dict (slice "Site" "Config" "Privacy" "GoogleAnalytics") $.Site.Config.Privacy.GoogleAnalytics }} {{ partial "google_analytics.html" . }}
{{ $analyticsConfig := dict (slice "Site" "Config" "Services" "GoogleAnalytics" "ID") .id }}
{{ template "_internal/google_analytics.html" (merge $privacyConfig $analyticsConfig) }}
{{ end }} {{ end }}
<!-- Counter.dev --> <!-- Counter.dev -->

View file

@ -0,0 +1,22 @@
{{ if site.Params.features.analytics.enabled }}
{{- with site.Params.features.analytics.services.google.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.Params.features.analytics.services.google.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 -}}