2

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 -->
<script>(function(w,,s,l,i){[l]=w[l]||[]w[l].push({'gtm.start':
new Date().geTme(),eent:'gm.js});var f=d.getElemensByTagame(s)[0],
j=d.createElment(s),dl=l!='dataLayer'?'&l='+l:'';j.aync=true;j.src=
'https://www.gletaganagercom/tm.js?id='+i+dl;f.parntNode.insrtBfore(j,f);
})(window,docment,'script','dataLye''GTMMVPD2');</script>
<!-- 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

0

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.

3
  • 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
    Commented 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
    Commented 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 Commented Oct 4, 2022 at 10:31
0

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 you have read our privacy policy.

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