Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1060992

    Hi there,

    I want to install Google Tag Manager (GTM) via child themes functions.php. I want to do it this way because it should be robust and efficient. I came up with the following code which I included in child theme functions.php

    function add_custom_gtm(){
    ?>
    <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=
    '//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer','GTM-XXXXXXX');
    </script><?php
    }
    add_action('wp_head', 'add_custom_gtm');
    
    function add_custom_gtm_noscript() {
    ?>
    <noscript><iframe src=”https://www.googletagmanager.com/ns.html?id=GTM-XXXXXX”
    height=”0″ width=”0″ style=”display:none;visibility:hidden”></iframe></noscript>
    <?php
    }
    add_action('ava_after_body_opening_tag', 'add_custom_gtm_noscript');

    However, it seems that GTM is not installed with this code, because Chromes extension Google Tag Assistant does not recognize GTM.

    What´s wrong and how to fix it?
    (Note: In the code example above I hide my GTM Id by replacing it with GTM-XXXXXXX )

    Thank you for the support.

    #1062225

    Hey P3T3R_0ne,

    Can you give us temporary admin access to your website in the private content box below, so that we can have a closer look?

    Are there any errors in the JavaScript console?

    Best regards,
    Victoria

    #1063647

    Hi Victoria,

    I tried again installing GMT again. It worked with the following code( the hook for the second function was wrong):

    function google_tag_head_script() {
    ?>
    <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-XXXXXXX');
    </script><?php
    
    }
    add_action( 'wp_head', 'google_tag_head_script' );
    
    function google_tag_after_body_script() {
    ?>
    <!-- Google Tag Manager (noscript) -->
    <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX"
    height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    <!-- End Google Tag Manager (noscript) --><?php
    
    }
    add_action('wp_footer', 'google_tag_after_body_script');
    

    Just a general question to finish this:

    Is this way of installing GTM via the above code in functions.php of child theme sound and robust? Isn´t it even better than installing GTM via copying the header.php into child theme, like proposed in other threads in the forum?

    #1066112

    Hi,

    The functions.php is just the quickest way. If u ask me, I also go to the functions.php file of the child theme and do it from there, as a function.
    But for everyone it is based on their knowledge. If you prefer modifying the header.php that is also fine.

    Best regards,
    Basilis

    #1188150

    So, is this a good (the best) way to implement GTM?

    Do you see any issues?

    If good, then you can close this thread.

    Best regards, Peter

    #1188580

    Hi P3T3R_0ne,

    This is the proper way to implement it :)

    Glad you got it working for you! :)

    If you need further assistance please let us know.
    Best regards,
    Victoria

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Clean GTM implemantation via child themes functions.php’ is closed to new replies.