function debug(debugString){ var debug = true; if (debug) { console.log(debugString); } } function roundPrice(price){ this.round = function(n){ n = Math.pow(10,n); return function(price) { return Math.round(price*n)/n; } } var priceRounder = this.round(2); return priceRounder(price); } function Product(name,id){ this.name = name; this.id = id; this.brand = null; this.category = null; this.variant = null; this.price = null; this.quantity = null; this.coupon = null; this.position = null; this.list = null; this.toGoogleJson = function(){ var json = ""; if (this.name != null){ json += "'name':'" +this.name +"'"; } if (this.id != null){ if (json.length > 0) { json += ","; } json += "'id':'" +this.id +"'"; } if (this.brand != null) { json += ",'brand':'" +this.brand +"'"; } if (this.category != null) { json += ",'category':'" +this.category +"'"; } if (this.variant != null) { json += ",'variant':'" +this.variant +"'"; } if (this.price != null && this.price > 0) { json += ",'price':'" +roundPrice(this.price) +"'"; } if (this.quantity != null) { json += ",'quantity':'" +this.quantity +"'"; } if (this.coupon != null) { json += ",'coupon':'" +this.coupon +"'"; } if (this.list != null) { json += ",'list':'" +this.list +"'"; } if (this.position != null) { json += ",'position':'" +this.position +"'"; } return "{" + json + "}"; } } function ProductList(){ this.productList = []; this.addProduct = function(product){ this.productList.push(product); } this.length = function(){ return this.productList.length; } this.concat = function(productConcatList){ if (productConcatList.length() > 0){ this.productList = this.productList.concat(productConcatList.productList); } } this.getProduct = function(id){ return this.productList[id]; } this.toGoogleJson = function(){ var json = ""; for (var i=0; i < this.productList.length;i++){ if (json.length > 0) json += "," json += this.productList[i].toGoogleJson(); } return "["+ json + "]"; } } function CheckOutOption(step){ this.step = step; this.option = null; this.toGoogleJson = function(){ var json = ""; json += "'step':'" +this.step+ "'"; if (this.option != null) { json += "'option':'" +this.option + "'"; } return "{"+json+"}"; } } function CheckoutAction(id){ this.id = id; this.affiliation = null; this.revenue = null; this.tax = null; this.shipping = null; this.coupon = null; this.toGoogleJson = function(){ var json = ""; json += "'id':'" +this.id+ "'"; if (this.option != null) { json += "'option':'" +this.option + "'"; } if (this.affiliation != null) { json += "'affiliation':'" +this.affiliation + "'"; } if (this.revenue != null) { json += "'revenue':'" +roundPrice(this.revenue) + "'"; } if (this.tax != null) { json += "'tax':'" +roundPrice(this.tax) + "'"; } if (this.shipping != null) { json += "'shipping':'" +roundPrice(this.shipping) + "'"; } if (this.coupon != null) { json += "'coupon':'" +this.coupon + "'"; } return "{"+json+"}"; } } function pushAddProductToGoogle(product){ debug(product.toGoogleJson()); dataLayer.push({ 'event': 'addToCart', 'ecommerce': { 'currencyCode': 'GBP', 'add': { 'products': [product] } } }); } function pushProductClickToGoogle(product){ debug(product.toGoogleJson()); dataLayer.push({ 'event': 'productClick', 'ecommerce': { 'click': { 'products': [product] } } }); } function pushProductImpressionsToGoogle(productList){ debug(productList.toGoogleJson()); dataLayer.push({ 'ecommerce': { 'impressions': productList.productList } }); } function pushProductDetailViewToGoogle(product){ debug(product.toGoogleJson()); dataLayer.push({ 'ecommerce': { 'detail': { 'products': [product] } } }); } function pushRemoveFromCartToGoogle(product){ debug(product.toGoogleJson()); dataLayer.push({ 'event': 'removeFromCart', 'ecommerce': { 'remove': { 'products': [product] } } }); } function pushCheckoutEventToGoogle(checkOutOption, productList){ debug(checkOutOption.toGoogleJson()); debug(productList.toGoogleJson()); dataLayer.push({ 'event': 'checkout', 'ecommerce': { 'checkout': { 'actionField': checkOutOption, 'products': productList.productList } } }); } function pushCheckoutOptionToGoogle(checkOutOption){ debug(checkOutOption.toGoogleJson()); dataLayer.push({ 'event': 'checkoutOption', 'ecommerce': { 'checkout_option': { 'actionField': checkOutOption } } }); } function pushPurchaseToGoogle(checkoutAction, productList){ debug(checkoutAction.toGoogleJson()); debug(productList.toGoogleJson()); dataLayer.push({ 'ecommerce': { 'purchase': { 'actionField': checkoutAction, 'products': productList.productList } } }); } function pushEventToGoogle(eventName) { debug('event:' + eventName); dataLayer.push({'event':"'" + eventName + "'"}); } function pushDetailPage(){ var product = getPageProduct(); if (product != null){ pushProductDetailViewToGoogle(product); } } function productClickFromListing(productel){ var product = new Product($(productel).closest('.stockthumb').attr("g-data-name"),$(productel).closest('.stockthumb').attr("g-data-id")); product.price = $(productel).closest('.stockthumb').attr("g-data-price"); product.seq = $(productel).closest('.stockthumb').attr("g-data-seq"); pushProductClickToGoogle(product); } function pushAddFromListingPage(productel){ var product = new Product($(productel).closest('.stockthumb').attr("g-data-name"),$(productel).closest('.stockthumb').attr("g-data-id")); product.price = $(productel).closest('.stockthumb').attr("g-data-price"); product.quantity = $('#qtyPlusMinus'+$(productel).closest('.stockthumb').attr("g-data-seq"))[0].value; pushAddProductToGoogle(product); } function pushAddFromDetailPage(){ var product = getPageProduct(); if (product != null){ product.quantity = $('.productdetailwrap .inputitem_productqty')[0].value; pushAddProductToGoogle(product); } } function pushProductImpressions(){ var product = {}; var productList = new ProductList(); productList.concat(getStockListingProductList()); productList.concat(getStockAlsoBroughtProductList()); productList.concat(getLinkedProductList()); productList.concat(getTopSellerProductList()); productList.concat(getRecentlyViewedList()); pushProductImpressionsToGoogle(productList); } function pushCheckoutStep(){ var pageName = getPageName(); var checkoutStep = getCheckoutStep(); if (checkoutStep > 0){ var checkoutOption = new CheckOutOption(getCheckoutStep()); var products = getBasketProductList(); pushCheckoutEventToGoogle(checkoutOption, products); } } function pushPurchaseDetailsToGoogle(){ var products = getBasketProductList(); var details = getBasketDetails(); if (products != null && details != null){ pushPurchaseToGoogle(details,products); } } function pushBasketRemove(basketItemId){ var product = getBasketProduct(basketItemId); if (product != null){ pushRemoveFromCartToGoogle(product); } } function processBasketUpdateForGoogle(basketItemId){ var newQty = $('.basketline #qty'+ basketItemId)[0].value; var product = getBasketProduct(basketItemId); if (product != null){ if (product.quantity > newQty){ product.quantity = product.quantity - newQty; pushRemoveFromCartToGoogle(product); } else if (product.quantity < newQty){ product.quantity = newQty - product.quantity; pushAddProductToGoogle(product); } } } //bind the product click event to stock listing $.each($(".stockthumbwrapper li a"),function(){ var url = this.href; if (url.substr(-3) === ".ir"){ $(this).bind("click",function(){productClickFromListing(this);}) } }); //bind the productAdd event to stock listing $.each($(".stockthumbaddbutton a"),function(){ $(this).bind("click",function(){pushAddFromListingPage(this);}) }); //bind the productPage Add to basket $('.productdetailwrap .buttonaddtobasket').bind("click",function(){pushAddFromDetailPage()}) $.each($(".basketremove a"),function(){ var query = this.href; var vars = query.split("&"); var index = -1; for (var i=0;i -1){ $(this).bind("click",function(){pushBasketRemove(index);}) } }); $.each($(".basketremove a"),function(){ var query = this.href; var vars = query.split("&"); var index = -1; for (var i=0;i -1){ $($(this).parent().children("input")[0]).bind("click",function(){processBasketUpdateForGoogle(index);}) } }); pushDetailPage(); pushProductImpressions(); pushCheckoutStep();