And the action hook wp_enqueue_scripts will find out when/where to place the styles/scripts in its callable function my_theme_scripts_001().

Inserting Header and Footer Code for Tracking Codes

If you are inserting tracking codes, you will definitely and only want to use a plugin for this implementation.

It is a bad habit (although common) to place tracking codes within themes.

Yet, there are different variations of the category of plugins. Like if it is for Google Analytics tracking codes, you will realise that there are now so many of these out there on the WordPress Plugin Directory.

Let’s dig into the common tracking code plugins.

Top Google Analytics Tracking Code Plugins

Update: If possible, you will want to make use of Google Tag Manager for your Google Analytics implementation.

However, if you are unsure about using Google Tag Manager, or are unable to use it, then use one of the following Google Analytics plugins for WordPress below:

Top Google Tag Manager Tracking Code Plugins

The primary reason to setup Google Tag Manager is if you will be placing tags on your website. To aid your implementation, there are dedicated plugins to help you easily add GTM tracking codes across your website:

Top Conversion Optimisation Tracking Code Plugins

When it comes to heat maps, or user behaviour analyses, there are plugins that deal with that too. Normally these tracking codes will be added before the closing HTML head tag. But it does help with organisation to know they can be easily deactivated if not needed for the time being:

Popular Live Chat Implementation Plugins

There are dozens of live chat companies now, that they too have their own plugins to help you easily integrate with WordPress. And more importantly, to easily send updates to their tracking templates to enable enhanced features for you in the long-term (rather than you having to manually update the tracking code you manually placed):

Another great implementation to easier organise your code will be to use the Code Snippets Plugin by Shea Bunge. What makes this plugin great is that you can tag your PHP code snippets with anything, and is therefore the most customisable.

Tagging using the Code Snippets Plugin

For example, to specify that it is an implementation between a contact form and Google Analytics event tracking using the ga() command queue, you can tag the code snippet as: Google Analytics, and Formidable Forms. This is because that PHP code snippet is meant for Formidable Forms and Google Analytics.

Add Code to wp_head Using Code Snippets

Here is a tracking code template where you can place your tracking code script within the comment // Tracking Code Goes Here. Make sure to edit the function name my_tracking_code_001 with your own unique function name, or else you may get a PHP fatal error.

Some common examples of tracking codes you would like to move over to Google Tag Manager are:

Google Ads Tracking codes

Google Analytics Tracking codes

Hotjar Analytics Tracking codes

Infinity Cloud Tracking codes

Twitter Ads Tracking codes

The reason is that these are provided by Google Tag Manager, so you can rest assured that they will have easy implementations, as well as updated tracking code templates. They may release a better version of their tracking code with enhanced features in the next year or so.

And if you make use of their tracking code templates as found on Google Tag Manager, and have GTM installed on your website using one of the Google Tag Manager plugins above, then you’ve successfully set up a future-proof tracking code!

Conclusion

WordPress is well-known for its tens of thousands of plugins with well over 1 million installations on each one of them.

Furthermore, what keeps it going is how easy it is for these plugins to integrate themselves to the WordPress platform.

You might’ve realised that these plugins listed in the article for example have started out as simply acting as a head/footer hook for code, but later prove to do more than just allow you to paste your tracking code. This is the beauty of using plugins with good development and activity!