|
1 | | -function registerAddToCartListener(selector, productId) { |
2 | | - let trackingSent = false; |
3 | | - const trackingHelper = factfinder.communication.Util.trackingHelper; |
4 | | - |
5 | | - function trackAddToCart(product) { |
6 | | - factfinder.communication.Tracking.cart({ |
7 | | - id: trackingHelper.getTrackingProductId(product), |
8 | | - masterId: trackingHelper.getMasterArticleNumber(product), |
9 | | - price: trackingHelper.getPrice(product), |
10 | | - title: trackingHelper.getTitle(product), |
11 | | - count: 1, |
| 1 | +function registerAddToCartListener(selector, productData) { |
| 2 | + if (typeof factfinder === 'undefined') { |
| 3 | + document.addEventListener('ffCommunicationReady', function () { |
| 4 | + init(selector, productData); |
12 | 5 | }); |
| 6 | + } else { |
| 7 | + init(selector, productData); |
13 | 8 | } |
14 | 9 |
|
15 | | - const element = document.querySelector(selector); |
| 10 | + function init(selector, productData) { |
| 11 | + const trackingHelper = factfinder.communication.Util.trackingHelper; |
16 | 12 |
|
17 | | - if (element) { |
18 | | - element.addEventListener('submit', function (e) { |
19 | | - if (trackingSent === false) { |
20 | | - e.preventDefault(); |
21 | | - } |
| 13 | + function trackAddToCart(product) { |
| 14 | + factfinder.communication.Tracking.cart({ |
| 15 | + id: trackingHelper.getTrackingProductId(product), |
| 16 | + masterId: trackingHelper.getMasterArticleNumber(product), |
| 17 | + price: trackingHelper.getPrice(product), |
| 18 | + title: trackingHelper.getTitle(product), |
| 19 | + count: 1, |
| 20 | + }); |
| 21 | + } |
22 | 22 |
|
23 | | - factfinder.communication.EventAggregator.addFFEvent({ |
24 | | - type: 'getRecords', |
25 | | - recordId: productId, |
26 | | - idType: 'productNumber', |
27 | | - success: function (response) { |
28 | | - if (response && response[0]) { |
29 | | - const product = response[0]; |
30 | | - trackAddToCart(product); |
31 | | - } |
| 23 | + const element = document.querySelector(selector); |
32 | 24 |
|
33 | | - if (trackingSent === false) { |
34 | | - trackingSent = true; |
35 | | - e.target.submit(); |
36 | | - } |
37 | | - }, |
| 25 | + if (element) { |
| 26 | + element.addEventListener('submit', function (e) { |
| 27 | + trackAddToCart(productData); |
38 | 28 | }); |
39 | | - }); |
| 29 | + } |
40 | 30 | } |
41 | 31 | } |
0 commit comments