Google Tag Manager code in functions.php

Hi @mldarshana,

This new thread is in response to your note on the other thread, called “Google Tag Manager.” Following the instructions in that thread, I’ve added both of the GTM scripts to my X (v.5.1.1) site, in the child theme, replacing my own GTM code blocks as required. However, the GTM Testing tool isn’t picking up the code just beneath the tag. I’m wondering if that’s perhaps because of the extra line of code after the opening tag. Is it possible to insert the code before this opening?

Thanks,
–Eric

Hi There @smartdataseo

Thank you for the information provided. Since you have other custom code in your functions.php file, I have moved Google tag manger code on top of the file.

Could you please purge your server cache (if you have any caching plugins installed) and then try testing your site again and see if that works.

Let us know how it goes.
Thanks!

Hi @mldarshana,

Thanks for that. Unfortunately, there’s no change to the placement of the second code (the block that should appear immediately beneath the opening tag. There’s still a line of code between the code and the GTM block. Code in the attached secure note.

Any further thoughts?

Thanks,
–Eric

Hi Eric,

Regarding the <noscript> immediately after the <body> tag. Please copy this file on your child theme.

x/framework/legacy/cranium/headers/views/global/_header.php

On that file you’ll see where the <body <?php body_class(); ?>> is being output. Add your gtm code just below that line. Do not include the hooks, just the <noscript> tag only.

<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-NR2G8QJ"
	height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>

Hope it helps,
Cheers!