Easily Insert Google Analytics Into WordPress

graph

Google Analytics is a great tool for checking and analyzing website traffic. But at times it can be a pain to go through your theme, and find just the right place to put the tracking code. There are plugins that can do this, but they aren't really necessary to do this. Thanks to the magic of WordPress hooks, you don't need to worry!

First, in your theme's folder, find a file called functions.php. If it's not already there, create it.

Insert the following code into the file. It doesn't really matter where, but around the end is generally a good idea.

<?php
add_action('wp_footer', 'analytics');
 
function analytics() { ?>
Your Google Analytics tracking code
<?php }

Be sure to insert your own Google Analytics tracking code in there. This will add your tracking code into the footer of your WordPress blog.

This calls the add_action function, which is used to add a function to a hook. It is used by plugins and themes to easily modify certain parts in WordPress. Here we are using the wp_footer hook. The function we are using is called analytics.

Next we actually create the analytics function. It basically just displays what is on the line. Since it is called with the wp_footer hook, the text there (your tracking code) will be displayed in the footer of your site.

Stay Updated

Did you enjoy this post? Don't miss a single post by getting free updates!

10 Comments

  1. February 15, 2010

    thanks man exactlly what i want because i don't want to install plugin...
    Thanks again..

  2. March 16, 2010

    Do you have any idea how long it takes Google Analytics to start 'reading' my site after installing the code?

    • March 16, 2010

      I think probably a few hours to a day or two.

  3. Lynda
    May 2, 2010

    Hello, I just added google analytics to my footer php file...what's the difference if I leave it there or put it into the function php file in word press?

    • May 2, 2010

      Well if you're distributing the theme, it can help make it easier for users to modify the code and insert their own tracking code.

      Also, some new users may find editing the footer.php file a bit confusing, maybe not knowing exactly where to put it. But here, you just add one snippet of code, pretty much anywhere in your functions.php file.

  4. Trojan
    July 6, 2010

    Hi all, I have a problem trying to input the code in my website, the fact is i dont know how to do this, i am actually a beginner at this and need to learn this for a job. Can anyone please tell me how to start and finish this? I have a static website.

    • July 6, 2010

      If you have a static website, this won't work at all. :P

      For a WordPress blog if you just follow the instructions, it should work fine! If not, please describe your problem so I can help you.

  5. Sarah May
    December 4, 2010

    this is a good way of inserting google analytics into your blog, i also use it. got it from http://wordpressfunctions.com/performance-and-tracking/statistics/google-analytics-integration/

  6. February 14, 2011

    Thanks for the tip - I wasn't sure where to put the code :-)

  7. October 31, 2011

    ...do you have to just do this once for the website, or repeat for every page??

    Thanks!

    OL