Wordpress Google AnalyticsThere are several ways you can add Google Analytics to a self-hosted WordPress site. Some people will say one method is better than the other. It is not my intention of telling you should use one or the other. I am merely showing a few different options to getting the job done. Ultimately it is your website and the choice is yours.

 

 

1. Use a plugin such as Google Analytics for WordPress
2. Use your theme/framework’s built in feature
3. Manually add it to the header between <head> and </head>
4. Manually add it to the footer before the closing </body>

 

How to add Google Analytics to WordPress

1. Use a WordPress Plugin:
Using a plugin is probably the first thing people think about if their theme/framework doesn’t have a built-in feature to do so. Plugins can be great, but they can really add up fast. They create more maintenance and upkeep because usually at some point they need to be upgraded. There is also the possibility that they will break or stop working after a WordPress upgrade, and the plugin developer could disappear.

One of the benefits to using a plugin is if you switch to a different theme your Analytics code will continue working with the new theme since a plugin usually stores the code in the database rather than in a file.

 

2. Using a WordPress them or framework built in feature
Some WordPress themes and frameworks have a built-in feature that will allow you to paste your Analytics tracking code into a field and simply save the changes.

These are very easy to use. They only take a few seconds, and you are good to go.

The drawback is that if you switch to another theme you will have to add your code to the new theme, use a plugin, or manually add Analytics code.

WordPress Theme With Built In Feature:

Wordpress Theme Options

 

3. Manually Adding Google Analytics to the header
Google Analytics says to paste the tracking code immediately before the closing </head> tag for the asynchronous code that they seem to have standardize now. This doesn’t mean that you have to use this location, but rather it’s their recommendation.

Snippet From Google Analytics:

Analytics Tracking Code

To add Analytics using this optional method in WordPress you will need to go to your WordPress dashboard and find:
Appearance
-Editor
–header.php

Once the header.php page code is displayed you will need to find the closing head tag </head> and paste the code right before it.

Example:
Analytics code
</head>

Like this:
<script type=”text/javascript”>

var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-XXXXXXX-X’]);
_gaq.push([‘_trackPageview’]);

(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);
})();

</script>
</head>

Then, scroll down and save the changes.

WordPress Header Before:

Wordpress Header

 

WordPress Header After:

Updated WordPress Header

Note:
Some WordPress theme header.php files are a little confusing so make sure you properly add it to the right location.

 

4. Manually Adding Google Analytics to the footer
Even though Google Analytics tells you to put the code in the header you can also add it to footer.php right before the closing body </body> tag. Which, is actually the way they told us to do it a few years ago with the older Urchin Analytics tracking code.

To do so go to your WordPress dashboard and find:
Appearance
-Editor
–footer.php

After the footer.php page code is displayed scroll down to the end/bottom of the page and find the closing body tag </body> and past the Analytics code right before it.

Example:
Analyics code
</body>

Like this:
<script type=”text/javascript”>

var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-XXXXXXX-X’]);
_gaq.push([‘_trackPageview’]);

(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);
})();

</script>
</body>

Scroll down a little more and save the changes.

WordPress Footer Before:

Wordpress Footer

 

WordPress Footer After:

Updated WordPress Footer

 

Additional:
You should also clear/empty the cache if you are using a caching plugin like w3total cache, super cache, etc. just to make sure the changes are applied to your pages.

You can also verify that the code exists on your pages by using your web browsers view source/view page source option and look for the code you added in the header or footer area.

If you have properly added it you should see something like: Receiving Data in your Analytics account. It can take up to 24 hours before you start to seeing statistics so be patient.

If you manually added the code and you decide to change to another WordPress theme you would need to manually add your Google Analytics code again to the new theme files.

Important:
Do not add Google Analytics code more than once, or to more than one location/page or you will skew your statistics and data.

Other:
It is possible to add Analytics code to your themes functions.php file as well, but I elected to skip this one because it is a little more temperamental than the others mentioned.

About: Jeremy LeSarge - AKA: Ray (233 Posts)

I am the site owner and administrator of DialMe.com. I provide help and tips for Boonex Dolphin on the main part of this website where you will also find an assortment of other resources. Here, on the blog I write about a variety of topics surrounding WordPress, technology, social media/networking, SEO, and webmaster resources.