A question that I see often on the Google Analytics Community, is how to install Google Analytics in WordPress.
Usually when installing Google Analytics to a website, the tracking code gets added to each page before the closing </head> tag. When using WordPress, the tracking code usually only need be added once to the correct file within your theme. This will then populate all existing and future pages and posts with tracking code before the closing </head> tag, removing any need to manually add it to the code of each page/post.
This article covers the different methods most used by site owners, to install Google Analytics on WordPress.
Method 1: Adding the tracking code to the active theme’s header.php file
Not Recommended
Manually editing and hard coding changes into core WordPress theme files goes against WordPress “best practices”.
- If the theme receives an update, any code edits will likely be overwritten and removed.
- Each time a theme is changed, the tracking code will need to be reinstalled into the header.php of the new active theme.
- Someone unfamiliar with WordPress and/or coding can easily break their website if a coding error occurs or a file is uploaded incorrectly to the server.
Method 2: Adding the tracking code via a provided theme setting
Better
- Requires no editing of core theme files
- Tracking wont get overwritten if the theme receives an update
- Tracking code will need to be reinstalled if the theme is changed
Whether this method is available, is entirely dependant on the theme you are using. Genesis by Studiopress is a commercial theme that gives the user the ability to add scripts to either the <head></head> or the footer of the site just before the closing </body> via the theme settings.
Divi by Elegant Themes is another theme that also has an option for adding script to the <head></head> section
Check your theme documentation to see if it offers a similar option for adding scripts.
Method 3: Using a child theme to add the tracking code
Better
A child theme is dependant on it’s parent theme being installed, and will inherit most of it’s functionality from the parent theme. If your active theme doesn’t have a theme settings option to add scripts to the head, then you may want to consider creating a child theme to make code related changes to your site. Using a child theme follows WordPress “best practices”.
- Child themes remove the need to edit core theme files.
- If coding errors are made, a child theme can usually be easily fixed, or deactivated without breaking the parent theme.
- Parent theme updates will not overwrite the edits and changes made in the child theme.
- Unfortunately, each time you change your theme, you’ll need to create a new child theme.
You can find information about how to create a child theme in the WordPress Codex.
There are two different ways you can add the tracking code … take your pick:
Copy the header.php file from the parent theme directory into your child theme directory and edit it to include the tracking code before the closing </head> tag,
or Create a php function that includes your tracking code (such as the following example), and put it in the child theme’s function.php file
// Add Google Analytics tracking code add_action('wp_head','bronv_ga_tracking_code', 200); function bronv_ga_tracking_code() { ?> <!-- Google Analytics --> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); //Swap out UA-XXXXX-Y with your own tracking id ga('create', 'UA-XXXXX-Y', 'auto'); ga('send', 'pageview'); </script> <!-- End Google Analytics --> <?php }
Without going into a huge, long explanation, I’ve called the function bronv_ga_tracking_code. I’ve used add_action, in this instance, it hooks my function with the tracking code to the wp_head action hook, which gets fired in the <head></head> of my site and outputs my tracking code within those tags. I’ve given it a priority of 200 which means my tracking code should pretty much be the last thing to be output just before the closing </head> tag
Tips
– Always clearly comment your code, (so you or someone else working with your code can easily recognize what the code does).
– Always prefix your functions to avoid possible naming conflicts with theme/plugin functions, (I usually prefix my functions with bronv).
Method 4: Using a plugin
My Preferred and Recommended Method
Using a plugin, separates non theme related functionality from the theme. This helps to ensure the functionality provided by the plugin, will keep working if the theme is changed or updated. Google Analytics tracking has zero to do with the theme’s appearance or the workings of the theme, so it makes sense to have the tracking installed via a plugin.
- No code edits/changes are manually made to core theme files.
- If the theme changes, tracking will continue to work and wont need to be reinstalled.
- For the most part, theme updates should not affect the plugin.
- Sometimes plugin updates will cause something to break (this is not something happens often – if ever).
There are a number of free plugins available on WordPress.org for installing Google Analytics. Two of the most widely installed are: Google Analytics Dashboard for WP and Google Analytics by Yoast.
When considering plugins, make sure to check out the plugin details
The easiest method for installing the plugin, is directly from the WordPress plugin repository via the plugin interface.
Installing directly from the WordPress Plugin Repository:
Click on the Plugins link then the Add New option and then type Google Analytics into the search box.
From the Search Results page, find the plugin you want to install and ensure it is up to date and compatible with your version of WordPress, then click the Install Now button:
Once installed, activate and follow the setup/configuration instructions for the particular plugin. Each plugin will be different.
Take the time to read the Installation, FAQ and Other Notes pages (if available). Sometimes they will contain links to videos with helpful configuration walkthrus and tips etc.
If you have problems with a plugin not working as expected, contact the plugin creator via the plugin’s support page on WordPress.org (unless the plugin creator has stated there is an alternative and preferred method for getting help).
If you need help with Google Analytics, visit the Google Analytics Community to ask your questions!