Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1195229

    Hello,
    I already have created a child theme to modify the header.php file.
    Can you please tell me precisely where in the code of the header.php file I should place the body code of Google Tag Manager?
    Once I’ve modified the child theme and determined it runs properly, should I then modify the parent theme the same way, or should I import those settings from the child theme to the parent theme?

    • This topic was modified 4 years, 10 months ago by JonathanUstin.
    #1195321

    Hey JonathanUstin,
    If you want to edit the header.php file look for this on line 81 </head> and add your script before or after it depending on if you want the code to fire in the “head” or the “body”
    Or you can add this code to the end of your functions.php file in Appearance > Editor if you want it in the header:

     function add_custom_gtm(){
    	?>
    	<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-MN3P7B"
    	height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    	<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=
    	'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
    	})(window,document,'script','dataLayer','GTM-MN3P7B');</script>
    	<?php
    	}
    	add_action('wp_head', 'add_custom_gtm');

    Please adjust the code to suit.
    If you want the code in the “body” add this code to the end of your functions.php file:

    add_action( 'ava_after_main_container', 'ava_after_main_container_mod', 10);
    function ava_after_main_container_mod() {
    	?><noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-MN3P7B"
    height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    <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=
    '//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer','GTM-MN3P7B');</script><?php
    }

    Please adjust the code to suit.

    Best regards,
    Mike

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.