diff --git a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php index 477c684f47d..751c65770d6 100644 --- a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php +++ b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php @@ -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(); } @@ -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(); } } @@ -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 @@ -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 @@ -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 @@ -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]; } } @@ -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); } diff --git a/docs/EVENTS.md b/docs/EVENTS.md index 88f8b20fd27..8557533c0d8 100644 --- a/docs/EVENTS.md +++ b/docs/EVENTS.md @@ -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 |