diff options
-rw-r--r-- | _config.yml | 5 | ||||
-rw-r--r-- | _includes/extensions/google-analytics.html | 20 |
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); |