It is quite confusing to choose the proper method to install google tag manager on wordpress.
There are many plugins which are available free but some of them DO NOT follow google’s best practises.
We’ll be looking at 2 methods to setup tag manager in wordpress.
- Manually deploy in header.php
- Using Plugins
So let us start with 1st method:
1.) Tag Manager Setup - Manual Deploy in header.php
You will see 2 codes.
- As per the best practice the code should be placed high up in the <head> section
- As per the best practice the code should be placed immediately after the opening <body> tag.
The browser parses the page from top to bottom. So it is better to place Tag Manager script at near top.
The code will trigger even if placed at bottom, but might miss some hits if user migrates before complete page load.
- Copy these codes & open you wordpress admin in new tab.
- Login To Your WordPress Admin
- Go To Appearance > Editor
- Verify the theme on the top right corner dropdown.
- On Right Side, Search & Click on header.php file.
- Copy the header code & paste it immediately after thetag
- Then copy the body code & paste it immediately aftertag.
- Click on Update File.
- Click on Gif below to enlarge.
- Open your website in new tab
- Install the Tag Assistant Plugin
- Click on plugin icon > Enable
- Refresh the Page
- You should see Tag Manager install success.
- The Setup is quite easy & codes are placed as per google’s best practises.
- If you are using child theme, make sure to edit the child theme files & not parent theme’s.
- WP Updates might require to place the code again.
- Don’t change other code in the file. You just have to copy paste the tag manager code. You can use CTRL+Z to undo any other changes.
2.) Tag Manager Seutp - Deploy via Plugin
- In wordpress, Go To Install Plugin.
- Search for ‘Tag Manager’ plugin
- Install & Activate Duracell Tomi’s Tag Manager plugin.
- Go To Settings > Google Tag Manager.
- In General Tab, Enter your Google Tag Manager ID. (GTM-XXXXXX)
- Select Container Code Placement > Custom.
- Copy the code php below (check image)
- Go To Appearance > Editor.
- Paste the copied code just below the body tag.
- Click Update.
- Verify the placement using tag assistant plugin as in Step 5 (GTM manual placement)
- Manual code placement is needed in both the methods.
- In plugin, you can do much more as populating the dataLayer with post type, author, date, etc.
- You can also track scroll events, form fills,etc.
- Sometimes plugins might not be compatible with your theme & not track the required data.
- You will not have to place the code again after any WP updates.