Dukeo » WordPress » WordPress & Google Analytics: Track Your Blog Without Editing Your Theme

WordPress & Google Analytics: Track Your Blog Without Editing Your Theme

Steven 2 responses WordPress

I use Google Analytics on all my WordPress-based websites. There simply isn’t any other free analytics that can compare with this one. The problem when implementing Google Analytics is that you must edit your theme files to paste the code where needed and you mustn’t forget to paste the code in your new theme whenever you are switching themes.

To make it easier, we are going to use a WordPress hook to insert our Google Analytics code without editing theme files. Insert the following code into the functions.php file of your WordPress theme.

add_action('wp_head', 'ga');

function ga() { ?>
    // Paste your Google Analytics code here
<?php } ?>

Don’t forget to paste your Google Analytics code on line 5.

If you enjoyed this article,
Get email updates

Join for Free »


  1. That’s great but I heard that with the asynchronous analytics the analytic code needs to go right before the head closing tag. However whenever I try to drop it in either in the functions.php or with a plugin or even directly into the header.php there is always a whole bunch of css and javascripts in between. Any idea how WordPress determines the order of items in the head tag?

    1. On Google’s site, they say that it needs to go right before the closing head tag, but the only thing that is necessary is that it’s in the head part of your page, having other stuff between Google Analytics’ code and your closing head tag doesn’t change squat.