In this short tutorial I'm going to show you how to add the JavaScript code from Google Tag Manager to a website made with WordPress without installing any plugin... well halfway because what we are really going to do is to create our own mini plugin that we will upload via FTP to the mu-plugins folder of our WordPress.
ADD Google Tag Manager code in WordPress
To be able to insert the Google Tag Manager code to our website made with WordPress, we are going to take advantage of the WordPress hooks, which allow us to insert functions in our website. Specifically we are going to use two hooks, wp_head and wp_footer in which we are going to define two functions to be able to insert in all the pages of our Web the two scripts that Google Tag Manager provides us when creating our container and that, following the instructions, we must insert in the header and after the opening of the body tag of each page.
To See how it is done, nothing better than watching the video that I leave below.
As you can see, the plugin is very easy to create and being in the mu-plugins folder you make sure that nobody who does not have access to the FTP can deactivate it. Next I leave you the php file that you will have to add to your function plugin or create a file with the name you want and the php extension and upload it by FTP to the mu-plugins folder of your server.
Remember that you have to change the phrase INSERT YOUR CONTENT IDENTIFIER HERE in both hooks for the value of the container provided by Tag Manager and that you can find in the work area of your Google Tag Manager account. Its syntax is GTM-XXXXXXXXX.
Conclusion
This is all for now, as you may have noticed, it is very easy to have this code added to your website without the need to install any plugin or add the code to the functions.php file or to the head template of your theme, which is not recommended at all.
From here, in following tutorials we will see how to add via Google Tag Manager different features to our WordPress.
If you have any questions, you can send them to me via the contact form.