External Webgains Knowledge Hub
Breadcrumbs

Shopify Container for Publisher Scripts

Supporting guide for Webgains Container for Publisher Scripts - Integration Guide for Shopify implementation.

var wbgNeoryId = "XXXXXXXXX";
analytics.subscribe("page_viewed", event => { 
	console.log('page_viewed');
	console.log("context pathname: ",JSON.stringify(event.context.document.location.pathname));
 
	if (event.context.document.location.pathname == "/"){

		window.ntmData=window.ntmData||[];
		window.ntmData.push({
			gdpr: "-1", /* 0 if GDPR does not apply, 1 if GDPR applies */
			gdprConsent: "", /* IAB TCF 2.2 consent string */
			pageType:"homepage"
		});

		(function(n,e,o,r,y){n[r]=n[r]||[];n[r].push({'event':'ntmInit','t':new Date().getTime()});var f=e.getElementsByTagName(o)[0],s=e.createElement(o),d=r!='ntmData'?'&ntmData='+r:'';s.async=true;s.src='http'+(document.location.protocol=='https:'?'s':'')+'://tm.container.webgains.link/tm/a/container/init/'+y+'.js?'+d+'&rnd='+Math.floor(Math.random()*100000000);f.parentNode.insertBefore(s,f);})(window,document,'script','ntmData', wbgNeoryId);
	}
});


analytics.subscribe("search_submitted", (event) => {
	console.log(JSON.stringify(event.data), 0, null);
  
   
	var products = event.data.searchResult.productVariants;
	var wgProductList = "[";
	for (i=0; i < products.length; i++) {
		if (i > 0){
			wgProductList = wgProductList + ",";
		}
		wgProductList = wgProductList + "{id: '" + products[i].id + "' }";
	}
	wgProductList = wgProductList + "]";
    
	window.ntmData=window.ntmData||[];
	window.ntmData.push({
		gdpr: "-1", /* 0 if GDPR does not apply, 1 if GDPR applies */
		gdprConsent: "", /* IAB TCF 2.2 consent string */
		pageType:"searchresult",
		searchTerm: event.data.searchResult.query,
		products: wgProductList
	});
	(function(n,e,o,r,y){n[r]=n[r]||[];n[r].push({'event':'ntmInit','t':new Date().getTime()});var f=e.getElementsByTagName(o)[0],s=e.createElement(o),d=r!='ntmData'?'&ntmData='+r:'';s.async=true;s.src='http'+(document.location.protocol=='https:'?'s':'')+'://tm.container.webgains.link/tm/a/container/init/'+y+'.js?'+d+'&rnd='+Math.floor(Math.random()*100000000);f.parentNode.insertBefore(s,f);})(window,document,'script','ntmData',wbgNeoryId);
});


analytics.subscribe("collection_viewed", event => { 
    console.log(JSON.stringify(event.data), 0, null);

	window.ntmData=window.ntmData||[];
	window.ntmData.push({
		gdpr: "-1", /* 0 if GDPR does not apply, 1 if GDPR applies */
		gdprConsent: "", /* IAB TCF 2.2 consent string */
		pageType:"category",
		categoryId: event.data.collection.id,
        categoryName: event.data.collection.title
	});
  
(function(n,e,o,r,y){n[r]=n[r]||[];n[r].push({'event':'ntmInit','t':new Date().getTime()});var f=e.getElementsByTagName(o)[0],s=e.createElement(o),d=r!='ntmData'?'&ntmData='+r:'';s.async=true;s.src='http'+(document.location.protocol=='https:'?'s':'')+'://tm.container.webgains.link/tm/a/container/init/'+y+'.js?'+d+'&rnd='+Math.floor(Math.random()*100000000);f.parentNode.insertBefore(s,f);})(window,document,'script','ntmData',wbgNeoryId);
});

analytics.subscribe("product_viewed", event => { 
	console.log('Product Viewed');
	console.log(JSON.stringify(event.data), 0, null);

	window.ntmData=window.ntmData||[];
	window.ntmData.push({
		gdpr: "-1", /* 0 if GDPR does not apply, 1 if GDPR applies */
		gdprConsent: "", /* IAB TCF 2.2 consent string */
		pageType:"product",
		categoryId: event.data.productVariant.product.type,
		productId: event.data.productVariant.id,
		productPrice: event.data.productVariant.price.amount,
		productName:  event.data.productVariant.product.title

	});
	(function(n,e,o,r,y){
	n[r]=n[r]||[];
	n[r].push({'event':'ntmInit','t':new Date().getTime()});
	var f=e.getElementsByTagName(o)[0],s=e.createElement(o),d=r!='ntmData'?'&ntmData='+r:'';
	s.async=true;
	s.src='http'+(document.location.protocol=='https:'?'s':'')+'://tm.container.webgains.link/tm/a/container/init/'+y+'.js?'+d+'&rnd='+Math.floor(Math.random()*100000000);
	f.parentNode.insertBefore(s,f);
	})(window,document,'script','ntmData',wbgNeoryId);
});

analytics.subscribe("cart_viewed", event => { 
	console.log("cart_viewed");
	console.log(JSON.stringify(event.data), 0, null);
	
	var cart = event.data.cart;
	var products = cart.lines;
	var wgProductList = "[";
	for (i=0; i < products.length; i++) {
		if (i > 0){
			wgProductList = wgProductList + ",";
		}
		wgProductList = wgProductList + "{id: '" + products[i].merchandise.product.id + "', qty: '" + products[i].quantity + "', price: '" + products[i].merchandise.price.amount + "', name: '" + products[i].merchandise.product.title + "' }";
	}
	wgProductList = wgProductList + "]";
	console.log("wgProductList",wgProductList);
	var uniqueDataLayerName = "ntmData"+Math.floor(Math.random()*10e12);
	window[uniqueDataLayerName] = [];
	window[uniqueDataLayerName].push({
		gdpr: "-1", 
		gdprConsent: "", /* IAB TCF 2.2 consent string */
		pageType:"basket",
		products: wgProductList
	});
	(function(n,e,o,r,y){n[r]=n[r]||[];n[r].push({'event':'ntmInit','t':new Date().getTime()});var f=e.getElementsByTagName(o)[0],s=e.createElement(o),d=r!='ntmData'?'&ntmData='+r:'';s.async=true;s.src='http'+(document.location.protocol=='https:'?'s':'')+'://tm.container.webgains.link/tm/a/container/init/'+y+'.js?'+d+'&rnd='+Math.floor(Math.random()*100000000);f.parentNode.insertBefore(s,f);})(window,document,'script',uniqueDataLayerName,wbgNeoryId);
});



analytics.subscribe("checkout_completed", event => { 

	console.log("checkout_completed");
	console.log(JSON.stringify(event.data), 0, null);
	var products = event.data.checkout.lineItems;
	var wgProductList = "[";
	for (i=0; i < products.length; i++) {
	  if (i > 0){
		wgProductList = wgProductList + ",";
	  }
	  wgProductList = wgProductList + "{id: '" + products[i].id + "', qty: '" + products[i].quantity + "', price: '" + products[i].variant.price.amount + "', name: '" + products[i].title + "' }";
	}
	wgProductList = wgProductList + "]";
	console.log("wgProductList",wgProductList);

	var uniqueDataLayerName = "ntmData"+Math.floor(Math.random()*10e12);
	window[uniqueDataLayerName] = [];
	window[uniqueDataLayerName].push({
		gdpr: "-1", /* 0 if GDPR does not apply, 1 if GDPR applies */
		gdprConsent: "", /* IAB TCF 2.2 consent string */
		pageType:"order",
		transactionId: event.data.checkout.order.id,
		orderValue: event.data.checkout.subtotalPrice.amount,
		products: wgProductList
	});
	(function(n,e,o,r,y){n[r]=n[r]||[];n[r].push({'event':'ntmInit','t':new Date().getTime()});var f=e.getElementsByTagName(o)[0],s=e.createElement(o),d=r!='ntmData'?'&ntmData='+r:'';s.async=true;s.src='http'+(document.location.protocol=='https:'?'s':'')+'://tm.container.webgains.link/tm/a/container/init/'+y+'.js?'+d+'&rnd='+Math.floor(Math.random()*100000000);f.parentNode.insertBefore(s,f);})(window,document,'script',uniqueDataLayerName,wbgNeoryId);
});