Easily Insert Google Analytics Into WordPress

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!

One Comment

  1. February 15, 2010

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

Leave a comment