• Hello. I am attempting to add the Google Tag Manager snippets of code into my website. I have read that to do this, simply modify the header.php file and add the first part of the code just below the opening head tag. No problem there. The issue I am having is with the second snippet. Google says to add the second snippet of code just before the opening body tag. I see a opening body tag in header.php but do not see any closing body tag. Something tells me I need to add the second snippet of code to a different file. Both snippets of code need to run on every page per Google.

    Please advise, thanks.

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter morrisonfirestick

    (@morrisonfirestick)

    These are the only body tags I see my header.php file:

    >

    <body <?php body_class(); ?> <?php oceanwp_schema_markup( 'html' ); ?>>
    <?php wp_body_open(); ?>

    Hello @morrisonfirestick,

    Thank you for reaching out.

    In this case, please follow the steps explained in this link:
    https://docs.oceanwp.org/article/709-add-google-tag-code-into-header-section.

    Also, you can search the WordPress repository to find a plugin for this: https://ww.wp.xz.cn/plugins/. Use a third-party plugin for Google Tag Manager, or find a plugin that allows custom code/snippets to add your code and assign it to the <head> tag.

    I hope it helps,
    Best Regards

    Thread Starter morrisonfirestick

    (@morrisonfirestick)

    Thank you for the reply. The document explains to create a child theme and then paste the code into the head part of the file. That would apply to the first part of the snippet which Google says to paste the code there.

    But my problem is the second snippet of code that Google says to paste right after the <body> tag.Where do I paste that exactly? (Check my inquiry again on WordPress as I posted the actual code from my header.php file)

    Hello @morrisonfirestick,

    Thank you for the update,

    For the second Google Tag snippet (the one Google says goes right after <body>), with OceanWP you don’t need to edit header.php directly. Instead, you can use the wp_body_open() hook that is already in your header.php file.

    Anything you want to appear immediately after the opening <body> tag can be hooked into wp_body_open(). Using a child theme function and if you’re comfortable with a bit of code, add this to your child theme’s functions.php: https://docs.oceanwp.org/article/90-sample-child-theme:

    add_action( 'wp_body_open', function() {
        // Paste your second Google Tag snippet here
    });

    This ensures it runs immediately after <body> on every page without manually editing theme files.

    Hope it helps,
    Best Regards

    Thread Starter morrisonfirestick

    (@morrisonfirestick)

    Thank you for the reply.

    I tried performing your steps. I can successfully create a child theme complete with imported customization, and per what I’ve read, copy the header.php file from the ‘primary’ theme, and paste it into the child theme. From there, I modify the header.php in the child theme and insert the first snippet of code from Google. No issues at all.

    The problem is editing the functions.php in the child theme. I insert your code and replace where you indicated with the snippet of Google code. The moment I save the changes to the functions.php file, it completely breaks my website and I have to perform a restore.

    Please advise. I am copying and pasting the code correctly into the header.php and functions.php files.

    Thanks.

    Hello @morrisonfirestick,

    Thank you for the feedback.

    The child theme itself isn’t causing the error, most likely the issue is with the code added to your child theme’s functions.php. Even a small syntax mistake (like a missing semicolon, quote, or bracket) can break the site completely.

    To help troubleshoot, could you:
    Share the exact code you added to functions.php here.
    Enable WordPress debugging by adding the following to wp-config.php if not already done:

    
    define( 'WP_DEBUG', true );
    define( 'WP_DEBUG_LOG', true );
    define( 'WP_DEBUG_DISPLAY', false );
    

    https://developer.ww.wp.xz.cn/advanced-administration/debug/debug-wordpress/
    Then check the debug log (wp-content/debug.log) and share any errors.

    Once we see the code and errors, we can pinpoint exactly what’s breaking your site and fix it safely.

    Hope it helps.
    Best Regards

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Adding Google Tag specifics’ is closed to new replies.