Print this Post

Where to put Google Analytics code in WordPress?

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']);
  (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);
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.

Short Link:

About the author

Prasanna SP

Student | WordPress Dev | Tech Blogger | Interested in GNU/Linux, FOSS, PHP, Drupal, WordPress, Ethical hacking, Photography, Painting, Literature etc..

Permanent link to this article: http://www.prasannasp.net/where-to-put-google-analytics-code-in-wordpress/


Skip to comment form

  1. Prasanna SP

    Thanks for your comments! 🙂

  2. brisbane

    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

  3. Ragavan

    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

  4. Ahmed


    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?

    1. Prasanna SP

      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.

  5. Ahmed


    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?


  6. Hemant

    Hi Prasanna,

    Your article is really helpful, thanks for writing. I am looking forward to read more articles in future. Keep writing!!! cheers.

  7. Ravina

    OMG… Finally i got it. Thanks dear admin to share the helpful post. Now i can put the analytic code into my blog easily.

Leave a Reply

Your email address will not be published. Required fields are marked *

Please put your code snippet between <code> and </code>. Comment moderation is in use. Please do not submit your comment twice -- it will appear shortly.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.