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

Steven2 responsesWordPress

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.

<?php
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.

Learn how I made $33,543 online in the past 12 months.

Simply input your best email below to get started.

2 Comments

  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.

Learn how I made $33,543 online in the past 12 months.

Simply input your best email below to get started.

No thanks, I have enough money