Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1347922
    aleadfreak
    Participant

    Hi,

    I am having trouble installing the Google Tag Manager code on my functions.php page.

    Code found on another thread has led me to enter this:

    // Add Google Tag Manager Code
    // =============================================================================
    function add_google_tag_manager_code(){
    ?>
    
    <!-- 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-T7LD9VH');</script>
    <!-- End Google Tag Manager -->
    
    <?php 
    }
    add_action( 'x_before_site_begin', 'add_google_tag_manager_code' );
    // End Add Google Tag Manager Code
    // =============================================================================

    But using Google Tag Manager Extension, this isn’t working.

    Please help.

    Thanks

    Alex

    #1347926
    aleadfreak
    Participant
    This reply has been marked as private.
    #1348125
    Rahul
    Moderator

    Hi There,

    Thanks for writing in!

    As an alternative, you need to install a child theme and once your already have your child theme active and ready, please insert this following code in your child theme’s functions.php file.

    
    // Add Google Tag Manager Code
    // =============================================================================
    function add_google_tag_manager_code(){
    ?>
    
    <!-- Insert Google Tag Code starts here -->
    
    <!-- Insert Google Tag Code ends here -->
    
    <?php 
    }
    add_action( 'x_before_site_begin', 'add_google_tag_manager_code' );
    // End Add Google Tag Manager Code
    // =============================================================================
    

    Hope this helps. Kindly let us know.

    #1348154
    aleadfreak
    Participant

    Hi,

    Yes, that is what I have done. I already have a child’s theme. The code shown above is the same except it contains my Google tags code.

    Thanks

    Alex

    #1348617
    Friech
    Moderator

    Hi Alex,

    Was it the Google Tag manager consist of two sets of code? The first one should go inside the <head> tag

    /*THIS CODE WILL BE OUTPUT INSIDE THE <HEAD> tag*/
    add_action('wp_head','my_tracking', 1);
    function my_tracking () { ?>
      <!-- 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-T7LD9VH');</script>
    <!-- End Google Tag Manager -->
    <?php }

    And the other one is immediately after the opening <body> tag. Which you’ll be using for this code.

    // Add Google Tag Manager Code
    // =============================================================================
    function add_google_tag_manager_code(){
    ?>
    
    <!-- Insert Google Tag Code starts here -->
    
    <!-- Insert Google Tag Code ends here -->
    
    <?php 
    }
    add_action( 'x_before_site_begin', 'add_google_tag_manager_code' );
    // End Add Google Tag Manager Code
    // =============================================================================

    Please refer here: https://developers.google.com/tag-manager/quickstart

    Thanks.

    #1348893
    aleadfreak
    Participant

    Ok, so where within the child theme files would I put the code in the head section? In the Header.php file? Or in the functions file?

    To be clear, where do I put this:

    /*THIS CODE WILL BE OUTPUT INSIDE THE <HEAD> tag*/
    add_action('wp_head','my_tracking', 1);
    function my_tracking () { ?>
      <!-- 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-T7LD9VH');</script>
    <!-- End Google Tag Manager -->
    <?php }
    #1348964
    Thai
    Moderator

    Hi There,

    This code should be put under functions.php file locates in your child theme:

    /*THIS CODE WILL BE OUTPUT INSIDE THE <HEAD> tag*/
    add_action('wp_head','my_tracking', 1);
    function my_tracking () { ?>
      <!-- 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-T7LD9VH');</script>
    <!-- End Google Tag Manager -->
    <?php }

    Regards!

    #1357968
    aleadfreak
    Participant

    OK,

    Just so we’re clear…both bits of code (header code and body code) should be entered into the functions.php file.

    Thanks

    Alex

    #1358178
    Rue Nel
    Moderator

    Hello Alex,

    You do not have to edit any file to add the header code or body. Just place both of the codes in your child theme’s functions.php file. The code itself will instruct WordPress to add the Google tag manager in the head/body section.

    Hope this helps.

    #1381235
    aleadfreak
    Participant

    Hi,

    I’ve done exactly what was asked but I’m still getting an error.

    Can you take a look please.

    Thanks

    Alex

    #1381535
    Darshana
    Moderator

    Hi there,

    Please provide us with your FTP credentials to check your issue.

    Thanks!

    #1400425
    aleadfreak
    Participant
    This reply has been marked as private.
    #1401061
    Rad
    Moderator

    Hi there,

    I checked and it’s added correctly, what error you’re currently getting?

    Thanks!

  • <script> jQuery(function($){ $("#no-reply-1347922 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>