• Resolved zo

    (@zotezo)


    Hi,
    How can I exclude GTM code from AMP.
    I don’t want to load GTM code in AMP.
    This is the code

    /* Google Tag Manager START */
    add_action( 'wp_head','add_gtm_script_part' );
    function add_gtm_script_part () { ?>
    <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-543DQCK');</script>
    <?php }
    
    add_action( 'astra_body_top','add_gtm_noscript_part' );
    function add_gtm_noscript_part () { ?>
    <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-543DQCK"
    height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    <?php }
    /* Google Tag Manager END */

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Pascal Birchler

    (@swissspidy)

    You could use if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) { return; } as the first thing within your functions.

    This will prevent the JavaScript from being output on AMP pages.

    Thread Starter zo

    (@zotezo)

    Thank you Sir..Thank you very much..You are always helpful…

    Thread Starter zo

    (@zotezo)

    Hi @swissspidy,
    I have added this code but not working-

    /* Google Tag Manager START */
    if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
    add_action( 'wp_head','add_gtm_script_part' );
    function add_gtm_script_part () { ?>
    <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-543DQCK');</script>
    <?php }
    
    add_action( 'astra_body_top','add_gtm_noscript_part' );
    function add_gtm_noscript_part () { ?>
    <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-543DQCK"
    height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    <?php }
    } else {  }
    /* Google Tag Manager END */

    Can you please check?

    Plugin Author Pascal Birchler

    (@swissspidy)

    You have to add this code within your functions. Not outside. Here’s an example:

    add_action( 'wp_head','add_gtm_script_part' );
    
    function add_gtm_script_part () {
    if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) { return; }
    ?>
    <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-543DQCK');</script>
    <?php }
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Exclude GTM code from AMP’ is closed to new replies.