Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/google/docsy.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrice Chalin <pchalin@gmail.com>2022-08-17 22:57:38 +0300
committerGeri Ochoa <gerino@protonmail.com>2022-08-31 17:53:50 +0300
commitc7c3fa7f4dc2ac318c59a68187b7647aac61e84d (patch)
treed1047a8da2e1e97db8f229d773d301bddc66dd25
parent6d987714561c9a87738c35f30c3019c09846ef16 (diff)
Use gtag.js analytics library for all site tags
-rw-r--r--layouts/_internal/google_analytics_gtag.html42
-rw-r--r--layouts/partials/head.html5
-rw-r--r--userguide/content/en/docs/adding-content/feedback.md6
3 files changed, 51 insertions, 2 deletions
diff --git a/layouts/_internal/google_analytics_gtag.html b/layouts/_internal/google_analytics_gtag.html
new file mode 100644
index 0000000..32b6a33
--- /dev/null
+++ b/layouts/_internal/google_analytics_gtag.html
@@ -0,0 +1,42 @@
+{{/*
+
+ This is a modified copy of
+
+ https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/google_analytics.html,
+
+ specifically this version:
+
+ https://github.com/gohugoio/hugo/blob/f7e00c039ff3cea5f991b05c1e325666004cf129/tpl/tplimpl/embedded/templates/google_analytics.html
+
+ The only differences between this copy and the original are that we've dropped:
+
+ - The `{{ if hasPrefix . "G-"}}` condition
+ - The `{{ else }}` block
+ - The `anonymize_ip` argument to the `gtag()` call, since it is superfluous.
+ For details, see https://github.com/gohugoio/hugo/issues/10093.
+
+*/}}
+
+{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
+{{- if not $pc.Disable }}{{ with .Site.GoogleAnalytics -}}
+<script async src="https://www.googletagmanager.com/gtag/js?id={{ . }}"></script>
+<script>
+{{ template "__ga_js_set_doNotTrack" $ }}
+if (!doNotTrack) {
+ window.dataLayer = window.dataLayer || [];
+ function gtag(){dataLayer.push(arguments);}
+ gtag('js', new Date());
+ gtag('config', '{{ . }}');
+}
+</script>
+{{- end }}{{ end -}}
+
+{{- define "__ga_js_set_doNotTrack" -}}{{/* This is also used in the async version. */}}
+{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
+{{- if not $pc.RespectDoNotTrack -}}
+var doNotTrack = false;
+{{- else -}}
+var dnt = (navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack);
+var doNotTrack = (dnt == "1" || dnt == "yes");
+{{- end -}}
+{{- end -}} \ No newline at end of file
diff --git a/layouts/partials/head.html b/layouts/partials/head.html
index fec2745..e6ae692 100644
--- a/layouts/partials/head.html
+++ b/layouts/partials/head.html
@@ -45,8 +45,9 @@
{{/* To comply with GDPR, cookie consent scripts places in head-end must execute before Google Analytics is enabled */ -}}
{{ if hugo.IsProduction -}}
- {{ if hasPrefix .Site.GoogleAnalytics "G-" -}}
- {{ template "_internal/google_analytics.html" . -}}
+ {{ $enableGtagForUniversalAnalytics := not .Site.Params.disableGtagForUniversalAnalytics -}}
+ {{ if (or $enableGtagForUniversalAnalytics (hasPrefix .Site.GoogleAnalytics "G-")) -}}
+ {{ template "_internal/google_analytics_gtag.html" . -}}
{{ else -}}
{{ template "_internal/google_analytics_async.html" . -}}
{{ end -}}
diff --git a/userguide/content/en/docs/adding-content/feedback.md b/userguide/content/en/docs/adding-content/feedback.md
index 55f0439..553488a 100644
--- a/userguide/content/en/docs/adding-content/feedback.md
+++ b/userguide/content/en/docs/adding-content/feedback.md
@@ -33,6 +33,11 @@ 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 false in your project's config.
+
{{% alert title="Warning" color="warning" %}}
<!-- Remove this warning once the Hugo docs have been updated to include it. -->
@@ -289,6 +294,7 @@ partial. For details, see [Customizing templates]({{< ref "lookandfeel#customizi
[Configure Google Analytics]: https://gohugo.io/templates/internal/#configure-google-analytics
[ga4-intro]: https://support.google.com/analytics/answer/1042508
[Google Analytics]: https://analytics.google.com/analytics/web/
+[gtag.js]: https://support.google.com/analytics/answer/10220869
[hugo-ga]: https://gohugo.io/templates/internal/#google-analytics
[internal templates]: https://gohugo.io/templates/internal/
[layouts/partials/page-description.html]: https://github.com/google/docsy/blob/main/layouts/partials/page-description.html