Updated on May 16, 2019

Looking to add Google Tag Manager code in your Oxygen-powered WordPress site?

Just add the following Snippet after installing and activating Code Snippets plugin:

add_action( 'wp_head', 'sk_google_tag_manager1', 1 );
/**
 * Adds Google Tag Manager code in <head> below the <title>.
 */
function sk_google_tag_manager1() { ?>

    <!-- 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( 'ct_before_builder', 'sk_google_tag_manager2' );
/**
 * Adds Google Tag Manager code immediately after the opening <body> tag.
 */
function sk_google_tag_manager2() { ?>

    <!-- Google Tag Manager (noscript) -->
    <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXX"
    height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    <!-- End Google Tag Manager (noscript) -->

<?php }

Replace the code inside the above functions with the snippets provided by Google in the Tag Manager interface or replace GTM-XXXXXX with your container ID.

Set the snippet to load on the frontend.

Reference: wp-content/plugins/oxygen/component-framework/oxygen-main-template.php.

Need help implementing a tutorial in your site or want to hire me for custom work?

HIRE ME

Find the article helpful and wish to donate?

DONATE

Comments

For adding code blocks wrap the code in three backticks. Markdown should work.
Provide a URL of your site/webpage if something is not working.
  • Hello Sridhar,

    Google Tag Manager is installed correctly, and the tag is detected in google tag manager, but none of the tags are actually firing! Could you help me out?

  • magnifiercross
    >
    linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram
    %d bloggers like this: