0

Hi here am trying to add a js script to the <head> tag,

methods used

  1. Content => Design => Configuration, Now click on the edit link of the current theme and Go to the HTML Head tab and inside scripts and style sheets text-area you can enter your custom script.

  2. create an XML file on theme location: app/design/frontend/packagename/themename/Magento_Theme/layout/default_head_blocks.xml

Created a template file on following location: app/design/frontend/packagename/themename/Magento_Theme/templates/html/header/head_tags.phtml

<script type="text/javascript">
 console.log("custom head!");
</script>
<script defer>(function (w, d, s, l, i) {
 w[l] = w[l] || []; w[l].push({
 'gtm.start':
 new Date().getTime(), event: 'gtm.js'
 }); var f = d.getElementsByTagName(s)[0],
 j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.async = true; j.src =
 'https://www.googletagmanager.com/gtm.js?id=' + i + dl; f.parentNode.insertBefore(j, f);
 })(window, document, 'script', 'dataLayer', 'GTM-NMWGPRJ');</script>
 <!-- End Google Tag Manager -->

But it's not getting under Header, it's coming on Body.

enter image description here

where I'm doing wrong. can I get some help? Thank you in advance

Viral Patel
1,1001 gold badge8 silver badges17 bronze badges
asked Nov 9, 2021 at 11:51
1
  • Using method one by adding script via backend AND cleaning cache is working fine for me. Script is added to head-tag. Commented Apr 30, 2024 at 10:37

1 Answer 1

0

Method 1: To add your Scripts and Style Sheets via admin panel:

  1. Go to CONTENT -> Design -> Configuration
  2. Click on the Edit link of your current theme.
  3. Under 'Other Settings' expand the HTML Head section.
  4. Place your script under the 'Scripts and Style Sheets' input box.
  5. Click on the 'Save Configuration' button.

Method 2: To add your Scripts and Style Sheets via layout and template file in theme:

  1. Go to location: app/design/frontend/vendor/yourtheme/Magento_Theme/templates/html/head.phtml

  2. Add your script code in head.phtml file.

  3. Save your file changes.

Clear your Magento store cache by running the below command after implementing the above changes.

php bin/magento cache:flush

Now check your script is placed under the <head> & </head> tags

answered Nov 9, 2021 at 12:07
3
  • hi @Ubed Shaikh Thank you for the response. Here I have used both methods. and added my script in app/design/frontend/vendor/yourtheme/Magento_Theme/templates/html/head.phtml and app/design/frontend/vendor/yourtheme/Magento_Theme/templates/html/header.phtml but still script is running inside <body>. Commented Nov 9, 2021 at 15:57
  • @PraveenNegimani Please use one method at a time. I recommend you for using Method 1 which is a default in Magento. Commented Nov 10, 2021 at 7:13
  • I have the same problem. With method 1, which seems to be recommended, <head> starts on line 3. My script is at line 65... below many other scripts and fonts. The script is a cookie tool, which should load before all other stuff to function well. But I can't seem to get it to the top of the head. As for method 2: I searched for head.phtml in my whole magento 2 installation and found no file with that name (find /var/www/html/magento2 -type f -name "head.phtml") Is this normal and if I create it, it will work? In that case, do I only put my script tag in head.phtml and nothing more? Commented May 25, 2023 at 14:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.