From f8a604fe5d80d1e7bbd54c22eef041bcb32d7830 Mon Sep 17 00:00:00 2001 From: hirale <22028058+hirale@users.noreply.github.com> Date: Thu, 13 Jun 2024 13:25:35 +0200 Subject: [PATCH] refactor: refactor debug mode and fix bugs --- .../GAMeasurementProtocol/Helper/Data.php | 26 +++++++------ .../GAMeasurementProtocol/Model/Api.php | 5 +-- .../GAMeasurementProtocol/Model/Observer.php | 37 +++++++++++++------ .../GAMeasurementProtocol/etc/config.xml | 9 +++++ .../GAMeasurementProtocol/etc/system.xml | 2 +- 5 files changed, 53 insertions(+), 26 deletions(-) diff --git a/src/app/code/community/Hirale/GAMeasurementProtocol/Helper/Data.php b/src/app/code/community/Hirale/GAMeasurementProtocol/Helper/Data.php index 7ea33f3..29b82e3 100644 --- a/src/app/code/community/Hirale/GAMeasurementProtocol/Helper/Data.php +++ b/src/app/code/community/Hirale/GAMeasurementProtocol/Helper/Data.php @@ -2,7 +2,7 @@ class Hirale_GAMeasurementProtocol_Helper_Data extends Mage_Core_Helper_Abstract { const GA4_MEASUREMENT_PROTOCOL_URL = 'https://www.google-analytics.com/mp/collect'; - const GA4_MEASUREMENT_PROTOCOL_DEBUG_URL = 'https://www.google-analytics.com/debug/mp/collect'; + const GTAG_URL = 'https://www.googletagmanager.com/gtag/destination'; protected $_isMeasurementEnabled = null; protected $_measurementId = null; protected $_apiSecret = null; @@ -26,7 +26,7 @@ public function isDebugMode() public function getMeasurementProtocolUrl() { - return $this->isDebugMode() ? self::GA4_MEASUREMENT_PROTOCOL_DEBUG_URL : self::GA4_MEASUREMENT_PROTOCOL_URL; + return self::GA4_MEASUREMENT_PROTOCOL_URL; } @@ -49,16 +49,20 @@ public function getApiSecret() public function getClientId() { - - if (isset($_COOKIE['_ga'])) { - $ga = explode('.', $_COOKIE['_ga']); - $clientId = $ga[2] . '.' . $ga[3]; - } else { - $randomNumber = mt_rand(1000000000, 9999999999); - $timestamp = time(); - $clientId = $randomNumber . '.' . $timestamp; + $session = Mage::getSingleton('core/session'); + $clientId = $session->getData('ga_client_id'); + if (!$clientId) { + if (isset($_COOKIE['_ga'])) { + $ga = explode('.', $_COOKIE['_ga']); + $clientId = $ga[2] . '.' . $ga[3]; + } else { + $randomNumber = mt_rand(1000000000, 9999999999); + $timestamp = time(); + $clientId = $randomNumber . '.' . $timestamp; + } + $session->setData('ga_client_id', $clientId); } - + return $clientId; } diff --git a/src/app/code/community/Hirale/GAMeasurementProtocol/Model/Api.php b/src/app/code/community/Hirale/GAMeasurementProtocol/Model/Api.php index 1c47bf7..12fdf69 100644 --- a/src/app/code/community/Hirale/GAMeasurementProtocol/Model/Api.php +++ b/src/app/code/community/Hirale/GAMeasurementProtocol/Model/Api.php @@ -10,7 +10,7 @@ public function __construct() public function handle($data) { - $event = $data['data']['event']; + $event = $data['data']; $url = $this->helper->getMeasurementProtocolUrl(); $measurementId = $this->helper->getMeasurementId(); $apiSecret = $this->helper->getApiSecret(); @@ -26,10 +26,9 @@ public function handle($data) curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); - $response = curl_exec($ch); + curl_exec($ch); if ($this->helper->isDebugMode()) { Mage::log($payload); - Mage::log($response); } if (curl_errno($ch)) { throw new Exception(curl_error($ch)); diff --git a/src/app/code/community/Hirale/GAMeasurementProtocol/Model/Observer.php b/src/app/code/community/Hirale/GAMeasurementProtocol/Model/Observer.php index 54d0507..41e8769 100644 --- a/src/app/code/community/Hirale/GAMeasurementProtocol/Model/Observer.php +++ b/src/app/code/community/Hirale/GAMeasurementProtocol/Model/Observer.php @@ -8,6 +8,7 @@ class Hirale_GAMeasurementProtocol_Model_Observer protected $baseEventData; + public function __construct() { $this->helper = Mage::helper('gameasurementprotocol'); @@ -15,17 +16,32 @@ public function __construct() $this->queue = Mage::getModel('hirale_queue/task'); } + public function generateClientId(Varien_Event_Observer $observer) + { + $this->helper->getClientId(); + } + + /** * Add a task to the queue for processing by the Hirale_GAMeasurementProtocol_Model_Api class. * - * @param array $event The name of the event to be processed. + * @param array $events The name of the event to be processed. */ - protected function addToQueue($event) + protected function addToQueue($events) { try { + foreach ($events['events'] as &$event) { + $params = &$event['params']; + if ($this->helper->isDebugMode()) { + $params['debug_mode'] = true; + } + $params['user_agent'] = Mage::helper('core/http')->getHttpUserAgent(); + $params['platform'] = Mage::helper('core/string')->cleanString(Mage::app()->getRequest()->getServer('HTTP_SEC_CH_UA_PLATFORM')); + } + $this->queue->addTask( 'Hirale_GAMeasurementProtocol_Model_Api', - compact('event') + $events ); } catch (Exception $e) { Mage::logException($e); @@ -37,9 +53,13 @@ protected function getBaseEventData() if (!$this->baseEventData) { $this->baseEventData = [ 'client_id' => $this->helper->getClientId(), - "timestamp_micros" => floor(microtime(true) * 1000000), - "non_personalized_ads" => false + 'timestamp_micros' => floor(microtime(true) * 1000000), + 'non_personalized_ads' => true ]; + if (Mage::getSingleton('customer/session')->isLoggedIn()) { + $customer = Mage::getSingleton('customer/session')->getCustomer(); + $this->baseEventData['user_id'] = $customer->getId(); + } } return $this->baseEventData; } @@ -122,7 +142,7 @@ public function addToWishlist(Varien_Event_Observer $observer) $eventData = $this->getBaseEventData(); $value = 0; $currency = Mage::app()->getStore()->getBaseCurrencyCode(); - $newItems =[]; + $newItems = []; foreach ($items as $item) { $_product = $item->getProduct(); $_price = $_product->getFinalPrice(); @@ -176,11 +196,6 @@ public function dispatchRouteEvent(Varien_Event_Observer $observer) $route = $request->getModuleName() . '_' . $request->getControllerName() . '_' . $request->getActionName(); $eventData = $this->getBaseEventData(); - if (Mage::getSingleton('customer/session')->isLoggedIn()) { - $customer = Mage::getSingleton('customer/session')->getCustomer(); - $eventData['user_id'] = $customer->getId(); - } - $events = []; switch ($route) { case 'checkout_onepage_index': diff --git a/src/app/code/community/Hirale/GAMeasurementProtocol/etc/config.xml b/src/app/code/community/Hirale/GAMeasurementProtocol/etc/config.xml index 4239a50..6c35d0e 100644 --- a/src/app/code/community/Hirale/GAMeasurementProtocol/etc/config.xml +++ b/src/app/code/community/Hirale/GAMeasurementProtocol/etc/config.xml @@ -19,6 +19,15 @@ + + + + singleton + gameasurementprotocol/observer + generateClientId + + + diff --git a/src/app/code/community/Hirale/GAMeasurementProtocol/etc/system.xml b/src/app/code/community/Hirale/GAMeasurementProtocol/etc/system.xml index 8da598f..967778e 100644 --- a/src/app/code/community/Hirale/GAMeasurementProtocol/etc/system.xml +++ b/src/app/code/community/Hirale/GAMeasurementProtocol/etc/system.xml @@ -52,7 +52,7 @@ 1 - When in debug mode, all requests will be logged and sent to debug endpoint, check system.log for more details. + When in debug mode, all requests will be logged in the system log, you can check debug view (https://support.google.com/analytics/answer/7201382) for more info.