diff options
author | Jeffrey Tse <jeffreytse.mail@gmail.com> | 2023-06-24 11:01:02 +0800 |
---|---|---|
committer | Jeffrey Tse <jeffreytse.mail@gmail.com> | 2023-06-24 11:21:44 +0800 |
commit | 54b30d735523c31237ab94959c983b0eeb5c4ebe (patch) | |
tree | d95b887420525712cd18f85231ee729ff41bd142 /_includes | |
parent | b8482e632c4996c3182114407a6fc73e8fb1aed0 (diff) |
feat: google analytics 4 (GA4) support (#112)
Diffstat (limited to '_includes')
-rw-r--r-- | _includes/extensions/google-analytics.html | 20 |
1 files changed, 14 insertions, 6 deletions
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); |