Skip to content

Commit c406aa8

Browse files
authored
Merge pull request #260 from WouterSteen/WouterSteen-patch-3
Create the possibility to give a callback function with the DL pusher so we dont use the event fireing way like in: script-product-clicks.phtml
2 parents 8c6504f + 304e782 commit c406aa8

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

view/frontend/templates/hyva/script-pusher.phtml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,18 @@
99
*/
1010
?>
1111
<script>
12-
function yireoGoogleTagManager2Pusher(eventData, message) {
12+
function yireoGoogleTagManager2Pusher(eventData, message, callback) {
1313
window.YIREO_GOOGLETAGMANAGER2_PAST_EVENTS = window.YIREO_GOOGLETAGMANAGER2_PAST_EVENTS || [];
1414

15+
function doCallback(cb) {
16+
if (undefined === cb) {
17+
return;
18+
}
19+
20+
cb();
21+
}
22+
23+
1524
const copyEventData = Object.assign({}, eventData);
1625
let metaData = {};
1726
if (copyEventData.meta) {
@@ -22,12 +31,14 @@
2231
const eventHash = btoa(encodeURIComponent(JSON.stringify(copyEventData)));
2332
if (window.YIREO_GOOGLETAGMANAGER2_PAST_EVENTS.includes(eventHash)) {
2433
yireoGoogleTagManager2Logger('Warning: Event already triggered', eventData);
34+
doCallback(callback);
2535
return;
2636
}
2737

2838
if (metaData && metaData.allowed_pages && metaData.allowed_pages.length > 0
2939
&& false === metaData.allowed_pages.some(page => window.location.pathname.includes(page))) {
3040
yireoGoogleTagManager2Logger('Warning: Skipping event, not in allowed pages', window.location.pathname, eventData);
41+
doCallback(callback);
3142
return;
3243
}
3344

@@ -42,7 +53,13 @@
4253
window.dataLayer.push({ecommerce: null});
4354
}
4455

45-
window.dataLayer.push(eventData);
46-
window.YIREO_GOOGLETAGMANAGER2_PAST_EVENTS.push(eventHash);
56+
try {
57+
window.dataLayer.push(eventData);
58+
window.YIREO_GOOGLETAGMANAGER2_PAST_EVENTS.push(eventHash);
59+
} catch(error) {
60+
doCallback(callback);
61+
}
62+
63+
doCallback(callback);
4764
}
4865
</script>

0 commit comments

Comments
 (0)