cancel
Showing results for 
Search instead for 
Did you mean: 

Google tag manager wont stay in <head>

Google tag manager wont stay in <head>

I've been running an opencart store for several years now and recently made the switch to magento 2.
I've added the google tag manager code to the shop, but I can't seem to place it anywhere in the <head> tag because of (I think) requireJS moving the tag down into the body.
Mainly due to some problems I'm having with the registering of conversions (in Ads) I want the tag to be just after <head> per google's recommendation. I've tried a bunch of different ways to do it, but no matter how I try to do this (including having tested 3-4 different extensions that claim to do this), the snippet is always moved to the footer.

So, does anyone here knows how to get the **bleep** thing to stay put?

3 REPLIES 3

Re: Google tag manager wont stay in <head>

Hello @tom_petersen 

 

Please check if you have any extension for defer parsing of Javascript in your Magento 2 store.

 

The extension may cause the issue, taking it to the footer, in an attempt to optimize the speed.

 

Try disabling such an extension and check for the issue.

 

Hope it helps.

---
If you've found my answer useful, please give"Kudos" and "Accept as Solution"

Re: Google tag manager wont stay in <head>

Hi Meetanshi,

I don't have an extension to defer javascript installed. The only non-stock magento modules I have are:
mageplaza deletorders (to be able to remove test orders)
mageplaza multiflatrates (so I can set multiple flat shipping rates)
magmodules_googleshopping (to create a google shopping product feed)
mollie_payment (payment method module)
I have verified this again just now by looking through the list produced by running bin/magento module:status

Nothing related to javascript and no modifications were made to core files attempting to achieve JS deferral. This appears to be 'out of the box' magento behavior.

 

Things I've tried to get the GTM code to stick in the <head> tag:
- Added the snippet through content > configuration > store view > HTML Head > Scripts and Style sheets
- Added the snippet in frontend/themedev/themename/Magento_Theme/templates/html/gtm_head.phtml and put it in head through default.xml
- Added the snippet in frontend/themedev/themename/Magento_Theme/templates/proot.html
- Tried several different modules that add the GTM snippet.

All of the above have worked to far, but no matter how I implement it, the snippet is always moved to the footer.

Re: Google tag manager wont stay in <head>

Hi @tom_petersen ,

Have you finally found a solution for set GTM tag just after the head tag ?

Thanks,