I haven’t decided what theme to have on my WordPress site yet.
But I do want Google Analytics on the site.
Before installing Google Analytics on the site, do I need to decide on the theme?
i.e. does the Google Analytics code go into a file that is specific to the theme, or that changes with a changed theme?
Thanks
If you add tracking code to a theme file you will need to do this on every theme change.
It is considered good practice to use plugin for this, like Google Analytics for WordPress. It will use common hooks (that every theme worth using has) to insert code and that way you don’t need to think about that when changing.
Absolutely you can keep your analytics code Theme-agnostic.
Most analytics code is simply a script, and scripts can (and should) be enqueued using proper hooks. In this case, the relevant hooks require nothing more than Theme support for two all-but-universal template tags:
wp_head()
andwp_footer()
.Keeping the analytics code Theme-agnostic will require writing (or using an existing) Plugin – but not a complicated one, at all. Essentially, it would be simply a hook call, and an associated callback to define the analytics code. Something like this (assuming you’ve got your analytics code in a file called
jsanalytics.js
:And that’s it!
You don’t need to pick a theme ahead of time. But when you do pick a theme, be sure it’s one that follows WordPress best practices for design (i.e. uses
wp_footer()
in the appropriate location).For all of my hosted client sites, I use a mu-plugin to add analytics. This way, they can change their themes whenever they want without losing their analytics. The gist of the plugin is a function and a hook:
So long as their theme uses
wp_footer()
, the analytics scripts will always be added to the page.