There isn’t really a best when it comes to adding an Analytics tracking code to your WordPress website.
Reason being that there are lots of great ways to add it for future-proofing.
If you’re a web developer you will always put the future in mind in every work that is being done. It goes against the ethics of a web developer to ‘hard-code’ a feature. It’s just better to have a placeholder rather than hard-coding the feature.
Because let’s be honest 9 times out of 10 you aren’t going to improve your hard-coded feature unless you’ve got the time!
So best to have it right the first time as a web developer!
The Number #1 thing you shouldn’t do when adding the Google Analytics tracking code is placing it in your
footer.php of your theme. That is just absurd!
Even if it’s a child theme, your theme is your ‘style’. Your Analytics tracking code will most likely not change anytime soon, so you might as well never add Analytics tracking codes to your theme. Or any other tracking code of sort.Never add Analytics tracking codes to your #wordpress theme... ever! Click To Tweet
Finding out if the Analytics tracking code is in the theme
To figure out if the Analytics tracking code has been placed incorrectly into the theme's files, first check where the Analytics tracking code is present.
You can find out how present it is by viewing the source code of the website. Most modern web browsers will have this as either ⌘+alt+U or ⌘+U (on Mac), or Ctrl+U on Windows.
And once you're viewing the source, just do a quick Find (shortcut ⌘+F or Ctrl+F on Windows) for the supposed Analytics tracking code.
If you're able to find it all around the website, then it's most likely placed in either the
header.php or the
footer.php. Locate the Analytics tracking code through these theme files first. Who knows, you might find it elsewhere which would be even suspicious to the guy who set it up! I've seen instances where it's found in all theme files except the
And once you have found the analytics tracking code, please make it certain that it gets placed properly using any of the good solutions below (as well as removing the previous placement across the theme file/s).
A great plugin that’s been here for some time in the WordPress plugin directory is the Insert Headers and Footers plugin.
But recently there’s been some visual issue with it and Yoast (WordPress SEO plugin) in that if you have Yoast installed as well you will get a warning notification to deactivate your Insert Headers and Footers plugin.
This might be a pain point for a web developer if your client might ask about it. We want to avoid clients asking for repeated questions which shouldn’t have started in the first place.
Therefore we might not as well use this plugin. Sorry guys, but onto the better solution below!
Now, you’ve got the Yoast Analytics plugin (recently renamed to Monster Insights plugin). I would say that this is a competitor to the Insert Headers and Footers plugin. Smart move Yoast trying to mention to deactivate a somewhat competitor plugin! Monster Insights is great because you’ve easily got the ability to tick the 2 boxes for Demographic and Interests data and Enhanced Link Attribution on the fly! It’s great to have these features for a free plugin. Just remember to also enable these features in your Analytics property, and include a statement in your Cookies Policy mentioning you enable these features! If you want a quick setup go for this solution.
A step further up the ladder is to use the amazing DuracellTomi’s Google Tag Manager. There are lots of features you can choose from for this plugin. Use this if your WordPress website has other tracking codes on it.
But let’s keep it simple. Just add the Container ID. That’s it!
However, you will still have to setup Analytics on your Google Tag Manager account.
You can create a GTM Account here.
Follow the setup, and you will have a tracking code to add on different parts of your website. Instead, just get the Container ID, and paste it in the plugin.
And you should have your GTM code tracking through Analytics.
What makes Google Tag Manager amazing is that you only need to place the Container ID on your website (via the plugin mentioned), and you can place every other tracking code using the GTM webapp, rather than inserting tracking codes around your website one by one.
It’s a marketers dream. As well as an Analytics guy’s dream!
Think that you’re WordPress website has Analytics code best placed? Think again!
The Code Snippets plugin is akin to the
functions.php of your theme. But instead of just that you can containerise your code into snippets of code. You can attach tags to them — say ‘Google Analytics’, and you can deactivate a snippet if need be!
Of course, you will need basic knowledge using the WordPress hooks. But you’re a web developer and this will only take 10 minutes to master! I won’t dig down into the details since it’s all over the internet and on WordPress.org, but you can insert your analytics tracking code or even just your Google Tag Manager snippet, and that’s all there is to it!
If your website is simple and doesn’t need multiple tracking codes yet (so just Google Analytics), then you can use Monster Insights plugin.
Remember that the ‘theme’ is not the ‘website’. Therefore your Analytics code should never be in your theme, but instead somewhere else where it’s safe from theme changes!
If you liked this article please leave a comment below or share this with your fellow Web developer or Analytics guy! You might also want to check out my WordPress Google Analytics Setup Tutorial Guide, where I go in-depth using the proper GA webapp setup for your client.