Navigation
This is archived content. Visit our new forum.
  • Author
    Posts
  • #225532

    mvives
    Participant

    Hi,

    I’d like to install Google’s Tag Manager code immediately after the <body> tag on every webpage. I’m using a child theme for integrity 1.

    In the past, I’ve inserted code using the Editor function within WordPress’s dashboard. However, I noticed that when using a child theme, there aren’t many files available to edit from within the WordPress dashboard.

    Where/How do I install the Google Tag Manager code, and will this prevent it from being overwritten with any updates to the X theme?

    Thanks!

    #225619

    Rue Nel
    Moderator

    Hello There,

    Thanks for posting in!

    You can create a custom function in your child theme’s functions.php that will insert the Google Tag manager code in the head section. Please consider the code below;

    // Add Google Tag Manager Code
    // =============================================================================
    function add_google_tag_manager_code(){
    ?>
    
    <!-- Insert Google Tage Code Here -->
    
    <!-- Insert Google Tage Code Here -->
    
    <?php 
    }
    add_action( 'wp_head', 'add_google_tag_manager_code' );
    // End Add Google Tag Manager Code
    // =============================================================================

    Please let us know if this has work for you.

    Thank you very much.

    #225630

    mvives
    Participant

    Thanks for the quick reply! I really appreciate it.

    I had a couple of questions based on what you recommended. In this screenshot, you’ll notice a red box. Based on your example, do you mean I should replace the two lines of code in the red box with the Google Tag manager code?

    Also, there are two red arrows. In one line it says “add_google_tag_manager_code” and another line says “add_google_tag_code”. Should these be the same or different as you typed?

    Thanks!

    #225664

    Rue Nel
    Moderator

    Hi Again,

    Sorry if the code is not that clear to you. Please see the revise code below;

    // Add Google Tag Manager Code
    // =============================================================================
    function add_google_tag_manager_code(){
    ?>
    
    <!-- Google Tag Code starts here  -->
    
    place the google tag manager code here
    
    <!-- Google Tag Code ends here -->
    
    <?php 
    }
    add_action( 'wp_head', 'add_google_tag_manager_code' );
    // End Add Google Tag Manager Code
    // =============================================================================

    Just insert your Google Tag Manager Code in between this two lines;

    
    <!-- Google Tag Code starts here  -->
    
    place the google tag manager code here
    
    <!-- Google Tag Code ends here -->
    

    Please let us know if anything is not clear enough.

    Thank you.

    #225688

    mvives
    Participant

    Hi again, I tried doing what you recommended, however, after pushing the changes live and viewing the source code, I cannot find the Google Tag Manager code anywhere.

    Is there another way or something that is missing?

    Please advise. Thanks!

    #225740

    Rue Nel
    Moderator

    Hello Again,

    Would you mind providing us the url of your site with login credentials, if necessary, so we can take a closer look? To do this, you can make a post with the following info:

    – Link to your site

    – WordPress Admin username / password (only if necessary)

    Don’t forget to select Set as private reply. This ensures your information is only visible to our staff.

    Thank you.

    #226042

    mvives
    Participant

    It’s showing up now. Thanks!

    I think there may have been an issue with copying and pasting of the code.

    #226043

    mvives
    Participant

    Actually, I have a follow up question. It is now working, but the code is being inserted in the <head> section.

    How do I place it immediately after the opening <body> tag rather than the <head> section?

    #226190

    Rad
    Moderator

    Hi there,

    You can replace this add_action( 'wp_head', 'add_google_tag_manager_code' );

    With this add_action( 'x_before_site_begin', 'add_google_tag_manager_code' );

    Cheers!

    #283520

    brandonlipman
    Participant

    I am looking for help with the same thing. I am in the process of creating a child theme but am unclear on how to add the appropriate code right after the `<body> tag.

    #283845

    Darshana
    Moderator

    @brandonlipman

    wp_head function will take care of that and make sure your code will be added before the body tag. You just have to follow instructions above.

    Thanks!

    #379289

    TobyElwin
    Participant

    I used the Google Chrome Tag Assistant Extension throughout and the above code would not work for me.

    As an interim solution I put the Google Tags code into the X – Google Analytics Extensions and placed the code in the footer and it worked.

    I kept returning to this functions.php solution over a series of days and too many hours. To finally get this code to work immediately after the opening <body> tag rather than the <head> section, I had to change this final line from: add_action( ‘x_before_site_begin’, ‘add_google_tag_manager_code’ );

    to: add_action( ‘x_before_site_begin’, ‘gtm_tracking_code’ );

    I put this Frankencode together with final help from this thread: https://community.theme.co/forums/topic/action-hooks-list-please/

    I hope this helps others as the Google Tag Manager is an outstanding resource and improves site speed SEO too.

    Toby

    #379455

    Rue Nel
    Moderator

    Hey Toby,

    This is very much helpful. We really appreciate it.
    Thanks for sharing.

    #382994

    mlsuffo
    Participant
    This reply has been marked as private.
    #382995

    Zeshan
    Member

    Hi there,

    There seems to be a error in your code. To fix this, please replace this line:

    add_action( ‘x_before_site_begin’, ‘gtm_tracking_code’ );
    

    With this:

    add_action( 'x_before_site_begin', 'add_google_tag_manager_code' );
    

    Hope this helps. 🙂

    Thank you.