Skip to content

Commit

Permalink
Merge branch 'main' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
fballiano committed Sep 10, 2023
2 parents 7b20aa2 + 26843d6 commit 8a0c083
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
23 changes: 16 additions & 7 deletions app/code/core/Mage/GoogleAnalytics/Block/Ga.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ protected function _getEnhancedEcommerceDataForAnalytics4()
'item_category' => $removedProduct['category'],
];
$eventData['items'][] = $_item;
$result[] = "gtag('event', 'remove_from_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");";
$result[] = ['remove_from_cart', $eventData];
}
Mage::getSingleton('core/session')->unsRemovedProductsForAnalytics();
}
Expand All @@ -180,7 +180,7 @@ protected function _getEnhancedEcommerceDataForAnalytics4()
'item_category' => $_addedProduct['category'],
];
$eventData['items'][] = $_item;
$result[] = "gtag('event', 'add_to_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");";
$result[] = ['add_to_cart', $eventData];
Mage::getSingleton('core/session')->unsAddedProductsForAnalytics();
}
}
Expand All @@ -205,7 +205,7 @@ protected function _getEnhancedEcommerceDataForAnalytics4()
$_item['item_brand'] = $productViewed->getAttributeText('manufacturer');
}
array_push($eventData['items'], $_item);
$result[] = "gtag('event', 'view_item', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");";
$result[] = ['view_item', $eventData];
} elseif ($moduleName == 'catalog' && $controllerName == 'category') {
// Log this event when the user has been presented with a list of items of a certain category.
// @see https://developers.google.com/tag-platform/gtagjs/reference/events#view_item_list
Expand Down Expand Up @@ -246,7 +246,7 @@ protected function _getEnhancedEcommerceDataForAnalytics4()
$eventData['value'] += $productViewed->getFinalPrice();
}
$eventData['value'] = $helper->formatPrice($eventData['value']);
$result[] = "gtag('event', 'view_item_list', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");";
$result[] = ['view_item_list', $eventData];
} elseif ($moduleName == 'checkout' && $controllerName == 'cart') {
// This event signifies that a user viewed his cart.
// @see https://developers.google.com/tag-platform/gtagjs/reference/events#view_cart
Expand Down Expand Up @@ -278,7 +278,7 @@ protected function _getEnhancedEcommerceDataForAnalytics4()
$eventData['value'] += $_product->getFinalPrice() * $productInCart->getQty();
}
$eventData['value'] = $helper->formatPrice($eventData['value']);
$result[] = "gtag('event', 'view_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");";
$result[] = ['view_cart', $eventData];
} elseif ($moduleName == static::CHECKOUT_MODULE_NAME && $controllerName == static::CHECKOUT_CONTROLLER_NAME) {
// This event signifies that a user has begun a checkout.
// @see https://developers.google.com/tag-platform/gtagjs/reference/events#begin_checkout
Expand Down Expand Up @@ -310,7 +310,7 @@ protected function _getEnhancedEcommerceDataForAnalytics4()
$eventData['value'] += $_product->getFinalPrice();
}
$eventData['value'] = $helper->formatPrice($eventData['value']);
$result[] = "gtag('event', 'begin_checkout', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");";
$result[] = ['begin_checkout', $eventData];
}
}

Expand Down Expand Up @@ -354,13 +354,22 @@ protected function _getEnhancedEcommerceDataForAnalytics4()
}
array_push($orderData['items'], $_item);
}
$result[] = "gtag('event', 'purchase', " . json_encode($orderData, JSON_THROW_ON_ERROR) . ");";
$result[] = ['purchase', $orderData];
}
}

$ga4DataTransport = new Varien_Object();
$ga4DataTransport->setData($result);
Mage::dispatchEvent('googleanalytics_ga4_send_data_before', ['ga4_data_transport' => $ga4DataTransport]);
$result = $ga4DataTransport->getData();

if ($this->helper('googleanalytics')->isDebugModeEnabled() && count($result) > 0) {
$this->helper('googleanalytics')->log($result);
}

foreach ($result as $k => $ga4Event) {
$result[$k] = "gtag('event', '{$ga4Event[0]}', " . json_encode($ga4Event[1], JSON_THROW_ON_ERROR) . ");";
}
return implode("\n", $result);
}

Expand Down
1 change: 1 addition & 0 deletions docs/EVENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@
| end_index_events_[getEventTypeName] | 1.9.4.5 |
| end_process_event_[getEventTypeName] | 1.9.4.5 |
| gift_options_prepare_items | 1.9.4.5 |
| googleanalytics_ga4_send_data_before | 20.1.1 |
| http_response_send_before | 1.9.4.5 |
| index_process_change_status | 1.9.4.5 |
| init_from_order_session_quote_initialized | 1.9.4.5 |
Expand Down

0 comments on commit 8a0c083

Please sign in to comment.