Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1356873

    Frocreate
    Participant

    I know this has been answered before, but it seems like Google has changed the way they like the code to be installed for tag manager.

    There are two scripts that Google requires on every page – one in the head and one after the opening body tag.

    Can you tell me how to access each of these on the front-end? I tried it in functions.php but got a 500 error. That was fun.

    Thanks!

    #1357179

    Darshana
    Moderator

    Hi there,

    Thanks for writing in! You can setup a child theme (refer knowledge base section) first and then add the following codes into your child theme’s functions.php file.

    To add anything into your WP Header section.

    
    // Add Google Tag Manager Code in Head
    // =============================================================================
    function my_custom_code1(){
    ?>
    
    <!-- Replace this line with your code provided by google -->
    
    <?php 
    }
    add_action( 'wp_head', 'my_custom_code1' );
    

    To add a code after body tag.

    
    // Add Google Tag Manager Code in Body
    // =============================================================================
    function my_custom_code2(){
    ?>
    
    <!-- Replace this line with your code provided by google -->
    
    <?php 
    }
    add_action( 'x_before_site_begin', 'my_custom_code2' );
    

    Hope that helps.

    #1418294

    soundview
    Participant

    This technique places the tag manager on the 154th line of the head (on my site). Is there a way to place it closer to the opening of the head tag?

    #1418676

    Friech
    Moderator

    Hi There,

    You can give a propriety to the first function given above.

    // Add Google Tag Manager Code in Head
    // =============================================================================
    function my_custom_code1(){
    ?>
    
    <!-- Replace this line with your code provided by google -->
    
    <?php 
    }
    add_action( 'wp_head', 'my_custom_code1', 10 );

    Notice the 10, the lower the closer to the <head> tag.

    Cheers!