escopecz / grav-ganalytics

Google Analytics plugin for Grav CMS
MIT License
20 stars 9 forks source link

Unable to set Global Site Tag (gtag.js), cannot get script to appear in head #18

Closed frankfarm closed 1 year ago

frankfarm commented 6 years ago

In my Google Analytics account it says:

This is the Global Site Tag (gtag.js) tracking code for this property. Copy and paste this code as the first item into the of every webpage you want to track. If you already have a Global Site Tag on your page, simply add the config line from the snippet below to your existing Global Site Tag.

and the tracking code looks like this:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=[trackingid]"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', '[trackingid]');
</script>

where [trackingid] is my tracking id. However, when I use this plugin the code I get is

<script>
(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','//www.google-analytics.com/analytics.js','ga');
ga('create', '[trackingid]', 'auto');
ga('send', 'pageview');

</script>

and it appears just before the end of the body tag regardless of whether I choose Head or End of Body for the code position in the Grav plugin configuration.

How do I get the Global Site Tag code as shown above and as the first item in the head as Google recommends?

trianity commented 5 years ago

The Grav plugin support Analitics.JS and you would like to use an other script called GTag.JS. To use GTag.JS the plugin have to rewrite.

ViliusS commented 1 year ago

ganalytics plugins now uses gtag.js as required per GA4 support. Update to v2 and give it ago.

frankfarm commented 1 year ago

I haven't tried it yet, but thank you!