Skip to content

Commit

Permalink
Merge branch 'master' of github.com:yireo/Yireo_GoogleTagManager2
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Jan 6, 2025
2 parents 57569f3 + c406aa8 commit 16c791d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
8 changes: 4 additions & 4 deletions MageWire/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ public function __construct(
$this->addPaymentInfo = $addPaymentInfo;
}

public function booted(): void
public function boot(): void
{
// @phpstan-ignore-next-line
parent::booted();
parent::boot();

// @todo: Do this only with the Hyva Checkout
// @phpstan-ignore-next-line
$this->listeners['shipping_method_selected'] = 'triggerShippingMethod';
$this->listeners['payment_method_selected'] = 'triggerShippingMethod';
$this->listeners['payment_method_selected'] = 'triggerPaymentMethod';

// @todo: Do this only with the Loki Checkout
$this->listeners['afterSaveShippingMethod'] = 'triggerShippingMethod';
$this->listeners['afterSavePaymentMethod'] = 'triggerShippingMethod';
$this->listeners['afterSavePaymentMethod'] = 'triggerPaymentMethod';
}

public function triggerShippingMethod()
Expand Down
23 changes: 20 additions & 3 deletions view/frontend/templates/hyva/script-pusher.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@
*/
?>
<script>
function yireoGoogleTagManager2Pusher(eventData, message) {
function yireoGoogleTagManager2Pusher(eventData, message, callback) {
window.YIREO_GOOGLETAGMANAGER2_PAST_EVENTS = window.YIREO_GOOGLETAGMANAGER2_PAST_EVENTS || [];

function doCallback(cb) {
if (undefined === cb) {
return;
}

cb();
}


const copyEventData = Object.assign({}, eventData);
let metaData = {};
if (copyEventData.meta) {
Expand All @@ -22,12 +31,14 @@
const eventHash = btoa(encodeURIComponent(JSON.stringify(copyEventData)));
if (window.YIREO_GOOGLETAGMANAGER2_PAST_EVENTS.includes(eventHash)) {
yireoGoogleTagManager2Logger('Warning: Event already triggered', eventData);
doCallback(callback);
return;
}

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

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

window.dataLayer.push(eventData);
window.YIREO_GOOGLETAGMANAGER2_PAST_EVENTS.push(eventHash);
try {
window.dataLayer.push(eventData);
window.YIREO_GOOGLETAGMANAGER2_PAST_EVENTS.push(eventHash);
} catch(error) {
doCallback(callback);
}

doCallback(callback);
}
</script>

0 comments on commit 16c791d

Please sign in to comment.