summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeffrey Tse <jeffreytse.mail@gmail.com>2023-06-24 11:01:02 +0800
committerJeffrey Tse <jeffreytse.mail@gmail.com>2023-06-24 11:21:44 +0800
commit54b30d735523c31237ab94959c983b0eeb5c4ebe (patch)
treed95b887420525712cd18f85231ee729ff41bd142
parentb8482e632c4996c3182114407a6fc73e8fb1aed0 (diff)
feat: google analytics 4 (GA4) support (#112)
-rw-r--r--_config.yml5
-rw-r--r--_includes/extensions/google-analytics.html20
2 files changed, 17 insertions, 8 deletions
diff --git a/_config.yml b/_config.yml
index 989eabb..59b1c2b 100644
--- a/_config.yml
+++ b/_config.yml
@@ -65,8 +65,9 @@ yat:
# If you want to change the content width, you can set to another value
# content_width: "920px"
-# Google analytics
-# google_analytics: [Tracking ID]
+# Google analytics - GA4
+# google_analytics: [Measurement ID]
+# enableDNT: "false"
# If you want to generate website sitemap, you can set true
# sitemap: false
diff --git a/_includes/extensions/google-analytics.html b/_includes/extensions/google-analytics.html
index a3b6d85..9ae8963 100644
--- a/_includes/extensions/google-analytics.html
+++ b/_includes/extensions/google-analytics.html
@@ -1,3 +1,4 @@
+<!-- Google tag (gtag.js) -->
<script>
function initGoogleAnalytics() {
var doNotTrack = (window.doNotTrack === "1" || navigator.doNotTrack === "1" ||
@@ -5,13 +6,20 @@
var enableDNT = "{{ site.enableDNT | default: true }}" == "true";
if (!enableDNT || !doNotTrack) {
- (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
- (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
- m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
- })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
+ var measurementId = '{{ site.google_analytics }}';
- ga('create', '{{ site.google_analytics }}', 'auto');
- ga('send', 'pageview');
+ (function(src) {
+ var tag = document.createElement('script');
+ tag.src = src;
+ tag.async = true;
+ var firstScriptTag = document.getElementsByTagName('script')[0];
+ firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
+ })('https://www.googletagmanager.com/gtag/js?id=' + measurementId);
+
+ window.dataLayer = window.dataLayer || [];
+ function gtag(){dataLayer.push(arguments);}
+ gtag('js', new Date());
+ gtag('config', measurementId);
}
}
window.addEventListener("load", initGoogleAnalytics);