Where to Put GTM Analytics code?

Where Should I put my Google Tracking code in the X theme or in the Child X theme?

Now I have GTM tracking code in the “Headers and Footers” plugin but getting error as a “non-standard implementation” in GTM extension…

In my Child theme, I have only " functions.php" and there is a header missing ?? Or it’s fine?

Also, when I try to put my code into “header.php” in X, it says “syntax error, unexpected ‘<’, expecting end of file”

Also, whY I got this error “Non-standard implementation” in GTM extension even when I properly put that code into header/footer?

So where should I put the code (child theme or parent theme) and how to put the code correctly in there? Thanks

Hi Viliam,

Thank you for writing in, sorry but we can not provide support for 3rd party plugins and script, please try using the Google Analytics Extension instead.

Please note that on this plugin you only need to put the Google Analytics ID on the Google Analytics ID field (not the entire GTM script)

Regarding the question about the parent and child theme, always add your customization on the child theme. Do not do any modification on the parent theme.

Happy New Year,
Cheers!

Well, when I put the code in the functions.php in the child theme, right after <?php then it says this error:

syntax error, unexpected ‘<’, expecting end of file … it’s so annoying for me sorry.

Happy NY You Too! (not ironic lol)

Hi Viliam,

That is because the Google tag is a JS script, not a PHP code, you need to wrap that into a PHP function first, see the method 2 provided here.

And do not put your custom function right after <?php, place it underneath the:

// Additional Functions // =============================================================================

Cheers!

I tried that but now the Google Tag Assistant is showing zero tags…

I made edit and it looks like this in my child theme functions.php… help please, I think it’s same as in the guide… what am I doing wrong with that ?

Hey Viliam,

It’s probably because you have extra <script> tag at the beginning and closing </script> tag at the end.

If that doesn’t help, please disable the HTML minification in your site. It’s probably done using an optimization plugin. If you’re not sure what’s minifying the HTML in your site, please hire a web developer.

Instead of manually coding in the child theme which is error prone, try using third-party plugins that can insert code to the header. Please read this: https://kinsta.com/knowledgebase/add-code-wordpress-header-footer/

Please note that we do not provide support both for custom code and third-party plugins so we can only go as far as showing you where to put the codes.

If you still have an issue with your GTM code, please hire a web developer.

Thanks.

okay no problem but very last thing, I found some different code now and it looks like this now

what “scripts” I have to remove because it has different sequence and placement ?

Thanks for that!

Hey Viliam,

You removed the PHP function and WordPress hook again. Go back to the suggestion of Friech here or follow what I suggested.

We already pointed where to put the code. If it’s too technical, please consider hiring a web developer.

Thanks.

how to close that php lol ? I cant even find any solution on google, thats funny

Hey Viliam,

I’d recommend you learn PHP to understand how it works. See https://www.w3schools.com/php/

Again, we already have pointed you to the right direction. We do not offer support for the custom codes you put in the child theme.

I will close down this thread now as the discussion is no longer connected to our theme.

Thank you for understanding.