View difference between Paste ID: hj228140 and Jxu5JU3a
SHOW: | | - or go back to the newest paste.
1
/*global $, $TB */
2
3
// Scrape inpage cart
4
var scrapeInPageCart = function () {
5
    try {
6
        var root = jQuery('#basketOverlayContent');
7
8
        store.cart = {
9
            p: [],
10
            qty: 0,
11
            curr: store.curr
12
        };
13
14
        root.find('#orderTableX tbody:eq(0) tr:even').each(function () {
15
            var item = {}, elt = jQuery(this);
16
17
            try {
18
                item.n = $TB.get_text(elt.find('p')[0], true);
19
                item.img_tn = (elt.find('img.basketProductImage')[0] || {}).src;
20
                item.prid = $TB.get_text(elt.find('p')[2], true);
21
                item.sku = item.prid;
22
            } catch(e) {
23
            }
24
25
            try {
26
                item.curr = store.curr;
27
28
                item.qty = $TB.get_value_from_text(elt.find('.basketQtyInput').val());
29
30
                item.uv = $TB.get_value_from_text($TB.get_text(elt.find('.numberFont:first'), true));
31
                item.up = $TB.format_currency(item.uv, item.curr);
32
            } catch(e) {
33
            }
34
35
            try {
36
                if(item.prid) {
37
                    store.cart.qty += item.qty;
38
                    store.cart.p.push(item);
39
                }
40
            } catch(e) {
41
            }
42
43
        });
44
45
        store.cart.cv = $TB.get_value_from_text($TB.get_text(root.find('.orderTotalDetailValue.numberFont')[0], true));
46
        store.cart.cp = $TB.format_currency(store.cart.cv, store.curr);
47
48
        jQuery(document).ajaxStop(function () {
49
            var currentCV = store.cart ? store.cart.cv : 0;
50
51
            updateInPageCart();
52
53
            if(store.cart.cv !== currentCV) {
54
                $TB.sendBeacon();
55
            }
56
        });
57
    } catch(e) {
58
    }
59
60
};
61
62
var updateInPageCart = function () {
63
    try {
64
        $TB.Scheduler.waitfor(function () {
65
            return jQuery('#basketOverlayContent').css('display') === 'block'
66
        }, function () {
67
            store.ptyp = PAGE_TYPE.CART_INPAGE;
68
            scrapeInPageCart();
69
            $TB.sendBeacon();
70
        }, {
71
            interval: 150,
72
            limit: 25
73
        });
74
    } catch(e) {
75
    }
76
};
77
78
var getPridFromU = function (u) {
79
80
    //Should return the product id (item.prid) or null if not found, given the URL for a product.
81
    try {
82
        var r = /\/invt\/([^\/]*)\//ig;
83
        var m = r.exec(u);
84
        if(m && m.length === 2) {
85
            return m[1];
86
        }
87
    } catch(e) {
88
    }
89
    return null;
90
91
};
92
93
store.typ = EVENT_TYPE.PAGE_VIEW;
94
store.ptyp = PAGE_TYPE.OTHER;
95
96
//{{CURRENCIES}}
97
98
store.curr = 'GBP';
99
100
var is_cart = function () {
101
102
    try {
103
        if($TB.oneof(store.doc.u, ['/basket'])) {
104
            store.ptyp = PAGE_TYPE.CART;
105
            return $('#content');
106
        }
107
    } catch(e) {
108
    }
109
};
110
111
var is_product_details = function () {
112
113
    try {
114
        if($TB.oneof(store.doc.u, ['/product/'])) {
115
            store.ptyp = PAGE_TYPE.PRODUCT_BROWSE;
116
            return $('body');
117
        }
118
    } catch(e) {
119
    }
120
};
121
122
var is_product_list = function () {
123
124
    try {
125
        if($TB.oneof(store.doc.u, ['/shop/'])) {
126
            store.ptyp = PAGE_TYPE.PRODUCT_LIST;
127
            return $('#content');
128
        }
129
    } catch(e) {
130
    }
131
};
132
133
var is_login = function () {
134
135
    try {
136
        if($TB.oneof(store.doc.u, ['/account'])) {
137
            store.ptyp = PAGE_TYPE.LOGIN;
138
            return $('#content');
139
        }
140
    } catch(e) {
141
    }
142
143
};
144
145
var is_purchase_complete = function () {
146
147
    try {
148
        if($TB.oneof(store.doc.u, ['/checkout/customer/complete'])) {
149
            store.ptyp = PAGE_TYPE.CHECKOUT_THANKS;
150
            return true;
151
        }
152
    } catch(e) {
153
    }
154
155
};
156
157
var is_signup = function () {
158
159
    try {
160
        if($TB.oneof(store.doc.u, ['/register'])) {
161
            store.ptyp = PAGE_TYPE.REGISTER;
162
            return $('#content');
163
        }
164
    } catch(e) {
165
    }
166
167
};
168
169
var is_checkout = function () {
170
171
    try {
172
        if($TB.oneof(store.doc.u, ['/payment'])) {
173
            store.ptyp = PAGE_TYPE.CHECKOUT;
174
            return $('#content');
175
        }
176
    } catch(e) {
177
    }
178
179
};
180
181
var base = is_product_list() || is_purchase_complete() || is_cart() || is_product_details() || is_checkout() || is_signup() || is_login();
182
183
var details = {
184
185
    pl: function (root) {
186
187
        store.p = [];
188
189
        root.find('.departmentProduct').each(function (index) {
190
            var item = {};
191
192
            try {
193
                item.prid = window.dataLayer[1].ProductID[index];
194
                item.sku = item.prid;
195
            } catch(e) {
196
            }
197
198
            try {
199
                var breadcrumbs = root.find('.breadcrumbs a:not(:first)');
200
                if(breadcrumbs.length) {
201
                    item.cat = item.cat || [];
202
                    breadcrumbs.each(function () {
203
                        item.cat.push($TB.get_text(this, true));
204
                    });
205
                }
206
            } catch(e) {
207
            }
208
209
            try {
210
                item.promo = item.promo || {};
211
                item.promo.clearance = $TB.oneof(store.doc.u, ['/clearance']);
212
            } catch(e) {
213
            }
214
215
            store.p.push(item);
216
        });
217
    },
218
219
    //PRODUCT
220
    pb: function (root) {
221
        store.p = [];
222
223
        var item = {ex: {}};
224
225
        try {
226
            var canonical = jQuery('link[rel="canonical"]');
227
            if(canonical.length) {
228
                item.u = (canonical[0] || {}).href;
229
            } else {
230
                item.u = store.doc.u;
231
            }
232
            item.n = $TB.get_text(root.find('#productTitle'), true);
233
234
            if(store.doc.u.split("//")[1].split(".")[0] !== "m") {
235
                item.img = root.find('#thumbnails #image1 a').data('zoomImage');
236
                item.ex.img2 = root.find('#thumbnails #image2 a').data('zoomImage');
237
            }
238
239
            //Sets description and adds a space after every full stop.
240
            item.desc = $TB.get_text(root.find('[itemprop="description"], .infoContent'), true).replace(/\.(\S)/g, '. $1');
241
242
        } catch(e) {
243
        }
244
245
        try {
246
            item.prid = window.dataLayer[0].product_sku || window.dataLayer[0].product_id;
247
            item.sku = item.prid;
248
        } catch(e) {
249
        }
250
251
        try {
252
            item.curr = store.curr;
253
            item.uv = $TB.get_value_from_text($TB.get_text(root.find('#webPrice, #ourPrice'), true));
254
            item.up = $TB.format_currency(item.uv, item.curr);
255
256
            if(root.find('#highStPrice').length) {
257
                item.ruv = $TB.get_value_from_text($TB.get_text(root.find('#highStPrice'), true));
258
                item.rup = $TB.format_currency(item.ruv, item.curr);
259
            } else {
260
                item.ruv = item.uv;
261
                item.rup = item.up;
262
            }
263
        } catch(e) {
264
        }
265
266
        try {
267
            item.cat = [];
268
269
            item.cat.push({
270
                g: 'half-price',
271
                n: item.ruv > item.uv * 2,
272
                r: true
273
            });
274
275
            item.ex.productId = root.find('input#productId').val();
276
277
            if(root.find('.rating-stats[itemprop="aggregateRating"]').length) {
278
                item.reviews = item.reviews || {};
279
                item.reviews.rating = $TB.get_value_from_text($TB.get_text(root.find('span[itemprop="ratingValue"]'), true));
280
                item.reviews.ratingCount = $TB.get_value_from_text($TB.get_text(root.find('span[itemprop="reviewCount"]'), true));
281
            }
282
        } catch(e) {
283
        }
284
        try {
285
            if(root.find('#productStatus p:contains(IN STOCK), #productStatus span:contains(In Stock)').length) {
286
                item.stock = 1;
287
            } else {
288
                item.stock = 0;
289
            }
290
        } catch(e) {
291
        }
292
293
        try {
294
            for(var i = 0; i < jQuery('[itemprop="additionalProperty"]').length; i++) {
295
296
                var name = jQuery('[itemprop="additionalProperty"]:eq(' + i + ') meta[itemprop="name"]')[0].content.replace(/\s+/g, '_').toLowerCase();
297
                item.ex.suitability = item.ex.suitability || {};
298
299
                item.ex.suitability[name] = {};
300
                item.ex.suitability[name]['value'] = jQuery('[itemprop="additionalProperty"]:eq(' + i + ') meta[itemprop="value"]')[0].content;
301
                item.ex.suitability[name]['image'] = jQuery('[itemprop="additionalProperty"]:eq(' + i + ') img')[0].src;
302
303
            }
304
        } catch(e) {
305
        }
306
307
        jQuery(document).on('click', '.addToBasket', function () {
308
            updateInPageCart();
309
        });
310
311
        if(item.prid !== "7804") {
312
            store.p.push(item);
313
        }
314
    },
315
316
    //CART
317
    ca: function (root) {
318
        var captureCart = function () {
319
            store.cart = {
320
                p: [],
321
                qty: 0,
322
                curr: store.curr
323
            };
324
325
            //Capture cart data,
326
            root.find('#basketShopping tbody:first > tr,  #basket .basketProduct').each(function (index) {
327
328
                var item = {}, elt = $(this);
329
330
                try {
331
                    item.n = $TB.get_text(elt.find('.productTitle'), true, " - ")[0];
332
333
                    if(store.doc.u.split("//")[1].split(".")[0] !== "m") {
334
                        item.img_tn = (elt.find('img')[0] || {}).src;
335
                    }
336
                } catch(e) {
337
                }
338
339
                try {
340
                    item.prid = window.dataLayer[1].ProductID[index].id;
341
                    item.sku = item.prid;
342
                } catch(e) {
343
                }
344
345
                try {
346
                    item.curr = store.curr;
347
348
                    item.qty = Number(elt.find(".basketQtyInput, .productQuantityAmount input").val());
349
350
                    item.uv = window.dataLayer[1].ProductID[index].price;
351
                    item.up = $TB.format_currency(item.uv, item.curr);
352
353
                } catch(e) {
354
                }
355
356
                try {
357
                    store.cart.qty += item.qty;
358
                } catch(e) {
359
                }
360
361
                // Santoku knife purposefully removed from capture
362
                // https://www.procook.co.uk/product/procook-professional-x50-santoku-knife-18cm-7inch-promo
363
364
                if(item.prid !== "7804") {
365
                    store.cart.p.push(item);
366
                }
367
            });
368
369
            store.cart.ex = store.cart.ex || {};
370
371
            var prices = jQuery('.orderTotalDetailValue');
372
373
            store.cart.ex.delivery = $TB.get_text(prices[1]) === "FREE" ? 0 : $TB.get_value_from_text($TB.get_text(prices[1]), true);
374
            store.cart.ex.total = $TB.get_value_from_text($TB.get_text(prices[2]), true);
375
376
            store.cart.cv = $TB.get_value_from_text($TB.get_text(prices[0]), true);
377
            store.cart.cp = $TB.format_currency(store.cart.cv, store.curr);
378
        };
379
380
        captureCart();
381
382
        jQuery(document).ajaxStop(function () {
383
            var currentCV = store.cart ? store.cart.cv : 0;
384
385
            captureCart();
386
387
            if(store.cart.cv !== currentCV) {
388
                $TB.sendBeacon();
389
            }
390
        });
391
392
        $TB.getExtraSlotQSOptions = function () {
393
            var prid = [];
394
            for(var i = 0; i < store.cart.p.length; i++) {
395
                prid.push(store.cart.p[i].prid);
396
            }
397
            return {prid: prid.join('||')};
398
        };
399
    },
400
401
    //LOGIN
402
    lo: function (root) {
403
        try {
404
405
            $TB.fieldListener('e', root.find('#email'), {
406
                email_field: true,
407
                immediate: true
408
            });
409
        } catch(e) {
410
        }
411
    },
412
413
    //REGISTER
414
    re: function (root) {
415
        try {
416
417
            $TB.fieldListener('e', root.find('#email'), {
418
                email_field: true,
419
                immediate: true
420
            });
421
            $TB.fieldListener('fn', root.find('#firstname'));
422
            $TB.fieldListener('ln', root.find('#lastname'));
423
424
        } catch(e) {
425
        }
426
    },
427
428
    //CHECKOUT
429
    co: function () {
430
        try {
431
432
            $TB.fieldListener('e', jQuery('#email'), {
433
                email_field: true,
434
                immediate: true
435
            });
436
437
        } catch(e) {
438
        }
439
    },
440
441
    //PURCHASE COMPLETE
442
    pc: function (root) {
443
444
        store.co = {};
445
        try {
446
            var onum = root.find('.ordernumber');
447
            if(onum.length) {
448
                store.co.o_num = $TB.get_text(onum, true);
449
            }
450
451
        } catch(e) {
452
        }
453
    }
454
};
455
456
switch(store.ptyp) {
457
    case PAGE_TYPE.PRODUCT_LIST:
458
        details.pl(base);
459
        break;
460
    case PAGE_TYPE.PRODUCT_BROWSE:
461
        details.pb(base);
462
        break;
463
    case PAGE_TYPE.CART:
464
        details.ca(base);
465
        break;
466
    case PAGE_TYPE.LOGIN:
467
        details.lo(base);
468
        break;
469
    case PAGE_TYPE.REGISTER:
470
        details.re(base);
471
        break;
472
    case PAGE_TYPE.CHECKOUT:
473
        details.co(base);
474
        break;
475
    case PAGE_TYPE.CHECKOUT_THANKS:
476
        details.pc(base);
477
        break;
478
}
479
480
// Email captures
481
try {
482
    $TB.fieldListener('e', jQuery('#signUp, #registerEmail, #email'), {
483
        email_field: true,
484
        immediate: true
485
    });
486
} catch(e) {
487
}
488
489
// Newsletter Login page
490
try {
491
    $TB.fieldListener('e', jQuery('#signUpFooter'), {
492
        email_field: true,
493
        immediate: true
494
    });
495
} catch(e) {
496
}