Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedIn

Google Analytics StatsI know … I know most of you especially beginners and those not into all the technical things will just install a plugin from the WordPress repository.

That’s great. There is nothing wrong with doing that. The reason I prefer to do it myself is that I get tired of all the plugin updates in the WordPress dashboard on a regular basis. To me they can be annoying, and not always worth updating anyway. I mean if the update is just for some simple css or text display what is the point?

What I do is manually add Google Analytics to my header or footer theme file for my own personal websites and projects. Occasionally I add it to child-theme/functions.php, and more recently I have been using my own custom plugin that will do the same.

The benefit of the custom plugin is that if I wanted to change themes the Google Analytics code would still work without any additional changes as long as the plugin was still active.

So, I will be showing you how to create a simple function that will add Google Analytics to your theme header or footer.

The functions.php – child-theme/functions.php way:

Hopefully you understand the value of a child theme when it comes to customizations because you will lose your changes after a theme update without one, but that’s up to you.

Open your theme or child-them/functions.php

At the bottom of the file add:

And save the changes.

That’s it, and it will be added to the footer of your website.

Google Analytics code varies depending on which you select in your account, and how long you have been using it, but the complete code would look something like this:

Simply refresh your website, and view the page source code to verify Google Analytics code is present.

Note:
If you use a cache/minify plugin you will want to clear that as well to see the changes.

Same thing if you are using something like CloudFlare or Incapsula with cache enabled. You will want to purge/clear the cache there as well.

If you already have Google Analytics code added make sure you remove it first. Otherwise, you will end up with a double set of tracking code and mess up your stats.

Additionally:
If you want it in the header instead, then change this from:

To:

 
If you want to push it down further so it appears later in the footer you can do this instead:

Or, this for the header:

 
Google Analytics With Your Own Plugin

 
The custom / anlytics plugin way:

If you intend to add other custom functions or code to this plugin, then create a new folder in your theme folder called:
my-custom-plugin

For just analytics create a folder called:
my-analytics-plugin

Make a file inside the newly created folder called:
my-plugin-name.php

You would end up with:
/wp-content/plugins/my-custom-plugin/my-plugin-name.php
Or:
/wp-content/plugins/my-analytics-plugin/my-plugin-name.php

Open it with an editor and add:

Save the changes to the file.

You can edit the plugin name, description, author, version, and author uri lines to your liking.

Go to your WordPress Dashboard
->Plugins
–>Installed Plugins

Find your new plugin in the list, and activate it.

Refresh your website, and view the page source code to verify Google Analytics code is present.

Note:
If you use a cache/minify plugin you will want to clear that as well.

Same thing if you are using CloudFlare or Incapsula with cache enabled. You will want to purge/clear the cache there too.

If you already have Google Analytics code added make sure you remove it first. Otherwise, you will end up with a double set of tracking code.

You can also alter the add_action line if you want it in the header instead just like the functions.php example above if you want.

About: Jeremy LeSarge - AKA: Ray (211 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.