Adding additional tracking code

Hi there, I have just found instruction on how to add the Google tag manager to the child theme’s functions.php file by using this code:

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
}

There are some tracking codes that aren’t compatible with the Google tag manager and so I will need to add their tracking code to the functions.php file too. I have tracking code for Ontraport which needs to be added below the body tag. Please could you supply me with the necessary code so that I can insert this additional tracking code below the google code without damaging the existing code.

Many thanks
Lewis

Hi Lewis,

You can add the code below after your code for google tag manager in your child theme’s functions.php file.

/* Ontraport Tracking Code */

add_action( 'wp_footer', 'ontraport_script', 999 );
function ontraport_script() { ?>

ADD YOUR ONTRAPORT SCRIPT HERE

<?php
}

Hope that helps.

Thanks very much!

You’re welcome.

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