Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*global $, $TB */
- // Scrape inpage cart
- var scrapeInPageCart = function () {
- try {
- var root = jQuery('#basketOverlayContent');
- store.cart = {
- p: [],
- qty: 0,
- curr: store.curr
- };
- root.find('#orderTableX tbody:eq(0) tr:even').each(function () {
- var item = {}, elt = jQuery(this);
- try {
- item.n = $TB.get_text(elt.find('p')[0], true);
- item.img_tn = (elt.find('img.basketProductImage')[0] || {}).src;
- item.prid = $TB.get_text(elt.find('p')[2], true);
- item.sku = item.prid;
- } catch(e) {
- }
- try {
- item.curr = store.curr;
- item.qty = $TB.get_value_from_text(elt.find('.basketQtyInput').val());
- item.uv = $TB.get_value_from_text($TB.get_text(elt.find('.numberFont:first'), true));
- item.up = $TB.format_currency(item.uv, item.curr);
- } catch(e) {
- }
- try {
- if(item.prid) {
- store.cart.qty += item.qty;
- store.cart.p.push(item);
- }
- } catch(e) {
- }
- });
- store.cart.cv = $TB.get_value_from_text($TB.get_text(root.find('.orderTotalDetailValue.numberFont')[0], true));
- store.cart.cp = $TB.format_currency(store.cart.cv, store.curr);
- jQuery(document).ajaxStop(function () {
- var currentCV = store.cart ? store.cart.cv : 0;
- updateInPageCart();
- if(store.cart.cv !== currentCV) {
- $TB.sendBeacon();
- }
- });
- } catch(e) {
- }
- };
- var updateInPageCart = function () {
- try {
- $TB.Scheduler.waitfor(function () {
- return jQuery('#basketOverlayContent').css('display') === 'block'
- }, function () {
- store.ptyp = PAGE_TYPE.CART_INPAGE;
- scrapeInPageCart();
- $TB.sendBeacon();
- }, {
- interval: 150,
- limit: 25
- });
- } catch(e) {
- }
- };
- var getPridFromU = function (u) {
- //Should return the product id (item.prid) or null if not found, given the URL for a product.
- try {
- var r = /\/invt\/([^\/]*)\//ig;
- var m = r.exec(u);
- if(m && m.length === 2) {
- return m[1];
- }
- } catch(e) {
- }
- return null;
- };
- store.typ = EVENT_TYPE.PAGE_VIEW;
- store.ptyp = PAGE_TYPE.OTHER;
- //{{CURRENCIES}}
- store.curr = 'GBP';
- var is_cart = function () {
- try {
- if($TB.oneof(store.doc.u, ['/basket'])) {
- store.ptyp = PAGE_TYPE.CART;
- return $('#content');
- }
- } catch(e) {
- }
- };
- var is_product_details = function () {
- try {
- if($TB.oneof(store.doc.u, ['/product/'])) {
- store.ptyp = PAGE_TYPE.PRODUCT_BROWSE;
- return $('body');
- }
- } catch(e) {
- }
- };
- var is_product_list = function () {
- try {
- if($TB.oneof(store.doc.u, ['/shop/'])) {
- store.ptyp = PAGE_TYPE.PRODUCT_LIST;
- return $('#content');
- }
- } catch(e) {
- }
- };
- var is_login = function () {
- try {
- if($TB.oneof(store.doc.u, ['/account'])) {
- store.ptyp = PAGE_TYPE.LOGIN;
- return $('#content');
- }
- } catch(e) {
- }
- };
- var is_purchase_complete = function () {
- try {
- if($TB.oneof(store.doc.u, ['/checkout/customer/complete'])) {
- store.ptyp = PAGE_TYPE.CHECKOUT_THANKS;
- return true;
- }
- } catch(e) {
- }
- };
- var is_signup = function () {
- try {
- if($TB.oneof(store.doc.u, ['/register'])) {
- store.ptyp = PAGE_TYPE.REGISTER;
- return $('#content');
- }
- } catch(e) {
- }
- };
- var is_checkout = function () {
- try {
- if($TB.oneof(store.doc.u, ['/payment'])) {
- store.ptyp = PAGE_TYPE.CHECKOUT;
- return $('#content');
- }
- } catch(e) {
- }
- };
- var base = is_product_list() || is_purchase_complete() || is_cart() || is_product_details() || is_checkout() || is_signup() || is_login();
- var details = {
- pl: function (root) {
- store.p = [];
- root.find('.departmentProduct').each(function (index) {
- var item = {};
- try {
- item.prid = window.dataLayer[1].ProductID[index];
- item.sku = item.prid;
- } catch(e) {
- }
- try {
- var breadcrumbs = root.find('.breadcrumbs a:not(:first)');
- if(breadcrumbs.length) {
- item.cat = item.cat || [];
- breadcrumbs.each(function () {
- item.cat.push($TB.get_text(this, true));
- });
- }
- } catch(e) {
- }
- try {
- item.promo = item.promo || {};
- item.promo.clearance = $TB.oneof(store.doc.u, ['/clearance']);
- } catch(e) {
- }
- store.p.push(item);
- });
- },
- //PRODUCT
- pb: function (root) {
- store.p = [];
- var item = {ex: {}};
- try {
- var canonical = jQuery('link[rel="canonical"]');
- if(canonical.length) {
- item.u = (canonical[0] || {}).href;
- } else {
- item.u = store.doc.u;
- }
- item.n = $TB.get_text(root.find('#productTitle'), true);
- if(store.doc.u.split("//")[1].split(".")[0] !== "m") {
- item.img = root.find('#thumbnails #image1 a').data('zoomImage');
- item.ex.img2 = root.find('#thumbnails #image2 a').data('zoomImage');
- }
- //Sets description and adds a space after every full stop.
- item.desc = $TB.get_text(root.find('[itemprop="description"], .infoContent'), true).replace(/\.(\S)/g, '. $1');
- } catch(e) {
- }
- try {
- item.prid = window.dataLayer[0].product_sku || window.dataLayer[0].product_id;
- item.sku = item.prid;
- } catch(e) {
- }
- try {
- item.curr = store.curr;
- item.uv = $TB.get_value_from_text($TB.get_text(root.find('#webPrice, #ourPrice'), true));
- item.up = $TB.format_currency(item.uv, item.curr);
- if(root.find('#highStPrice').length) {
- item.ruv = $TB.get_value_from_text($TB.get_text(root.find('#highStPrice'), true));
- item.rup = $TB.format_currency(item.ruv, item.curr);
- } else {
- item.ruv = item.uv;
- item.rup = item.up;
- }
- } catch(e) {
- }
- try {
- item.cat = [];
- item.cat.push({
- g: 'half-price',
- n: item.ruv > item.uv * 2,
- r: true
- });
- item.ex.productId = root.find('input#productId').val();
- if(root.find('.rating-stats[itemprop="aggregateRating"]').length) {
- item.reviews = item.reviews || {};
- item.reviews.rating = $TB.get_value_from_text($TB.get_text(root.find('span[itemprop="ratingValue"]'), true));
- item.reviews.ratingCount = $TB.get_value_from_text($TB.get_text(root.find('span[itemprop="reviewCount"]'), true));
- }
- } catch(e) {
- }
- try {
- if(root.find('#productStatus p:contains(IN STOCK), #productStatus span:contains(In Stock)').length) {
- item.stock = 1;
- } else {
- item.stock = 0;
- }
- } catch(e) {
- }
- try {
- for(var i = 0; i < jQuery('[itemprop="additionalProperty"]').length; i++) {
- var name = jQuery('[itemprop="additionalProperty"]:eq(' + i + ') meta[itemprop="name"]')[0].content.replace(/\s+/g, '_').toLowerCase();
- item.ex.suitability = item.ex.suitability || {};
- item.ex.suitability[name] = {};
- item.ex.suitability[name]['value'] = jQuery('[itemprop="additionalProperty"]:eq(' + i + ') meta[itemprop="value"]')[0].content;
- item.ex.suitability[name]['image'] = jQuery('[itemprop="additionalProperty"]:eq(' + i + ') img')[0].src;
- }
- } catch(e) {
- }
- jQuery(document).on('click', '.addToBasket', function () {
- updateInPageCart();
- });
- if(item.prid !== "7804") {
- store.p.push(item);
- }
- },
- //CART
- ca: function (root) {
- var captureCart = function () {
- store.cart = {
- p: [],
- qty: 0,
- curr: store.curr
- };
- //Capture cart data,
- root.find('#basketShopping tbody:first > tr, #basket .basketProduct').each(function (index) {
- var item = {}, elt = $(this);
- try {
- item.n = $TB.get_text(elt.find('.productTitle'), true, " - ")[0];
- if(store.doc.u.split("//")[1].split(".")[0] !== "m") {
- item.img_tn = (elt.find('img')[0] || {}).src;
- }
- } catch(e) {
- }
- try {
- item.prid = window.dataLayer[1].ProductID[index].id;
- item.sku = item.prid;
- } catch(e) {
- }
- try {
- item.curr = store.curr;
- item.qty = Number(elt.find(".basketQtyInput, .productQuantityAmount input").val());
- item.uv = window.dataLayer[1].ProductID[index].price;
- item.up = $TB.format_currency(item.uv, item.curr);
- } catch(e) {
- }
- try {
- store.cart.qty += item.qty;
- } catch(e) {
- }
- // Santoku knife purposefully removed from capture
- // https://www.procook.co.uk/product/procook-professional-x50-santoku-knife-18cm-7inch-promo
- if(item.prid !== "7804") {
- store.cart.p.push(item);
- }
- });
- store.cart.ex = store.cart.ex || {};
- var prices = jQuery('.orderTotalDetailValue');
- store.cart.ex.delivery = $TB.get_text(prices[1]) === "FREE" ? 0 : $TB.get_value_from_text($TB.get_text(prices[1]), true);
- store.cart.ex.total = $TB.get_value_from_text($TB.get_text(prices[2]), true);
- store.cart.cv = $TB.get_value_from_text($TB.get_text(prices[0]), true);
- store.cart.cp = $TB.format_currency(store.cart.cv, store.curr);
- };
- captureCart();
- jQuery(document).ajaxStop(function () {
- var currentCV = store.cart ? store.cart.cv : 0;
- captureCart();
- if(store.cart.cv !== currentCV) {
- $TB.sendBeacon();
- }
- });
- $TB.getExtraSlotQSOptions = function () {
- var prid = [];
- for(var i = 0; i < store.cart.p.length; i++) {
- prid.push(store.cart.p[i].prid);
- }
- return {prid: prid.join('||')};
- };
- },
- //LOGIN
- lo: function (root) {
- try {
- $TB.fieldListener('e', root.find('#email'), {
- email_field: true,
- immediate: true
- });
- } catch(e) {
- }
- },
- //REGISTER
- re: function (root) {
- try {
- $TB.fieldListener('e', root.find('#email'), {
- email_field: true,
- immediate: true
- });
- $TB.fieldListener('fn', root.find('#firstname'));
- $TB.fieldListener('ln', root.find('#lastname'));
- } catch(e) {
- }
- },
- //CHECKOUT
- co: function () {
- try {
- $TB.fieldListener('e', jQuery('#email'), {
- email_field: true,
- immediate: true
- });
- } catch(e) {
- }
- },
- //PURCHASE COMPLETE
- pc: function (root) {
- store.co = {};
- try {
- var onum = root.find('.ordernumber');
- if(onum.length) {
- store.co.o_num = $TB.get_text(onum, true);
- }
- } catch(e) {
- }
- }
- };
- switch(store.ptyp) {
- case PAGE_TYPE.PRODUCT_LIST:
- details.pl(base);
- break;
- case PAGE_TYPE.PRODUCT_BROWSE:
- details.pb(base);
- break;
- case PAGE_TYPE.CART:
- details.ca(base);
- break;
- case PAGE_TYPE.LOGIN:
- details.lo(base);
- break;
- case PAGE_TYPE.REGISTER:
- details.re(base);
- break;
- case PAGE_TYPE.CHECKOUT:
- details.co(base);
- break;
- case PAGE_TYPE.CHECKOUT_THANKS:
- details.pc(base);
- break;
- }
- // Email captures
- try {
- $TB.fieldListener('e', jQuery('#signUp, #registerEmail, #email'), {
- email_field: true,
- immediate: true
- });
- } catch(e) {
- }
- // Newsletter Login page
- try {
- $TB.fieldListener('e', jQuery('#signUpFooter'), {
- email_field: true,
- immediate: true
- });
- } catch(e) {
- }
Add Comment
Please, Sign In to add comment