Skip to content

Commit 7f9dc28

Browse files
chalingeriom
authored andcommitted
Use gtag.js analytics library for all site tags
1 parent c937bcd commit 7f9dc28

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{{/*
2+
3+
This is a modified copy of
4+
5+
https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/google_analytics.html,
6+
7+
specifically this version:
8+
9+
https://github.com/gohugoio/hugo/blob/f7e00c039ff3cea5f991b05c1e325666004cf129/tpl/tplimpl/embedded/templates/google_analytics.html
10+
11+
The only differences between this copy and the original are that we've dropped:
12+
13+
- The `{{ if hasPrefix . "G-"}}` condition
14+
- The `{{ else }}` block
15+
- The `anonymize_ip` argument to the `gtag()` call, since it is superfluous.
16+
For details, see https://github.com/gohugoio/hugo/issues/10093.
17+
18+
*/}}
19+
20+
{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
21+
{{- if not $pc.Disable }}{{ with .Site.GoogleAnalytics -}}
22+
<script async src="https://www.googletagmanager.com/gtag/js?id={{ . }}"></script>
23+
<script>
24+
{{ template "__ga_js_set_doNotTrack" $ }}
25+
if (!doNotTrack) {
26+
window.dataLayer = window.dataLayer || [];
27+
function gtag(){dataLayer.push(arguments);}
28+
gtag('js', new Date());
29+
gtag('config', '{{ . }}');
30+
}
31+
</script>
32+
{{- end }}{{ end -}}
33+
34+
{{- define "__ga_js_set_doNotTrack" -}}{{/* This is also used in the async version. */}}
35+
{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
36+
{{- if not $pc.RespectDoNotTrack -}}
37+
var doNotTrack = false;
38+
{{- else -}}
39+
var dnt = (navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack);
40+
var doNotTrack = (dnt == "1" || dnt == "yes");
41+
{{- end -}}
42+
{{- end -}}

layouts/partials/head.html

+3-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@
4545

4646
{{/* To comply with GDPR, cookie consent scripts places in head-end must execute before Google Analytics is enabled */ -}}
4747
{{ if hugo.IsProduction -}}
48-
{{ if hasPrefix .Site.GoogleAnalytics "G-" -}}
49-
{{ template "_internal/google_analytics.html" . -}}
48+
{{ $enableGtagForUniversalAnalytics := not .Site.Params.disableGtagForUniversalAnalytics -}}
49+
{{ if (or $enableGtagForUniversalAnalytics (hasPrefix .Site.GoogleAnalytics "G-")) -}}
50+
{{ template "_internal/google_analytics_gtag.html" . -}}
5051
{{ else -}}
5152
{{ template "_internal/google_analytics_async.html" . -}}
5253
{{ end -}}

userguide/content/en/docs/adding-content/feedback.md

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ started** section of [Introducing Google Analytics 4 (GA4)][ga4-intro].
3333
Enable Google Analytics by adding your project's analytics ID to the site
3434
configuration file. For details, see [Configure Google Analytics][].
3535

36+
By default, Docsy uses the [gtag.js][] analytics library for both GA4 (which
37+
_requires_ `gtag.js`) and Universal Analytics (UA) site tags. If you prefer using
38+
the older `analytics.js` library for your UA site tag, then set
39+
`params.disableGtagForUniversalAnalytics` to true in your project's config.
40+
3641
{{% alert title="Warning" color="warning" %}}
3742
<!-- Remove this warning once the Hugo docs have been updated to include it. -->
3843

@@ -289,6 +294,7 @@ partial. For details, see [Customizing templates]({{< ref "lookandfeel#customizi
289294
[Configure Google Analytics]: https://gohugo.io/templates/internal/#configure-google-analytics
290295
[ga4-intro]: https://support.google.com/analytics/answer/1042508
291296
[Google Analytics]: https://analytics.google.com/analytics/web/
297+
[gtag.js]: https://support.google.com/analytics/answer/10220869
292298
[hugo-ga]: https://gohugo.io/templates/internal/#google-analytics
293299
[internal templates]: https://gohugo.io/templates/internal/
294300
[layouts/partials/page-description.html]: https://github.com/google/docsy/blob/main/layouts/partials/page-description.html

0 commit comments

Comments
 (0)