Skip to content

Commit

Permalink
Fix google analytics feature (#989)
Browse files Browse the repository at this point in the history
* fix: Fix google analytics

* fix: Change respectDoNotTrack in GA html template

---------

Co-authored-by: Emruz Hossain <hossainemruz@gmail.com>
  • Loading branch information
joyao and hossainemruz authored Nov 26, 2024
1 parent 515b7eb commit 3be5a58
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
4 changes: 1 addition & 3 deletions layouts/partials/analytics.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
{{ with .services }}
<!-- Google Analytics -->
{{ with .google }}
{{ $privacyConfig:= dict (slice "Site" "Config" "Privacy" "GoogleAnalytics") $.Site.Config.Privacy.GoogleAnalytics }}
{{ $analyticsConfig := dict (slice "Site" "Config" "Services" "GoogleAnalytics" "ID") .id }}
{{ template "_internal/google_analytics.html" (merge $privacyConfig $analyticsConfig) }}
{{ partial "google_analytics.html" . }}
{{ end }}

<!-- Counter.dev -->
Expand Down
22 changes: 22 additions & 0 deletions layouts/partials/google_analytics.html
Original file line number Diff line number Diff line change
@@ -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 -}}

0 comments on commit 3be5a58

Please sign in to comment.