Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls
 

Include GTM Script immediately after <body> tag?

 
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)
 
Thread Tools Search this Thread
  #1  
Old 08-06-2016, 12:34 AM
 
paulmighty paulmighty is offline
 

Member
  
Join Date: Apr 2014
Posts: 11
 

Default Include GTM Script immediately after <body> tag?

Having a really tough go at getting my Google Tag Manager code to appear right after the <body> tag. I have the GTM being included in custom module, which is working great, except I can't get it to appear in the correct location. Google demands that the GTM script appear immediately after the <body> tag.

I am able to insert the script successfully into the <head> tag, but when I try to reconfigure to have it be included right after the opening <body> tag, I can never seem to get to it display before the <nav id="slider"> tag. Even when my GTM tpl has @ListChild (list="layout.slidebar", weight="5"), the <nav id="slider"> always appears first. When my GTM tpl has @ListChild (list="body", weight="5"), it also does not appear immediately after the <body> tag. I even tried setWeight(100) on 'XLite\View\Slidebar' in my runBuildCacheHandler, which changed the weight in the DB from 10 to 100, but didn't change the frontend hierarchy.

Any suggestion will be greatly appreciated. I'm running out of ideas :/

Thanks!
-Paul
__________________
X-Cart Version: 5.2
Reply With Quote
  #2  
Old 08-06-2016, 09:46 AM
 
paulmighty paulmighty is offline
 

Member
  
Join Date: Apr 2014
Posts: 11
 

Default Re: Include GTM Script immediately after <body> tag?

In addition to trying to use a few combos of @ListChild list="body" and list="layout.slidebar" with my custom .tpl I also tried an alternate approach modifying the skins/mytheme/customer/en/body.tpl template like below, where getGTMCode() is a function defined in my custom module:
Code:
<!DOCTYPE html> <html lang="{currentLanguage.getCode()}"{foreach:getHTMLAttributes(),k,v} {k}="{v}"{end:}> <widget class="\XLite\View\Header" /> <body {if:getBodyClass()}class="{getBodyClass()}"{end:}> {if:getGTMCode()}<!-- Google Tag Manager -->{getGTMCode():h}<!-- END Google Tag Manager -->{end:} {displayCommentedData(getCommonJSData()):s} <list name="body" /> {** * Please note that any custom list child of 'body' will NOT have its CSS/JS resources loaded because the resources block is being 'body' child itself. Use 'layout.main' or 'layout.footer' instead. *} </body> </html>

This produces output on the frontend that looks like this (I've simplified it for clarity), where the #slidebar nav element is still above the GTM code:
Code:
<body> <nav id="slidebar">…</nav> <!-- Google Tag Manager --> <noscript>…</noscript> <script>…</script> <!-- END Google Tag Manager --> <script type="text/x-cart-data">…</script> <div id="mm-0" class="mm-page mm-slideout">…</div> <script type="text/javascript" src="http://mysite.com/cached.js?43452345"></script> <div id="mm-blocker" class="mm-slideout"></div> </body>

I greatly prefer NOT having to customize body.tpl, because then my module is dependent on my custom theme. Any suggestions? Looking forward to any ideas

EDIT:
In the second code example above I noticed that when viewing page source (as opposed to looking at the markup in Dev Tools), the GTM script is being placed above the <nav id="slidebar"> element, so I assume that #slidebar is being moved by some JS at runtime. So when decorating body.tpl, I get the placement I want, but then my custom module is theme-dependent, which is not ideal. How do I achieve the same script placement in my module without making it theme-dependent?
__________________
X-Cart Version: 5.2
Reply With Quote
  #3  
Old 03-28-2017, 01:48 PM
 
plumstripe plumstripe is offline
 

Member
  
Join Date: Sep 2016
Posts: 11
 

Default Re: Include GTM Script immediately after <body> tag?

HI Paul, Did you ever get any help with this, or work it out yourself? I'm trying to do the same thing, (though with x-cart 5.3 and even less documentation). It seems crazy that it's so hard to put GTM code in x-cart. If you have any insights please let me know. Thanks
__________________
5.3.1.3
Reply With Quote
  #4  
Old 04-04-2017, 12:23 AM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default Re: Include GTM Script immediately after <body> tag?

You can use the attached custom module for injecting Google Tag Manager code's into all your X-Cart 5.3.x website pages.
Attached Files
File Type: tar BitsAround-GoogleTagManager-v5_3_0.tar (25.0 KB, 298 views)
__________________
Alex Solovev,
Qualiteam

---

User manual Video tutorials X-Cart FAQ

You are welcome to press "Thanks" button
if you find this post useful

Click here to learn how to apply patches

X-Cart Extensions
Reply With Quote
  #5  
Old 04-04-2017, 05:49 AM
 
anandat anandat is offline
 

X-Adept
  
Join Date: Jan 2004
Posts: 914
 

Default Re: Include GTM Script immediately after <body> tag?

Quote:
Originally Posted by qualiteam
You can use the attached custom module for injecting Google Tag Manager code's into all your X-Cart 5.3.x website pages.
Hi,
Does similar code available for x-cart classic also ?
__________________
X-Cart: 4.7.7 LIVE
Skin:Ultra by xcartmods.co.uk
X-cart Modules: | ACR, Rich Google Search, Customer Testimonials | Cloud Search, | Websitecm: CDSEO (2.1.9)
---------------
Server: Linux
php: 5.3
mysql: 5.0.89
----------------
Reply With Quote
  #6  
Old 04-04-2017, 11:34 PM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default Re: Include GTM Script immediately after <body> tag?

As far as I know there is no ready-made module for X-Cart 4 and you will have to edit core files manually.
I can't remember if anyone shared their tweaks for X-Cart 4 in these forums (at least I can't find one at the moment).
__________________
Alex Solovev,
Qualiteam

---

User manual Video tutorials X-Cart FAQ

You are welcome to press "Thanks" button
if you find this post useful

Click here to learn how to apply patches

X-Cart Extensions
Reply With Quote
  #7  
Old 06-15-2017, 01:33 PM
 
plumstripe plumstripe is offline
 

Member
  
Join Date: Sep 2016
Posts: 11
 

Default Re: Include GTM Script immediately after <body> tag?

Quote:
Originally Posted by qualiteam
You can use the attached custom module for injecting Google Tag Manager code's into all your X-Cart 5.3.x website pages.
How do I implement this custom module? Do I simply put these files in the correct locations, upload them to the website and rebuild cache to incorporate them?
Or is there some other way?
Please explain, thanks!
__________________
5.3.1.3
Reply With Quote
  #8  
Old 06-16-2017, 02:08 AM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default Re: Include GTM Script immediately after <body> tag?

Please use the "Upload add-on" button on the "Modules" back-end page.
__________________
Alex Solovev,
Qualiteam

---

User manual Video tutorials X-Cart FAQ

You are welcome to press "Thanks" button
if you find this post useful

Click here to learn how to apply patches

X-Cart Extensions
Reply With Quote
  #9  
Old 01-23-2018, 09:53 AM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default Re: Include GTM Script immediately after <body> tag?

There is a free ready-made module now:
https://market.x-cart.com/addons/google-tag-manager.html
__________________
Alex Solovev,
Qualiteam

---

User manual Video tutorials X-Cart FAQ

You are welcome to press "Thanks" button
if you find this post useful

Click here to learn how to apply patches

X-Cart Extensions
Reply With Quote
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 10:17 PM.

   

 
X-Cart forums © 2001-2020