-
AuthorPosts
-
March 6, 2015 at 11:37 pm #222051
I have read a lot about the GA codes not working. Google currently says to put the code immediately after the opening <body> tag.
I did this on another WordPress site of mine running another theme that lets me edit the header.php file in the child theme and it works perfectly. The GA code is after the opening <body> tag. Google is happy. Here’s what it looks like-
<body <?php body_class(); ?> itemscope itemtype="http://schema.org/WebPage"> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-52307865-1', 'auto'); ga('send', 'pageview'); </script> <?php do_action('kleo_after_body');?> <!-- PAGE LAYOUT
But the header.php file in X looks quite different. How can I get this GA code inserted immediately after the <body> tag in X?
March 6, 2015 at 11:40 pm #222052Here’s the copy from the Google Verifications page-
Google Analytics Use your Google Analytics account. You must be using the asynchronous tracking code . <strong>Your tracking code should be in the <head> section of your page.</strong> You must have the "edit" permission for the Analytics web property. The Google Analytics tracking code is used only to verify site ownership. No Google Analytics data will be accessed.
March 7, 2015 at 3:58 pm #222362Hi there,
Thanks for writing in.
You can add your analytics code at Customizer’s custom javascript with no
<script></script>
. Or add it using our GA Extension that can be installed at Admin > Addons > Extensions.Or if you insist of adding it on header, then simply add this code at your child theme’s functions.php
add_action('wp_head', 'embed_my_ga_code'); function embed_my_ga_code () { ?> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-52307865-1', 'auto'); ga('send', 'pageview'); </script> <?php }
The instruction is add it on
<head>
, but not necessary to be directly to header.php. WordPress uses wp_head action so user can add script to <head> without directly modifying the file.Or if you wish to add it after
<body>
opening.add_action('x_before_site_begin', 'embed_my_ga_code'); function embed_my_ga_code () { ?> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-52307865-1', 'auto'); ga('send', 'pageview'); </script> <?php }
Then clear your caching plugin’s cache. Please disable it if you’re still on development. Any change will not reflect on real time if you did not clear and disable it.
Thanks!
-
AuthorPosts