Google Tag Manager no script

Has there been a way to use a function to insert the Google Tag Manager no script code right after <body>

This method actually fails to launch the new tag debugger that Google has begun to roll out:

// Add Google Tag Manager Noscript Code
// =============================================================================
function add_google_tag_manager_code(){
?>

<?php } add_action( 'x_before_site_begin', 'add_google_tag_manager_code' ); Which adds it just after
I've resolved the GTM issue by creating in the child theme folder /framework/views/header/base.php and added the no script code directly after

On the side-note, if you are adding the GTM code manually, you’ll also have to implenent the Data Layer manually, which is not trivial.

Why not utilizing the industry-standard GTM for WP plugin from Thomas Geiger? It can inject both GTM tags into Pro without any conflicts, and it also has the fantastic Data Layer with many variables pre-configured.

Over the years, I’ve been trying to minimize the use of plugins, but I’ll take a look at the one you mentioned.

That said, for GTM, isn’t adding the two snippets that GTM provides (the tracking code and the noscript) all that’s necessary? I wasn’t aware that implementing a Data Layer was necessary.

I agree with you about minimizing plugins. However, GTM for WP is one of those situations where the benefits are greater than potential cons. I actually don’t se a con at all. The plugin’s author is a great and respectful professional and his plugin is working flawlessly for many years now.

The Data Layer is not necessary for basic use. If all you need is outputting scripts, than that’s all you need. But if you want to pick up information from the site, so you can feed this information into Facebook Pixel and Analytics for example, than you need the DL. This information can be as simple as the logged-in status so you don’t track admins for example, up to sending complex valuable information for retargeting and/or Remarketing purposes.

A while back WordPress added a hook for this so it would be standardized across themes. You can do something like this:

add_action('wp_body_open', function() { ?>

<script></script>

<?php });

Thanks @alexander , this is what I was missing.

Thanks @Misho. That was a great clarification on when to use the plugin.

1 Like

You’re welcome!