When you register your site at Google Analytics, it gives a piece of JavaScript and tells you to put that code immediately before the </head> tag on your site. Thats a tricky thing if you are using WordPress as CMS in your site. Because it is almost impossible to put the code on every page of WordPress blogs.
So where to put the Google Analytics code in WordPress? Well, here is a quick fix. You need to just add the analytics code to head section of your pages. To this, WordPress gives you an action hook – wp_head. Write a custom function and hook it to wp_head action hook. Here is a custom function which you can add to your child-theme‘s functions.php file. Since it is a php function, you need to put this anywhere between opening and closing php tags (between <?php and ?>)
function my_google_analytics_code() {
?>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<?php
}
add_action('wp_head', 'my_google_analytics_code'); |
Replace UA-XXXXXXXX-X with your actual Google Analytics code.
If you are using a WordPress theme which never gets an update from WordPress.org theme repository, you can directly edit header.php of your theme to insert Google Analytics code. To do this just open up the header.php file and paste your Google analytics code just above the </head> tag. header.php can be found in Dashboard --> Appearance --> Editor --> footer.php. If you don't have the access to edit the file there, then go to root/wp-content/themes/yourcurrenttheme/ directory in file manager and edit header.php file.
Follow these topics Technology, WordPress
Short Link:


6 comments
Skip to comment form ↓
Prasanna SP
November 22, 2011 at 9:38 am (UTC 5.5) Link to this comment
Thanks for your comments!
brisbane
December 12, 2011 at 10:41 pm (UTC 5.5) Link to this comment
I think that is one of the so much significant information for me. And i am satisfied studying your article. But want to statement on some general issues, The website style is ideal, the articles is actually nice : D. Good process, cheers
Ragavan
June 27, 2012 at 3:29 am (UTC 5.5) Link to this comment
Hi Prasanna,
Thanks for the article. I followed all your steps. But GA is not at all displaying in my website. I have All In one SEO plugin, GA for wordpress plugins and in Graphene theme itself I have pasted my tracking code. Pls help me on this
Ahmed
October 4, 2012 at 10:43 pm (UTC 5.5) Link to this comment
Hey,
I have also put Google Analytics code directly to Graphene Theme using theme option. Is it okay the process of putting the code directly to Graphene Theme? Do I have to put it on footer.php file?
Prasanna SP
October 4, 2012 at 10:59 pm (UTC 5.5) Link to this comment
Hi Ahmed!
Yes, you are doing it right. When you put Google Analytics code in Graphene Options, it will add the GA script to every page. No, need to follow this post. It is for other themes which have no option in the theme settings to add Google Analytics script.
Ahmed
October 5, 2012 at 10:31 pm (UTC 5.5) Link to this comment
Hey,
Thanks for your response. Now I am willing insert Google Ad-Sense code just below Nav bar or primary menu bar. or is it possible to insert Google Ad-Sense code in header section. Please do let me know if any possible way. or other alternative ad positions?
Regards
Ahmed