External Webgains Knowledge Hub
Breadcrumbs

Shopify Container for Publisher Scripts

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


Within Shopify, please create a new Customer Events pixel (Settings → Customer Events → Add custom pixel).

Copy and paste the code source below, and replace the XXXXXXXXX with the ContainerID your Integration manager has provided to you. 

var wbgNeoryId = "XXXXXXXXX";

analytics.subscribe("page_viewed", event => { 
	 
	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.0 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) => {
	
	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.0 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 => { 
 	window.ntmData=window.ntmData||[];
	window.ntmData.push({
		gdpr: "-1", /* 0 if GDPR does not apply, 1 if GDPR applies */
		gdprConsent: "", /* IAB TCF 2.0 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 => { 
	window.ntmData=window.ntmData||[];
	window.ntmData.push({
		gdpr: "-1", /* 0 if GDPR does not apply, 1 if GDPR applies */
		gdprConsent: "", /* IAB TCF 2.0 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 => { 
	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 + "]";
	var uniqueDataLayerName = "ntmData"+Math.floor(Math.random()*10e12);
	window[uniqueDataLayerName] = [];
	window[uniqueDataLayerName].push({
		gdpr: "-1", 
		gdprConsent: "", /* IAB TCF 2.0 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_started", event => { 
 	var docLocation =  event.context.document.location;	
	
	var checkout = event.data.checkout;
	var products = 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].finalLinePrice.amount + "', name: '" + products[i].title + "' }";
	}
	wgProductList = wgProductList + "]";

	var uniqueDataLayerName = "ntmData"+Math.floor(Math.random()*10e12);
	window[uniqueDataLayerName] = [];
	window[uniqueDataLayerName].push({
		gdpr: "-1", 
		gdprConsent: "", /* IAB TCF 2.0 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 => { 

	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 + "]";
	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.0 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);
});

Save the pixel, and set the setting as below:

image-20260128-163054.png

Your Integration Manager will support with testing.