Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1222820
    metamedia
    Participant

    Hi,

    I am trying to properly install a Google Tag Manager code. I searched the forum and found this code to install in my child theme’s functions.php section:

    —————-

    function my_custom_head_output() {
    ?>
    <!– 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-W3PT5X’);</script>
    <!– End Google Tag Manager –>
    <?php
    }

    add_action( ‘wp_head’, ‘my_custom_head_output’, 99999 );

    function my_custom_footer_output() {
    ?>
    <!– Google Tag Manager (noscript) –>
    <noscript><iframe src=”https://www.googletagmanager.com/ns.html?id=GTM-W3PT5X&#8221;
    height=”0″ width=”0″ style=”display:none;visibility:hidden”></iframe></noscript>
    <!– End Google Tag Manager (noscript) –>
    <?php
    }

    add_action( ‘wp_footer’, ‘my_custom_footer_output’, 99999 );

    ———–

    However, that I keep getting the following error messages when testing with the Google tag assistant:

    “HTTP response code indicates tag failed to fire: Status 404. This could be due to an empty or un-published container”
    “Code found outside of <body> tag” message”

    Any ideas on how to fix this? Thanks!

    metamediacreative.com

    #1222989
    metamedia
    Participant

    Disregard this post. I overlooked that I had not yet published GTM. It appears to be working correctly now.

    #1222991
    metamedia
    Participant

    Re-regard this post. I’m still getting the “Code found outside of <body> tag” message.

    #1223291
    Rad
    Moderator

    Hi there,

    It is outside, since you added it through wp_head which is outside the <body> tag. Please change this line

    add_action( 'wp_head', 'my_custom_head_output', 99999 );

    to this,

    add_action( 'x_before_site_begin', 'my_custom_head_output', 99999 );

    Hope this helps.

    #1223880
    metamedia
    Participant

    I copied and pasted your line and now get:

    Parse error: syntax error, unexpected ‘function’ (T_FUNCTION) in /home/metamedi/public_html/wp-content/themes/x-child/functions.php on line 40

    #1223913
    metamedia
    Participant

    Line 40 is:

    function my_custom_footer_output() {
    ?>

    It broke the front end, so I had to change it back.

    #1224018
    metamedia
    Participant

    I found the following code in a thread:

    // 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
    // =============================================================================

    It appears to work. Thanks!

    #1224038
    Rahul
    Moderator

    Glad you’ve sorted it out for yourself.

    Let us know if you need any further help with the theme!

    Thanks!

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