I 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:
1 2 3 4 5 6 |
/*--Start Google Analytics Tracking Footer--*/ function add_google_analytics(){ ?> <!--Google Anlytics Tracking Code Here--> <?php } add_action('wp_footer', 'add_google_analytics'); /*--End Google Analytics Tracking Footer--*/ |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/*--Start Google Analytics Tracking Footer--*/ function add_google_analytics(){ ?> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXXXX-X']); _gaq.push(['_trackPageview']); _gaq.push(['_trackPageLoadTime']); (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> <?php } add_action('wp_footer', 'add_google_analytics'); /*--End Google Analytics Tracking Footer--*/ |
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:
1 |
add_action('wp_footer', 'add_google_analytics'); |
To:
1 |
add_action('wp_head', 'add_google_analytics'); |
If you want to push it down further so it appears later in the footer you can do this instead:
1 |
add_action('wp_footer', 'add_google_analytics', 30); |
Or, this for the header:
1 |
add_action('wp_head', 'add_google_analytics', 30); |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php /* Plugin Name: My Analytics Plugin Description: This plugin adds Google Analytics to header or footer. Author: You of yoursite.com Version: 1.0.0 Author URI: http://www.yoursite.com/ */ /*--Start Google Analytics Tracking Footer--*/ function add_google_analytics(){ ?> <!--Google Anlytics Tracking Code Here--> <?php } add_action('wp_footer', 'add_google_analytics'); /*--End Google Analytics Tracking Footer--*/ ?> |
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.