Google Analytics is a fantastic service that gives you clear insight into your websites user experience and visitor traffic. I like to install Analytics on just about every site I build so I can get an accurate idea of the traffic and health of the site.
This post outlines several ways to add Google Analytics or Google Tag Manager (they do essentially the same, but tag manager adds code in two sections of the site) to your site, the pros and cons to each method, and when it is best to use each method.
Adding Google Analytics tracking to your website can be accomplished several different ways.
- Manually add tracking code directly to the template
- Use WordPress plugin
- Use template feature/field (if it exists)
Manually add tracking code directly to the template.
For most developers, this is super easy to do and gives you the benefit of 100% knowing the tracking code is inserted in the correct location.
The downside is that it takes WordPress coding experience or developer knowledge to execute this correctly.
Additionally, if a non-developer needs to make an update, they could have difficulty figuring out what you did and how to make the code change correctly, leading to a potential broken site and a way worse situation.
Several plugins provide this exact functionality, quickly allowing you to add the tracking code. I like these b/c they only do one thing, and that is adding GA tracking code to your website. They are super easy to use, and generally, all you have to do is enter the UA tag number, and they take care of everything else.
In the future, should a non-developer need to update the code, it is a matter of updating the plugin in the backend. Super easy.
The downside is you now have one more plugin installed on your site, and it could need updates in the future, or it could be a possible security risk, not likely but it could happen.
Use the template
Most modern templates have fields that allow you to paste the code into the WordPress admin backend, automatically inserting it into the template in the correct location.
This solution is suitable b/c you do not have to mess with any of the raw WordPress or template code, accessible from the WP backend and should be somewhat documented by your template maker.
The downside is this method can vary from template to template and may not be consistent across all the websites you manage. Since the templates vary from site to site, it can difficult to remember how the code was added several years after adding the tracking code.
What to do? Well, it depends on your situation.
When to use the template field method
If you own one site or always use the same template, I would add it to the template, if that feature exists. There is no need add any additional plugins, keeping your site cleaner with fewer plugins.
When to use a WordPress plugin
I would install a WordPress plugin if you are a WordPress beginner or the site will be turned over to a WordPress beginner. By installing a plugin, you know the added tracking code is accurate, easy to locate and modify, and see what tracking variables are inserted into your website. Sure it is one more plugin added to your site, but this additional plugin makes super easy for admins in the future to see how the tracking code was added and how to update it.
When to add the tracking code directly to the template
I would use the add the tracking code directly to the code if you are a super developer or knowledgeable developers will manage the site. This method is also useful if you want your site to be as efficient as possible, keeping plugins and additional code to a bare minimum.
What are your thoughts on the info or methodology in this post? I love getting different perspectives.
How do you usually add Google Analytics code to your WordPress site?
Steven Johnson, a WP Hosting Reviews senior editor, works from Atlanta and covers all things related to WordPress and Hosting.
He graduated from Georgia Tech in Chemical Engineering, has managed hosting companies and now builds WordPress and Joomla Websites for small to medium companies full time.