Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

How to implement Google Tag Manager?

Solved by Gray View solution

Started by Oliver Schlatter 6 years ago · 3 replies · 1630 views
6 years ago

Hi,

First, I'd like to say that I am a big fan of Grav and this Forum! I am a complete Newby but thanks to the great documentation and the posts here, I was able to create a pretty decent website without any prior knowledge.

Now, I would like to implement Google Tag Manager on the website. The GTM Quick Start Guide tells me to:

Copy the following JavaScript and paste it as close to the opening <head> tag as possible on every page of your website, replacing GTM-XXXX with your container ID:

HTML

<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXX');</script>
<!-- End Google Tag Manager -->

And the same for another snippet which goes into the body:

Copy the following snippet and paste it immediately after the opening <body> tag on every page of your website, replacing GTM-XXXX with your container ID:

TXT

<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->

I have been reading into the Asset Manager but I am not sure if this is the right way to go. I would really appreciate a quick pointer on this.

How/where do I implement those two code snippets on the website? Through the Asset Manager or can I just put them in the "base.html.twig" right after the <head> and <body> tag?

Thank you very much for your help!

last edited 05/07/20 by Oliver Schlatter
3 years ago

I couldn't find much info on it either, so I wrote up a guide after I figured it out. As @Andrass mentioned, putting it in the base.html.twig file is the solution.

Add GTM to Grav

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 56 13 hours ago
Support · by Anna, 3 days ago
2 61 16 hours ago
Support · by Justin Young, 17 hours ago
1 32 17 hours ago
Support · by Duc , 1 week ago
2 66 5 days ago
Support · by Colin Hume, 1 week ago
2 59 5 days ago