steveosoule
2/19/2016 - 11:06 PM

Miva - Google Tag Manager Data Layer Sample

Miva - Google Tag Manager Data Layer Sample

<!-- Google Tag Manager - Data Layer -->
	<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="l.settings:page:code EQ 'CTGY'">
			<mvt:assign name="l.settings:dataLayer:pageType" value="'category'" />
		<mvt:elseif expr="l.settings:page:code 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 'CTGY'">
			<mvt:assign name="l.settings:datalayer:pageview" value="'category/' $ l.settings:category:code $ '.html'"/>
		<mvt:elseif expr="l.settings:page:code EQ 'PROD'">
			<mvt:assign name="l.settings:datalayer:pageview" value="'product/' $ l.settings:product:code $ '.html'"/>
		<mvt:elseif expr="l.settings:page:code EQ 'SFNT'">
			<mvt:assign name="l.settings:datalayer:pageview" value="'SFNT.html'"/>
		<mvt:elseif expr="l.settings:page:code EQ 'BASK'">
			<mvt:assign name="l.settings:datalayer:pageview" value="'checkout/basket.html'"/>
			<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:pageview" value="'checkout/login.html'"/>
			<mvt:assign name="l.settings:datalayer:checkout_step" value="2"/>
		<mvt:elseif expr="l.settings:page:code EQ 'OCST'">
			<mvt:if expr="l.settings:upsaddr:invalid">
				<mvt:assign name="l.settings:datalayer:pageview" value="'checkout/address-validation.html'"/>
				<mvt:assign name="l.settings:datalayer:checkout_step" value="4"/>
			<mvt:else>
				<mvt:assign name="l.settings:datalayer:pageview" value="'checkout/address.html'"/>
				<mvt:assign name="l.settings:datalayer:checkout_step" value="3"/>
			</mvt:if>
		<mvt:elseif expr="l.settings:page:code EQ 'OSEL'">
			<mvt:assign name="l.settings:datalayer:pageview" value="'checkout/shipping.html'"/>
			<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:pageview" value="'checkout/payment.html'"/>
			<mvt:assign name="l.settings:datalayer:checkout_step" value="6"/>
		<mvt:elseif expr="l.settings:page:code EQ 'INVC'">
			<mvt:assign name="l.settings:datalayer:pageview" value="'checkout/invoice.html'"/>
		<mvt:elseif expr="l.settings:page:code EQ 'WP-FOOTER'">
			<mvt:assign name="l.settings:datalayer:pageview" value="''"/>
		<mvt:else>
			<mvt:assign name="l.settings:datalayer:pageview" value="l.settings:ga_tracking:url_override_unencoded"/>
		</mvt:if>
		<mvt:if expr="g.basket:cust_id">
			<mvt:if expr="g.customer:bill_email">
				<mvt:assign name="l.settings:datalayer:userEmail" value="g.customer:bill_email" />
			</mvt:if>
		<mvt:comment>
		<mvt:elseif expr="g.basket:bill_email">
			<mvt:assign name="l.settings:datalayer:userEmail" value="g.basket:bill_email" />
		</mvt:comment>
		<mvt:else>
			<mvt:assign name="l.settings:datalayer:userEmail" value="''" />
		</mvt:if>

	<script>
		dataLayer = [{
			"pageCode": "&mvt:page:code;",
			"pageType": "&mvt:datalayer:pageType;",
			"pageName": "&mvt:datalayer:pageName;",
			"userId": "&mvte:global:basket:cust_id;",
			"userEmail": "&mvte:datalayer:userEmail;",
			"basketId": "&mvte:global:basket:basket_id;",
			"pageView": "&mvt:datalayer:pageview;",
			"categoryCode": "&mvte:global:Category_Code;",
			"categoryName": "&mvte:datalayer:categoryName;",
			"productCode": "&mvte:global:Product_Code;",
			"productCodeAlt": "&mvte:product:code;",
			"checkoutStep": "&mvt:datalayer:checkout_step;",
			<mvt:if expr="l.settings:page:code EQ 'INVC'">
				<mvt:assign name="l.settings:ga_transaction:total_sub" value="l.settings:ga_transaction:order_total - (l.settings:ga_transaction:total_ship + l.settings:ga_transaction:total_tax)"/>
				"transactionId": "&mvt:ga_tracking:order_id;",
				"transactionAffiliation": "&mvt:ga_tracking:store_name;",
				"transactionTotal": "&mvt:ga_tracking:order_total;",
				"transactionSubTotal": "&mvt:ga_transaction:total_sub;",
				"transactionTax": "&mvt:ga_transaction:total_tax;",
				"transactionShipping": "&mvt:ga_transaction:total_ship;",
				"transactionProducts": [
					<mvt:foreach iterator="item" array="ga_tracking:orderitems">
					<mvt:item name="customfields" param="Read_Product_Code( l.settings:item:code, 'Brand', l.settings:customfieldstrans )" />
						{
							"sku": "&mvte:item:code;",
							"name": "&mvtj:item:name;",
							"category": "",
							"brand": "&mvt:customfieldstrans;",
							"price": "&mvte:item:price;",
							"quantity": "&mvte:item:quantity;"
						},
					</mvt:foreach>
				]
			</mvt:if>
		}];

		<mvt:if expr="l.settings:page:code EQ 'PROD'">
			dataLayer.push({
				"ecommerce": {
					"detail": {
						"products": [{
							"name": "&mvte:product:name;",
							"id": "&mvte:product:code;",
							"price": "&mvte:product:price;",
							"brand": "&mvte:product:brand:name;",
							"sku": "&mvte:item:code;",
							"category": "",
							"variant": ""
						}]
					}
				}
			});
		</mvt:if>
		<mvt:if expr="l.settings:page:code EQ 'INVC'">
			dataLayer.push({
				"ecommerce": {
					"purchase": {
						"actionField": {
							"id": "&mvt:order:id;",
							"brand": "&mvte:group:customfield_values:customfields:brand;",
							"affiliation": "&mvt:ga_tracking:store_name;",
							"revenue": "&mvt:order:total;",
							"tax":"&mvt:ga_transaction:total_tax;",
							"shipping": "&mvt:ga_transaction:total_ship;",
						},
						"products": [
							<mvt:foreach iterator="item" array="ga_tracking:orderitems">
							<mvt:item name="customfields" param="Read_Product_Code( l.settings:item:code, 'Brand', l.settings:customfields )" />
								{                            // List of productFieldObjects.
									"name": "&mvtj:item:name;",     // Name or ID is required.
									"id": "&mvte:item:code;",
									"price": "&mvte:item:price;",
									"brand": "&mvt:customfields;",
									"sku": "&mvte:item:code;",
									"category": "",
									"variant": "",
									"quantity": &mvte:item:quantity;,
									"coupon": ""
								},
							</mvt:foreach>
						]
					}
				}
			});
		<mvt:elseif expr="l.settings:datalayer:checkout_step">
			dataLayer.push({
				"event": "checkout",
				"ecommerce": {
					"checkout": {
						"actionField": {
							"step": &mvt:datalayer:checkout_step;
						},
						"products": [
							<mvt:foreach iterator="item" array="global_minibasket:items">
							<mvt:item name="customfields" param="Read_Product_Code( l.settings:item:code, 'Brand', l.settings:customfieldsmini )" />
								{
									"name": "&mvtj:item:name;",
									"id": "&mvte:item:product:code;",
									"price": "&mvte:item:price;",
									"brand": "&mvt:customfieldsmini;",
									"sku": "&mvte:item:code;",
									"category": "",
									"variant": "",
									"quantity": &mvte:item:quantity;
								},
							</mvt:foreach>
						]
					}
				}
			});
		</mvt:if>
	</script>
<!-- End Google Tag Manager - Data Layer -->

<!-- Google Tag Manager -->
	<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-KNSJHH" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
	<script>(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='//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer','GTM-KNSJHH');</script>
<!-- End: Google Tag Manager -->