-
AuthorPosts
-
March 11, 2015 at 8:28 pm #225532
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!
March 11, 2015 at 10:50 pm #225619Hello 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.
March 11, 2015 at 11:11 pm #225630Thanks 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!
March 12, 2015 at 12:37 am #225664Hi 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.
March 12, 2015 at 1:54 am #225688Hi 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!
March 12, 2015 at 3:22 am #225740Hello 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.
March 12, 2015 at 12:43 pm #226042It’s showing up now. Thanks!
I think there may have been an issue with copying and pasting of the code.
March 12, 2015 at 12:45 pm #226043Actually, 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?
March 12, 2015 at 3:44 pm #226190Hi 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!
May 27, 2015 at 5:42 pm #283520I 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.
May 27, 2015 at 9:57 pm #283845wp_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!
September 4, 2015 at 6:23 pm #379289I 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
September 5, 2015 at 2:01 am #379455Hey Toby,
This is very much helpful. We really appreciate it.
Thanks for sharing.September 9, 2015 at 6:19 am #382994This reply has been marked as private.September 9, 2015 at 6:22 am #382995Hi 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.
-
AuthorPosts