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

Setting up Data Layer for Ecom tracking in GA4

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions
 
Thread Tools Search this Thread
  #1  
Old 03-29-2022, 12:03 PM
 
keystone keystone is offline
 

X-Adept
  
Join Date: Jul 2006
Location: USA
Posts: 787
 

Default Setting up Data Layer for Ecom tracking in GA4

Hi, I've just installed the Google Tracking Manager code on my site and it seems to be connected and reading ok. I'd like to set up the Ecomm tracking. I've been following a tutorial on setting up the Data Layer required. I've kind of hacked together some code using existing Google Analytic code/variables from the built in x-cart module plus the example from the tutorial. Can someone take a look at what I've got to see if I'm even close to having what I need?

Code:
<script> window.dataLayer = window.dataLayer || []; dataLayer.push({ {foreach from=$orders item="order"} 'transactionId' : "{$order.order.orderid}", // order ID - required 'transactionAffiliation' : "{$partner|default:'Main stock'}", // affiliation or store name 'transactionTotal' : "{$order.order.total}", // total - required 'transactionShipping' : "{$order.order.shipping_cost}", // shipping 'transactionTax' : "{$order.order.tax}" // tax 'transactionProducts' : [{ {foreach from=$order.products item="product"} 'transactionId' : "{$order.order.orderid}", // order ID - required 'name' : "{$product.product|wm_remove|escape:javascript}{if $active_modules.Product_Options ne "" and $product.product_options_txt} ({$product.product_options_txt|replace:"\n":", "|wm_remove|escape:javascript}){/if}", // product name 'sku' : "{$product.productcode|wm_remove|escape:javascript}", // SKU/code - required 'category' : "{$product.category|default:'Unknown category'}", // category or variation 'price' : "{$product.price}", // unit price - required 'quantity' : "{$product.amount}" // quantity - required {/foreach} }] }); </script>

the original GA code in x-cart google anyalytics module ecommerce section has some additional lines like below but not sure if I need those (mainly the ldelim and rdelim) or the {literal} tags.
Code:
ga('ecommerce:addTransaction', {ldelim}
__________________
www.uscandleco.com - X-Cart Version 4.7.11 Gold Plus php7.3
mods:
reCaptcha
running on UNIX

www.keystonecandle.com X-Cart Gold Plus - Version 4.7.11 php7.2
mods:
reCaptcha
cdseo pro
running on UNIX
Reply With Quote
  #2  
Old 04-07-2022, 10:06 AM
 
PhilJ PhilJ is offline
 

X-Guru
  
Join Date: Nov 2002
Posts: 4,094
 

Default Re: Setting up Data Layer for Ecom tracking in GA4

Looks like you might be missing a closing {/foreach}
__________________
xcartmods.co.uk
Reply With Quote
  #3  
Old 04-08-2022, 10:29 AM
  BCSE's Avatar 
BCSE BCSE is online now
 

X-Guru
  
Join Date: Apr 2003
Location: Ohio - bcsengineering.com
Posts: 3,089
 

Default Re: Setting up Data Layer for Ecom tracking in GA4

yeah I agree on the missing end foreach
Should be like this:
Code:
<script> window.dataLayer = window.dataLayer || []; dataLayer.push({ {foreach from=$orders item="order"} 'transactionId' : "{$order.order.orderid}", // order ID - required 'transactionAffiliation' : "{$partner|default:'Main stock'}", // affiliation or store name 'transactionTotal' : "{$order.order.total}", // total - required 'transactionShipping' : "{$order.order.shipping_cost}", // shipping 'transactionTax' : "{$order.order.tax}" // tax 'transactionProducts' : [{ {foreach from=$order.products item="product"} 'transactionId' : "{$order.order.orderid}", // order ID - required 'name' : "{$product.product|wm_remove|escape:javascript}{if $active_modules.Product_Options ne "" and $product.product_options_txt} ({$product.product_options_txt|replace:"\n":", "|wm_remove|escape:javascript}){/if}", // product name 'sku' : "{$product.productcode|wm_remove|escape:javascript}", // SKU/code - required 'category' : "{$product.category|default:'Unknown category'}", // category or variation 'price' : "{$product.price}", // unit price - required 'quantity' : "{$product.amount}" // quantity - required {/foreach} }] {/foreach} }); </script>
__________________
Custom Development, Custom Coding and Pre-built modules for X-cart since 2002!

We support X-cart versions 3.x through 5.x!

Home of the famous Authorize.net DPM & CIM Modules, Reward Points Module, Point of Sale module, Speed Booster modules and more!


Over 200 X-cart Mods available & Thousands of Customizations Since 2002 - bcsengineering.com

Please E-Mail us for questions/support!
Reply With Quote
  #4  
Old 04-08-2022, 11:28 AM
 
keystone keystone is offline
 

X-Adept
  
Join Date: Jul 2006
Location: USA
Posts: 787
 

Default Re: Setting up Data Layer for Ecom tracking in GA4

Thanks Phil and Carrie, I haven't actually tried the code in my site yet. I need to get my development site back up to do some testing first.
__________________
www.uscandleco.com - X-Cart Version 4.7.11 Gold Plus php7.3
mods:
reCaptcha
running on UNIX

www.keystonecandle.com X-Cart Gold Plus - Version 4.7.11 php7.2
mods:
reCaptcha
cdseo pro
running on UNIX
Reply With Quote

The following user thanks keystone for this useful post:
BCSE (04-11-2022)
  #5  
Old 04-08-2022, 12:11 PM
 
keystone keystone is offline
 

X-Adept
  
Join Date: Jul 2006
Location: USA
Posts: 787
 

Default Re: Setting up Data Layer for Ecom tracking in GA4

Do either of you already have this set up on your sites (or do you have gtag) for google analytics set up? I have the OLD universal analytics on my site still using ga() instead of gtag() so a bit of a confusing to try and get GA4 set up as well.
__________________
www.uscandleco.com - X-Cart Version 4.7.11 Gold Plus php7.3
mods:
reCaptcha
running on UNIX

www.keystonecandle.com X-Cart Gold Plus - Version 4.7.11 php7.2
mods:
reCaptcha
cdseo pro
running on UNIX
Reply With Quote
  #6  
Old 04-08-2022, 12:35 PM
 
keystone keystone is offline
 

X-Adept
  
Join Date: Jul 2006
Location: USA
Posts: 787
 

Default Re: Setting up Data Layer for Ecom tracking in GA4

Here is what I'm up to so far...
Code:
<!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-#########"></script> {literal} <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); {if $config.Google_Analytics.ganalytics_e_commerce_analysis eq "Y" and $ga_track_commerce eq "Y" and $main eq "order_message" and $orders } gtag('event', 'purchase', { {foreach from=$orders item="order"} 'transactionId' : "{$order.order.orderid}", // order ID - required 'transactionAffiliation' : "{$partner|default:'Main stock'}", // affiliation or store name 'transactionTotal' : "{$order.order.total}", // total - required 'transactionShipping' : "{$order.order.shipping_cost}", // shipping 'transactionTax' : "{$order.order.tax}" // tax 'transactionProducts' : [{ {foreach from=$order.products item="product"} 'transactionId' : "{$order.order.orderid}", // order ID - required 'name' : "{$product.product|wm_remove|escape:javascript}{if $active_modules.Product_Options ne "" and $product.product_options_txt} ({$product.product_options_txt|replace:"\n":", "|wm_remove|escape:javascript}){/if}", // product name 'sku' : "{$product.productcode|wm_remove|escape:javascript}", // SKU/code - required 'category' : "{$product.category|default:'Unknown category'}", // category or variation 'price' : "{$product.price}", // unit price - required 'quantity' : "{$product.amount}" // quantity - required {/foreach} }] {/foreach} }); {/if} gtag('config', 'G-#######'); </script> {/literal}
__________________
www.uscandleco.com - X-Cart Version 4.7.11 Gold Plus php7.3
mods:
reCaptcha
running on UNIX

www.keystonecandle.com X-Cart Gold Plus - Version 4.7.11 php7.2
mods:
reCaptcha
cdseo pro
running on UNIX
Reply With Quote
  #7  
Old 04-11-2022, 10:06 AM
  BCSE's Avatar 
BCSE BCSE is online now
 

X-Guru
  
Join Date: Apr 2003
Location: Ohio - bcsengineering.com
Posts: 3,089
 

Default Re: Setting up Data Layer for Ecom tracking in GA4

I'll be working on this soon for a website. If you want to drop us an email I can let you know how to do it when I'm done. (I'm afraid I'll forget otherwise) Just ask Luke to put it in 'quotes' and I'll see it and can give you instructions on what to do. I may write a blog post on it when I figure it all out.

Thanks,

Carrie
__________________
Custom Development, Custom Coding and Pre-built modules for X-cart since 2002!

We support X-cart versions 3.x through 5.x!

Home of the famous Authorize.net DPM & CIM Modules, Reward Points Module, Point of Sale module, Speed Booster modules and more!


Over 200 X-cart Mods available & Thousands of Customizations Since 2002 - bcsengineering.com

Please E-Mail us for questions/support!
Reply With Quote

The following 2 users thank BCSE for this useful post:
dpcompany (04-11-2022), ITVV (04-11-2022)
  #8  
Old 04-11-2022, 10:45 AM
 
keystone keystone is offline
 

X-Adept
  
Join Date: Jul 2006
Location: USA
Posts: 787
 

Default Re: Setting up Data Layer for Ecom tracking in GA4

That would be great. I'll do that. Thanks a lot.
__________________
www.uscandleco.com - X-Cart Version 4.7.11 Gold Plus php7.3
mods:
reCaptcha
running on UNIX

www.keystonecandle.com X-Cart Gold Plus - Version 4.7.11 php7.2
mods:
reCaptcha
cdseo pro
running on UNIX
Reply With Quote

The following user thanks keystone for this useful post:
BCSE (04-11-2022)
Reply
   X-Cart forums > X-Cart 4 > Dev Questions



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not 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 07:56 PM.

   

 
X-Cart forums © 2001-2020