Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trigger view-cart only when the mini-cart is opened (Hyva) #266

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DataLayer/Event/ViewCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private function getAllowedPages(): array
private function getAllowedEvents(): array
{
if ($this->config->showViewMiniCartOnExpandOnly() && $this->config->showViewCartEventEverywhere()) {
return ['minicart_collapse'];
return ['view_cart_event' => 'minicart_collapse'];
}

return [];
Expand Down
24 changes: 23 additions & 1 deletion view/frontend/templates/hyva/script-additions.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use Yireo\GoogleTagManager2\ViewModel\Commons;
$commons = $block->getCommonsViewModel();
?>
<script>
window.YIREO_GOOGLETAGMANAGER2_SKIPPED_EVENTS = window.YIREO_GOOGLETAGMANAGER2_SKIPPED_EVENTS || [];

window.addEventListener('private-content-loaded', function(event) {
window.dataLayer = window.dataLayer || [];

Expand Down Expand Up @@ -55,10 +57,21 @@ $commons = $block->getCommonsViewModel();
}

const gtmEvents = event.detail.data[sectionName].gtm_events;
for (const [eventId, eventData] of Object.entries(gtmEvents)) {
eventsLoop: for (const [eventId, eventData] of Object.entries(gtmEvents)) {
yireoGoogleTagManager2Logger('found event in section "' + sectionName + '"', eventData);

const metaData = eventData.meta;

if (metaData && metaData.allowed_events) {
for (const [allowedEventKey, allowedEvent] of Object.entries(metaData.allowed_events)) {
if (allowedEventKey === eventId) {
yireoGoogleTagManager2Logger('Warning: Skipping event, not in allowed events', eventData);
window.YIREO_GOOGLETAGMANAGER2_SKIPPED_EVENTS.push(eventData);
continue eventsLoop;
}
}
}

yireoGoogleTagManager2Pusher(eventData, 'push (customerData "' + sectionName + '" event "' + eventId + '")');

if (!metaData || metaData.cacheable !== true) {
Expand All @@ -82,4 +95,13 @@ $commons = $block->getCommonsViewModel();
yireoGoogleTagManager2Pusher(attributes, 'dataLayer (initial js) [script-additions.phtml]');
}
});
window.addEventListener('toggle-cart', function(event) {
if (event.detail.isOpen) {
for (const skippedEvent of window.YIREO_GOOGLETAGMANAGER2_SKIPPED_EVENTS) {
if (skippedEvent.event === 'view_cart') {
yireoGoogleTagManager2Pusher(skippedEvent, 'push (view_cart - mini-cart) [script-additions.phtml]')
}
}
}
});
</script>
2 changes: 1 addition & 1 deletion view/frontend/web/js/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ define([
logger('customerData section "' + sectionName + '" contains event "' + eventId + '"', eventData);

const metaData = Object.assign({}, eventData.meta);
if (metaData && metaData.allowed_events && metaData.allowed_events.length > 0) {
if (metaData && metaData.allowed_events) {
for (const [allowedEventKey, allowedEvent] of Object.entries(metaData.allowed_events)) {
$(window).on(allowedEvent, function () {
pusher(eventData, 'push (allowed event "' + allowedEventKey + '") [generic.js]');
Expand Down
Loading