Integrate Google Tag Manager

Hi Themeco

I’m trying to integrate Google Tag Manager on my site and I would like to do it without installing yet another plugin - can you help me/guide me to do it correctly? I have a Tag Manager tracking Code for both header and body.

My website is www.redrive.dk

Looking forward to hearing.

Best
Carina

Hi There,

Upon checking your website I could see that you’ve already installed and activated the child theme.

Please add the following code under functions.php file locates in your child theme:

add_action( 'wp_head', 'print_google_tagmanager_script', 999 );
function print_google_tagmanager_script(){
	?>
<!-- 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-xxxxxx');</script>
<!-- End Google Tag Manager -->
	<?php
}

add_action( 'x_before_site_begin', 'print_script_before_site_begin' );
function print_script_before_site_begin(){
	?>	
	<!-- Google Tag Manager (noscript) -->
	<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-xxxxxx"
height="0" width="0" style="display:none;visibility:hidden"></noscript>
	<!-- End Google Tag Manager (noscript) -->
	<?php
}

Let us know how it goes!

1 Like

Thanks for your reply.

Like this?

After the Additional Functions should be better.

Regards!

Thanks, I’ll do that

Let us know how it goes!

It works for me!

Glad that it worked for you :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.