Google analytics Version 4 - hard code snippet request

Hi I have the new Version 4 tracking code that I need to add to my Meta Data. Unfortunately the Analytics plugin I’m currently using (John Linhart V1.4.0) doesn’t work with GA 4. How can I hard code the Google analytics snippet into the Meta Data on Grav please? I’m new to Grav so not sure how its done.

Many Thanks in Advance

Hi and welcome, @ChunkyChats!

It’ll be very simple to add this into your theme template. I suspect it’s not metadata you want to add, but an inline Javascript snippet. If that’s all it is, find the snippet and add something like this as per the Grav learn docs site:

{% script at 'bottom' %} {# only use "at ..." if it matters where in the document the JS is inserted - this will depend on your theme and what Google requires #}
// their JS snippet goes here
{% endscript %}

If you can provide these, a forum member can probably give you exact snippets if required:

  • the snippet(s) and instructions provided by Google
  • the theme you are using.

Also, there’s a plugin for the much simpler Plausible analytics if that is an option. I know it works.

There isn’t much to these plugins, you can usually add them yourself into the templates (as you’ve asked) and you learn more that way.

Many Thanks for the support, we have embedded it in the Theme as per your advice, and await results. for info the snippet is below. its the new code for Version 4 analytics: X is my tracking code. Google says it needs to be in the head section

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

  gtag('config', 'G-XXXXXXXX');

Great, I’m not quite sure if that means you have it solved :confused:

HI Hughbris, Apologies, I was waiting to see if it was working. The answer is yes. Although I am having a few little issues which I’m working through. I appear to have a duplicate entry in the meta data which I believe is associated with a legacy analytics plugin. We entered the Google snippet directly into the head on the template as recommended and are receiving data.

1 Like