Skip to content

Instantly share code, notes, and snippets.

@tessguefen
Created September 13, 2018 17:21
Show Gist options
  • Save tessguefen/720e453632c738f81d74e83e0f954829 to your computer and use it in GitHub Desktop.
Save tessguefen/720e453632c738f81d74e83e0f954829 to your computer and use it in GitHub Desktop.
GTM dataLayer - in progress
<mvt:comment>
|
| Google Tag Manager - Data Layer
|
</mvt:comment>
<mvt:comment> Page Type Assignment </mvt:comment>
<mvt:if expr="l.settings:page:code EQ 'SFNT'">
<mvt:assign name="l.settings:dataLayer:pageType" value="'home'" />
<mvt:elseif expr="( l.settings:page:code EQ 'SRCH' ) OR ( l.settings:page:code EQ 'SEARCH' )">
<mvt:assign name="l.settings:dataLayer:pageType" value="'searchresults'" />
<mvt:elseif expr="g.Screen EQ 'CTGY'">
<mvt:assign name="l.settings:dataLayer:pageType" value="'category'" />
<mvt:elseif expr="g.Screen EQ 'PROD'">
<mvt:assign name="l.settings:dataLayer:pageType" value="'product'" />
<mvt:elseif expr="l.settings:page:code CIN 'BASK,ORDL,OCST,OUS1,OUSM,OSEL,OPAY'">
<mvt:assign name="l.settings:dataLayer:pageType" value="'cart'" />
<mvt:elseif expr="l.settings:page:code EQ 'INVC'">
<mvt:assign name="l.settings:dataLayer:pageType" value="'purchase'" />
<mvt:else>
<mvt:assign name="l.settings:dataLayer:pageType" value="'other'" />
</mvt:if>
<mvt:comment> Page/ Category Name Assignment </mvt:comment>
<mvt:assign name="l.settings:dataLayer:categoryName" value="l.settings:category:name" />
<mvt:assign name="l.settings:dataLayer:pageName" value="l.settings:page:name" />
<mvt:comment> Page View URL Assignment </mvt:comment>
<mvt:if expr="l.settings:page:code EQ 'BASK'">
<mvt:assign name="l.settings:datalayer:checkout_step" value="1"/>
<mvt:elseif expr="l.settings:page:code EQ 'ORDL'">
<mvt:assign name="l.settings:datalayer:checkout_step" value="1"/>
<mvt:elseif expr="l.settings:page:code EQ 'ACAD' AND g.order EQ 1">
<mvt:assign name="l.settings:datalayer:checkout_step" value="3"/>
<mvt:elseif expr="l.settings:page:code EQ 'OCST'">
<mvt:if expr="l.settings:upsaddr:invalid">
<mvt:assign name="l.settings:datalayer:checkout_step" value="4"/>
<mvt:else>
<mvt:assign name="l.settings:datalayer:checkout_step" value="2"/>
</mvt:if>
<mvt:elseif expr="l.settings:page:code EQ 'OSEL'">
<mvt:assign name="l.settings:datalayer:checkout_step" value="5"/>
<mvt:elseif expr="l.settings:page:code EQ 'OPAY'">
<mvt:assign name="l.settings:datalayer:checkout_step" value="6"/>
<mvt:elseif expr="l.settings:page:code EQ 'INVC'">
<mvt:comment> Setup custom Variables for later </mvt:comment>
<mvt:foreach iterator="charge" array="order:charges">
<mvt:if expr="l.settings:charge:type EQ 'TAX'">
<mvt:assign name="l.settings:datalayer:order:tax" value="l.settings:charge:disp_amt" />
<mvt:elseif expr="l.settings:charge:type EQ 'SHIPPING'">
<mvt:assign name="l.settings:datalayer:order:shipping" value="l.settings:charge:disp_amt" />
</mvt:if>
</mvt:foreach>
<mvt:assign name="l.settings:datalayer:order:sub_total" value="l.settings:order:total - ( l.settings:datalayer:order:shipping + l.settings:datalayer:order:tax )"/>
</mvt:if>
<mvt:if expr="( g.Basket:cust_id GT 0 ) AND ( NOT ISNULL g.Customer:bill_email )">
<mvt:assign name="l.settings:datalayer:userEmail" value="g.customer:bill_email" />
<mvt:else>
<mvt:assign name="l.settings:datalayer:userEmail" value="NULL" />
</mvt:if>
<script>
var dataLayer = dataLayer || [];
dataLayer.push({
"pageCode": "&mvtj:page:code;",
"pageType": "&mvtj:datalayer:pageType;",
"pageName": "&mvtj:datalayer:pageName;",
"userId": "&mvtj:global:basket:cust_id;",
"userEmail": "&mvtj:datalayer:userEmail;",
"basketId": "&mvtj:global:basket:basket_id;",
"categoryCode": "&mvtj:global:Category_Code;",
"categoryName": "&mvtj:datalayer:categoryName;",
"productCode": "&mvtj:global:Product_Code;",
"productCodeAlt": "&mvtj:product:code;",
"checkoutStep": "&mvtj:datalayer:checkout_step;",
<mvt:if expr="( g.Screen EQ 'CTGY' ) AND ( l.settings:category_listing:products_on_page_count GT 0 )">
'event': 'impressions',
'ecommerce': {
'currencyCode': 'USD',
'impressions': [
<mvt:foreach iterator="product" array="category_listing:products">
<mvt:if expr="POS1 GT 1">, </mvt:if>
<mvt:comment>
Load in Category Path
</mvt:comment>
<mvt:if expr="NOT l.settings:product:category_path">
<mvt:if expr="l.settings:product:cancat_id GT 0">
<mvt:do file="g.Module_Library_DB" name="l.success" value="Category_Load_ID( l.settings:product:cancat_id, l.settings:product:category )" />
<mvt:else>
<mvt:do file="g.Module_Library_DB" name="l.success" value="CategoryList_Load_Offset_Product_Assigned( l.settings:product:id, 0, 1, 'active', 1, l.next, l.settings:product:_categories )" />
<mvt:assign name="l.settings:product:category" value="l.settings:product:_categories[1]" />
</mvt:if>
<mvt:assign name="l.settings:product:category_path" value="''" />
<mvt:do file="g.Module_Root $ '/modules/component/cmp-cssui-breadcrumbs.mvc'" name="l.settings:product:category_parents_count" value="CategoryList_Load_PathToRoot( l.settings:product:category, l.settings:product:category_parents )" />
<mvt:if expr="l.settings:product:category_parents_count GT 0">
<mvt:foreach iterator="parent_cat" array="product:category_parents">
<mvt:if expr="l.settings:product:category_path">
<mvt:assign name="l.settings:product:category_path" value="'/' $ l.settings:product:category_path" />
</mvt:if>
<mvt:assign name="l.settings:product:category_path" value="l.settings:parent_cat:name $ l.settings:product:category_path" />
</mvt:foreach>
<mvt:else>
<mvt:assign name="l.settings:product:category_path" value="l.settings:product:category:name" />
</mvt:if>
</mvt:if>
<mvt:assign name="l.multiplier" value="0" />
<mvt:if expr="g.CatListingOffset GT 0">
<mvt:assign name="l.multiplier" value="int( g.CatListingOffset ) * int( g.Per_Page )" />
</mvt:if>
{
'name': '&mvtj:product:name;',
'id': '&mvtj:product:id;',
'brand': '&mvtj:product:customfield_values:customfields:brand;',
'category': '&mvtj:product:category_path;',
'list': '&mvtj:category:customfield_values:customfields:ga_category_list;',
'position': <mvt:eval expr="POS1 + l.multiplier" />
}
</mvt:foreach>
]
}
<mvt:elseif expr="( g.Screen EQ 'SRCH' ) AND ( l.settings:search_results:products_on_page_count GT 0 )">
'event': 'impressions',
'ecommerce': {
'currencyCode': 'USD',
'impressions': [
<mvt:foreach iterator="product" array="search_results:products">
<mvt:if expr="POS1 GT 1">, </mvt:if>
<mvt:comment>
Load in Category Path
</mvt:comment>
<mvt:if expr="NOT l.settings:product:category_path">
<mvt:if expr="l.settings:product:cancat_id GT 0">
<mvt:do file="g.Module_Library_DB" name="l.success" value="Category_Load_ID( l.settings:product:cancat_id, l.settings:product:category )" />
<mvt:else>
<mvt:do file="g.Module_Library_DB" name="l.success" value="CategoryList_Load_Offset_Product_Assigned( l.settings:product:id, 0, 1, 'active', 1, l.next, l.settings:product:_categories )" />
<mvt:assign name="l.settings:product:category" value="l.settings:product:_categories[1]" />
</mvt:if>
<mvt:assign name="l.settings:product:category_path" value="''" />
<mvt:do file="g.Module_Root $ '/modules/component/cmp-cssui-breadcrumbs.mvc'" name="l.settings:product:category_parents_count" value="CategoryList_Load_PathToRoot( l.settings:product:category, l.settings:product:category_parents )" />
<mvt:if expr="l.settings:product:category_parents_count GT 0">
<mvt:foreach iterator="parent_cat" array="product:category_parents">
<mvt:if expr="l.settings:product:category_path">
<mvt:assign name="l.settings:product:category_path" value="'/' $ l.settings:product:category_path" />
</mvt:if>
<mvt:assign name="l.settings:product:category_path" value="l.settings:parent_cat:name $ l.settings:product:category_path" />
</mvt:foreach>
<mvt:else>
<mvt:assign name="l.settings:product:category_path" value="l.settings:product:category:name" />
</mvt:if>
</mvt:if>
<mvt:assign name="l.multiplier" value="0" />
<mvt:if expr="g.CatListingOffset GT 0">
<mvt:assign name="l.multiplier" value="int( g.CatListingOffset ) * int( g.Per_Page )" />
</mvt:if>
{
'name': '&mvtj:product:name;',
'id': '&mvtj:product:id;',
'brand': '&mvtj:product:customfield_values:customfields:brand;',
'category': '&mvtj:product:category_path;',
'list': '&mvtj:category:customfield_values:customfields:ga_category_list;',
'position': <mvt:eval expr="POS1 + l.multiplier" />
}
</mvt:foreach>
]
}
<mvt:elseif expr="g.Screen EQ 'PROD'">
<mvt:if expr="NOT l.settings:product:category_path">
<mvt:if expr="l.settings:product:cancat_id GT 0">
<mvt:do file="g.Module_Library_DB" name="l.success" value="Category_Load_ID( l.settings:product:cancat_id, l.settings:product:category )" />
<mvt:else>
<mvt:do file="g.Module_Library_DB" name="l.success" value="CategoryList_Load_Offset_Product_Assigned( l.settings:product:id, 0, 1, 'active', 1, l.next, l.settings:product:_categories )" />
<mvt:assign name="l.settings:product:category" value="l.settings:product:_categories[1]" />
</mvt:if>
<mvt:assign name="l.settings:product:category_path" value="''" />
<mvt:do file="g.Module_Root $ '/modules/component/cmp-cssui-breadcrumbs.mvc'" name="l.settings:product:category_parents_count" value="CategoryList_Load_PathToRoot( l.settings:product:category, l.settings:product:category_parents )" />
<mvt:if expr="l.settings:product:category_parents_count GT 0">
<mvt:foreach iterator="parent_cat" array="product:category_parents">
<mvt:if expr="l.settings:product:category_path">
<mvt:assign name="l.settings:product:category_path" value="'/' $ l.settings:product:category_path" />
</mvt:if>
<mvt:assign name="l.settings:product:category_path" value="l.settings:parent_cat:name $ l.settings:product:category_path" />
</mvt:foreach>
<mvt:else>
<mvt:assign name="l.settings:product:category_path" value="l.settings:product:category:name" />
</mvt:if>
</mvt:if>
"ecommerce": {
"detail": {
"products": [{
"name": "&mvtj:product:name;",
"id": "&mvte:product:code;",
"price": "&mvte:product:price;",
"brand": "&mvtj:product:customfield_values:customfields:brand;",
"sku": "&mvtj:product:sku;",
"category": "&mvtj:product:category_path;"<mvt:comment>,
"variant": "&mvtj:attribute:options[1]:code;"</mvt:comment>
}]
}
}
<mvt:elseif expr="l.settings:page:code EQ 'INVC'">
"ecommerce": {
"purchase": {
"actionField": {
"id": "&mvt:order:id;",
"affiliation": "&mvtj:store:name;",
"revenue": "&mvtj:order:total;",
"tax":"&mvtj:datalayer:order:tax;",
"shipping": "&mvtj:datalayer:order:shipping;"
<mvt:foreach iterator="coupon" array="order:coupons">
<mvt:if expr="POS1 GT 1">
<mvt:foreachstop />
</mvt:if>
,"coupon": "&mvtj:coupon:code;"
</mvt:foreach>
},
"products": [
<mvt:foreach iterator="group" array="order:groups">
<mvt:if expr="l.settings:group:product:id EQ 0">
<mvt:do file="g.Module_Library_DB" name="l.success" value="Product_Load_ID( l.settings:group:product_id, l.settings:group:product )" />
</mvt:if>
<mvt:if expr="NOT l.settings:group:category_path">
<mvt:if expr="l.settings:group:product:cancat_id GT 0">
<mvt:do file="g.Module_Library_DB" name="l.success" value="Category_Load_ID( l.settings:group:product:cancat_id, l.settings:group:category )" />
<mvt:else>
<mvt:do file="g.Module_Library_DB" name="l.success" value="CategoryList_Load_Offset_Product_Assigned( l.settings:group:product_id, 0, 1, 'active', 1, l.next, l.settings:group:_categories )" />
<mvt:assign name="l.settings:group:category" value="l.settings:group:_categories[1]" />
</mvt:if>
<mvt:assign name="l.settings:group:category_path" value="''" />
<mvt:do file="g.Module_Root $ '/modules/component/cmp-cssui-breadcrumbs.mvc'" name="l.settings:group:category_parents_count" value="CategoryList_Load_PathToRoot( l.settings:group:category, l.settings:group:category_parents )" />
<mvt:if expr="l.settings:group:category_parents_count GT 0">
<mvt:foreach iterator="parent_cat" array="product:category_parents">
<mvt:if expr="l.settings:group:category_path">
<mvt:assign name="l.settings:group:category_path" value="'/' $ l.settings:group:category_path" />
</mvt:if>
<mvt:assign name="l.settings:group:category_path" value="l.settings:parent_cat:name $ l.settings:group:category_path" />
</mvt:foreach>
<mvt:else>
<mvt:assign name="l.settings:group:category_path" value="l.settings:group:category:name" />
</mvt:if>
</mvt:if>
<mvt:if expr="POS1 GT 1">,</mvt:if>
<mvt:item name="customfields" param="Read_Product_ID( l.settings:group:product_id, 'Brand', l.settings:group:brand )" />
{
"name": "&mvtj:group:name;",
"id": "&mvtj:group:code;",
"price": "&mvtj:group:price;",
"brand": "&mvtj:group:brand;",
"sku": "&mvtj:group:code;",
"category": "&mvtj:group:category_path;",
"variant": "<mvt:foreach iterator="option" array="group:options"><mvt:if expr="POS2 GT 1"> </mvt:if>&mvtj:option:opt_prompt;</mvt:foreach>",
"quantity": &mvtj:group:quantity;
<mvt:if expr="l.settings:group:discounts[1]:descrip">,"coupon": "&mvtj:group:discounts[1]:descrip;"</mvt:if>
}
</mvt:foreach>
]
}
}
<mvt:elseif expr="l.settings:datalayer:checkout_step">
"event": "checkout",
"ecommerce": {
"checkout": {
"actionField": {
"step": &mvtj:datalayer:checkout_step;
},
"products": [
<mvt:foreach iterator="item" array="global_minibasket:items">
<mvt:if expr="NOT l.settings:item:product:id">
<mvt:do file="g.Module_Library_DB" name="l.success" value="Product_Load_Code( l.settings:item:code, l.settings:item:product )" />
</mvt:if>
<mvt:if expr="l.settings:item:product:cancat_id GT 0">
<mvt:do file="g.Module_Library_DB" name="l.success" value="Category_Load_ID( l.settings:item:product:cancat_id, l.settings:item:product:category )" />
<mvt:else>
<mvt:do file="g.Module_Library_DB" name="l.success" value="CategoryList_Load_Offset_Product_Assigned( l.settings:item:product:id, 0, 1, 'active', 1, l.next, l.settings:item:product:_categories )" />
<mvt:assign name="l.settings:item:product:category" value="l.settings:item:product:_categories[1]" />
</mvt:if>
<mvt:assign name="l.settings:item:category_path" value="''" />
<mvt:assign name="l.product_cat" value="l.settings:item:product:category" />
<mvt:do file="g.Module_Root $ '/modules/component/cmp-cssui-breadcrumbs.mvc'" name="l.settings:item:product:category_parents_count" value="CategoryList_Load_PathToRoot( l.product_cat, l.settings:item:product:category_parents )" />
<mvt:if expr="l.settings:item:product:category_parents_count GT 0">
<mvt:foreach iterator="parent_cat" array="item:product:category_parents">
<mvt:if expr="l.settings:item:category_path">
<mvt:assign name="l.settings:item:category_path" value="'/' $ l.settings:item:category_path" />
</mvt:if>
<mvt:assign name="l.settings:item:category_path" value="l.settings:parent_cat:name $ l.settings:item:category_path" />
</mvt:foreach>
<mvt:else>
<mvt:assign name="l.settings:item:category_path" value="l.settings:item:product:category:name" />
</mvt:if>
<mvt:item name="customfields" param="Read_Product_ID( l.settings:item:product_id, 'Brand', l.settings:item:brand )" />
{
"name": "&mvtj:item:name;",
"id": "&mvtj:item:product:code;",
"price": "&mvtj:item:price;",
"brand": "&mvtj:item:brand;",
"sku": "&mvtj:item:code;",
"category": "&mvtj:item:category_path;",
"variant": "<mvt:foreach iterator="option" array="item:options"><mvt:if expr="POS2 GT 1"> </mvt:if>&mvtj:option:opt_prompt;</mvt:foreach>",
"quantity": &mvtj:item:quantity;
},
</mvt:foreach>
]
}
}
</mvt:if>
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment