I am new to magento2 and I'm trying to install Google Tag Manager on our websites. I'm adding the js code in the scripts and styls sheets field under the HTML Head. This is how it should look in the head:

<!-- Google Tag Manager -->
new Date().geTme(),eent:'gm.js});var f=d.getElemensByTagame(s)[0],
<!-- End Google Tag Manager -->

Unfortunately only this part of the script appears in the head:

<!-- Google Tag Manager -->

<!-- End Google Tag Manager -->

The rest of it that should be here appears later on in the body.

Am i doing something wrong or is there a fix to this?

2 Answers 2


can you please provide the screenshot of the code in the head tag and what appears in the body tag , so that it will make easier for debugging purposes anyway you should also consider turning off this setting in this path

Stores->Configuration->Advanced->Developer->JavaScript settings->Move JavaScript to Bottom of the page -> set this setting to No and check if your code works or not. (Please check the below screenshot for more info on this path) and make sure you are in developer mode and the scope is in the default config.

enter image description here

If yes , after disabling this setting your functionality works , then you need to over-ride a PHP file which will not move your code in the tag except your script tag all will move to the body tag , please do let me know if you need that kind of functionality.

I hope this post helped you , please do give me a like,as this helps me to answer more questions.

  • Hi, I dont have the option to be in dev mode, I guess this is something my organization has to set up? I have also updated the initial post with the code
    – Teo
    Oct 4, 2022 at 7:10
  • Also, If I set this setting to "no" and it breaks something, i would be able to just change it back and everything should work again?
    – Teo
    Oct 4, 2022 at 7:39
  • Okay so this setting will be turned on , you can check it by executing this command = bin/magento deploy:mode:set developer , this will change your magento instance to developer mode and after that those settings will be visible to you , and yes if this turned on then we need to use plugins and over-ride a method where we will disallow only this script tag to be moved to body tag.Please check this link for more ref - dckap.com/blog/exclude-script-from-magento-2-3 Oct 4, 2022 at 10:31

You can add it from the backend please follow the below step.

Admin -> Content -> Design -> Configuration -> Edit the Store View -> HTML Head -> Scripts and Style Sheets -> Save Configuration.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.