From cca288b799f4a0f94e1453810e0ce96b3f3578bf Mon Sep 17 00:00:00 2001
From: Xen-Echo <67518109+Xen-Echo@users.noreply.github.com>
Date: Wed, 10 Mar 2021 21:40:56 +0000
Subject: [PATCH] Adding support for Google Analytics 4 (#149)
---
layouts/partials/google-analytics-gtag-async.html | 8 ++++++++
README.md | 7 +++++++
layouts/partials/footer.html | 4 ++++
3 files changed, 19 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
index 3503e1b..0eb3ba4 100644
--- a/README.md
+++ b/README.md
@@ -242,6 +242,13 @@
googleAnalytics = "UA-123-45"
```
+To use the modern Google Analytics 4, include the following under `[params]` replacing the id with your own.
+
+```toml
+[params]
+gtagId = "G-XXXXXXXXXX"
+```
+
### Simple Analytics
To use Simple Analytics, it has to be enabled by setting the parameter to true. If you are using a custom subdomain to evade Adblockers, then specify the URL without a trailing slash.
diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html
index 6e668c8..a9c6681 100644
--- a/layouts/partials/footer.html
+++ b/layouts/partials/footer.html
@@ -16,3 +16,7 @@
{{- partial "medium-zoom.html" . -}}
{{- partial "math.html" . -}}
{{- template "_internal/google_analytics_async.html" . -}}
+
+{{- if and (hugo.IsProduction) (.Site.Params.gtagId) -}}
+ {{ partial "google-analytics-gtag-async.html" . }}
+{{- end -}}
\ No newline at end of file
diff --git a/layouts/partials/google-analytics-gtag-async.html b/layouts/partials/google-analytics-gtag-async.html
new file mode 100644
index 0000000..0a5325d
--- /dev/null
+++ b/layouts/partials/google-analytics-gtag-async.html
@@ -0,0 +1,8 @@
+<!-- Global site tag (gtag.js) - Google Analytics -->
+<script async src="https://www.googletagmanager.com/gtag/js?id={{ .Site.Params.gtagId }}"></script>
+<script>
+ window.dataLayer = window.dataLayer || [];
+ function gtag(){dataLayer.push(arguments);}
+ gtag('js', new Date());
+ gtag('config', '{{ .Site.Params.gtagId }}');
+</script>
\ No newline at end of file
--
Gitblit v1.10.0