Skip to content

Commit

Permalink
Cleanup of Google analytics templates (#2006)
Browse files Browse the repository at this point in the history
  • Loading branch information
chalin authored May 16, 2024
1 parent 16979af commit 102892d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 72 deletions.
30 changes: 30 additions & 0 deletions layouts/_internal/google_analytics.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{{/*

This is a copy of https://github.com/gohugoio/hugo/pull/12505,
which changes all `{{- ... }}` to `{{- ... }}. Drop this file if
and once it is merged.

*/ -}}

{{ 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 }}
<script async src="https://www.googletagmanager.com/gtag/js?id={{- . }}"></script>
<script>
var doNotTrack = false;
if ({{ site.Config.Privacy.GoogleAnalytics.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 -}}
42 changes: 0 additions & 42 deletions layouts/_internal/google_analytics_gtag.html

This file was deleted.

7 changes: 1 addition & 6 deletions layouts/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,7 @@

{{/* To comply with GDPR, cookie consent scripts places in head-end must execute before Google Analytics is enabled */ -}}
{{ if hugo.IsProduction -}}
{{ $enableGtagForUniversalAnalytics := not .Site.Params.disableGtagForUniversalAnalytics -}}
{{ if (or $enableGtagForUniversalAnalytics (hasPrefix .Site.Config.Services.GoogleAnalytics.ID "G-")) -}}
{{ template "_internal/google_analytics_gtag.html" . -}}
{{ else -}}
{{ template "_internal/google_analytics.html" . -}}
{{ end -}}
{{ template "_internal/google_analytics.html" . -}}
{{ end -}}

{{ define "algolia/head" -}}
Expand Down
24 changes: 0 additions & 24 deletions userguide/content/en/docs/adding-content/feedback.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,6 @@ started** section of [Introducing Google Analytics 4 (GA4)][ga4-intro].
Enable Google Analytics by adding your project's analytics ID to the site
configuration file. For details, see [Configure Google Analytics][].

By default, Docsy uses the [gtag.js][] analytics library for both GA4 (which
_requires_ `gtag.js`) and Universal Analytics (UA) site tags. If you prefer using
the older `analytics.js` library for your UA site tag, then set
`params.disableGtagForUniversalAnalytics` to `true` in your project's [configuration file].

{{< tabpane >}}
{{< tab header="Configuration file:" disabled=true />}}
{{< tab header="hugo.toml" lang="toml" >}}
[params]
disableGtagForUniversalAnalytics = true
{{< /tab >}}
{{< tab header="hugo.yaml" lang="yaml" >}}
params:
disableGtagForUniversalAnalytics: true
{{< /tab >}}
{{< tab header="hugo.json" lang="json" >}}
{
"params": {
"disableGtagForUniversalAnalytics": true
}
}
{{< /tab >}}
{{< /tabpane >}}

{{% alert title="Deprecation note and warning" color="warning" %}}
<!-- Remove this warning once the Hugo docs have been updated to include it. -->

Expand Down

0 comments on commit 102892d

Please sign in to comment.