|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GoogleGtag\Block; |
| 9 | + |
| 10 | +use Magento\Cookie\Helper\Cookie; |
| 11 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 12 | +use Magento\Framework\Serialize\SerializerInterface; |
| 13 | +use Magento\Framework\View\Element\Template; |
| 14 | +use Magento\Framework\View\Element\Template\Context; |
| 15 | +use Magento\GoogleGtag\Model\Config\GtagConfig as GtagConfiguration; |
| 16 | +use Magento\Sales\Api\OrderRepositoryInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * GoogleAnalytics Page Block |
| 20 | + * |
| 21 | + * @api |
| 22 | + */ |
| 23 | +class Ga extends Template |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var GtagConfiguration |
| 27 | + */ |
| 28 | + private $googleGtagConfig; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var Cookie |
| 32 | + */ |
| 33 | + private $cookieHelper; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var SerializerInterface |
| 37 | + */ |
| 38 | + private $serializer; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var OrderRepositoryInterface |
| 42 | + */ |
| 43 | + private $orderRepository; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var SearchCriteriaBuilder |
| 47 | + */ |
| 48 | + private $searchCriteriaBuilder; |
| 49 | + |
| 50 | + /** |
| 51 | + * @param Context $context |
| 52 | + * @param GtagConfiguration $googleGtagConfig |
| 53 | + * @param Cookie $cookieHelper |
| 54 | + * @param SerializerInterface $serializer |
| 55 | + * @param SearchCriteriaBuilder $searchCriteriaBuilder |
| 56 | + * @param OrderRepositoryInterface $orderRepository |
| 57 | + * @param array $data |
| 58 | + */ |
| 59 | + public function __construct( |
| 60 | + Context $context, |
| 61 | + GtagConfiguration $googleGtagConfig, |
| 62 | + Cookie $cookieHelper, |
| 63 | + SerializerInterface $serializer, |
| 64 | + SearchCriteriaBuilder $searchCriteriaBuilder, |
| 65 | + OrderRepositoryInterface $orderRepository, |
| 66 | + array $data = [] |
| 67 | + ) { |
| 68 | + $this->googleGtagConfig = $googleGtagConfig; |
| 69 | + $this->cookieHelper = $cookieHelper; |
| 70 | + $this->serializer = $serializer; |
| 71 | + $this->orderRepository = $orderRepository; |
| 72 | + $this->searchCriteriaBuilder = $searchCriteriaBuilder; |
| 73 | + parent::__construct($context, $data); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get a specific page name (may be customized via layout) |
| 78 | + * |
| 79 | + * @return string|null |
| 80 | + */ |
| 81 | + public function getPageName(): ?string |
| 82 | + { |
| 83 | + return $this->_getData('page_name'); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Return cookie restriction mode value. |
| 88 | + * |
| 89 | + * @return bool |
| 90 | + */ |
| 91 | + public function isCookieRestrictionModeEnabled(): bool |
| 92 | + { |
| 93 | + return (bool) $this->cookieHelper->isCookieRestrictionModeEnabled(); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Return current website id. |
| 98 | + * |
| 99 | + * @return int |
| 100 | + */ |
| 101 | + public function getCurrentWebsiteId(): int |
| 102 | + { |
| 103 | + return (int) $this->_storeManager->getWebsite()->getId(); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Return information about page for GA tracking |
| 108 | + * |
| 109 | + * @link https://developers.google.com/analytics/devguides/collection/gtagjs |
| 110 | + * @link https://developers.google.com/analytics/devguides/collection/ga4 |
| 111 | + * |
| 112 | + * @param string $measurementId |
| 113 | + * @return array |
| 114 | + */ |
| 115 | + public function getPageTrackingData($measurementId): array |
| 116 | + { |
| 117 | + return [ |
| 118 | + 'optPageUrl' => $this->getOptPageUrl(), |
| 119 | + 'measurementId' => $this->escapeHtmlAttr($measurementId, false) |
| 120 | + ]; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Return information about order and items for GA tracking. |
| 125 | + * |
| 126 | + * @link https://developers.google.com/analytics/devguides/collection/ga4/ecommerce#purchase |
| 127 | + * @link https://developers.google.com/gtagjs/reference/ga4-events#purchase |
| 128 | + * @link https://developers.google.com/analytics/devguides/collection/gtagjs/enhanced-ecommerce#product-data |
| 129 | + * @link https://developers.google.com/gtagjs/reference/event#purchase |
| 130 | + * |
| 131 | + * @return array |
| 132 | + */ |
| 133 | + public function getOrdersTrackingData(): array |
| 134 | + { |
| 135 | + $result = []; |
| 136 | + $orderIds = $this->getOrderIds(); |
| 137 | + if (empty($orderIds) || !is_array($orderIds)) { |
| 138 | + return $result; |
| 139 | + } |
| 140 | + $this->searchCriteriaBuilder->addFilter( |
| 141 | + 'entity_id', |
| 142 | + $orderIds, |
| 143 | + 'in' |
| 144 | + ); |
| 145 | + $collection = $this->orderRepository->getList($this->searchCriteriaBuilder->create()); |
| 146 | + |
| 147 | + foreach ($collection->getItems() as $order) { |
| 148 | + foreach ($order->getAllVisibleItems() as $item) { |
| 149 | + $result['products'][] = [ |
| 150 | + 'item_id' => $this->escapeJsQuote($item->getSku()), |
| 151 | + 'item_name' => $this->escapeJsQuote($item->getName()), |
| 152 | + 'price' => number_format((float) $item->getPrice(), 2), |
| 153 | + 'quantity' => (int)$item->getQtyOrdered(), |
| 154 | + ]; |
| 155 | + } |
| 156 | + $result['orders'][] = [ |
| 157 | + 'transaction_id' => $order->getIncrementId(), |
| 158 | + 'affiliation' => $this->escapeJsQuote($this->_storeManager->getStore()->getFrontendName()), |
| 159 | + 'value' => number_format((float) $order->getGrandTotal(), 2), |
| 160 | + 'tax' => number_format((float) $order->getTaxAmount(), 2), |
| 161 | + 'shipping' => number_format((float) $order->getShippingAmount(), 2), |
| 162 | + ]; |
| 163 | + $result['currency'] = $order->getOrderCurrencyCode(); |
| 164 | + } |
| 165 | + return $result; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Return page url for tracking. |
| 170 | + * |
| 171 | + * @return string |
| 172 | + */ |
| 173 | + private function getOptPageUrl(): string |
| 174 | + { |
| 175 | + $optPageURL = ''; |
| 176 | + $pageName = $this->getPageName() !== null ? trim($this->getPageName()) : ''; |
| 177 | + if ($pageName && substr($pageName, 0, 1) === '/' && strlen($pageName) > 1) { |
| 178 | + $optPageURL = ", '" . $this->escapeHtmlAttr($pageName, false) . "'"; |
| 179 | + } |
| 180 | + return $optPageURL; |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Provide analytics events data |
| 185 | + * |
| 186 | + * @return bool|string |
| 187 | + */ |
| 188 | + public function getAnalyticsData() |
| 189 | + { |
| 190 | + $analyticData = [ |
| 191 | + 'isCookieRestrictionModeEnabled' => $this->isCookieRestrictionModeEnabled(), |
| 192 | + 'currentWebsite' => $this->getCurrentWebsiteId(), |
| 193 | + 'cookieName' => Cookie::IS_USER_ALLOWED_SAVE_COOKIE, |
| 194 | + 'pageTrackingData' => $this->getPageTrackingData($this->googleGtagConfig->getMeasurementId()), |
| 195 | + 'ordersTrackingData' => $this->getOrdersTrackingData(), |
| 196 | + 'googleAnalyticsAvailable' => $this->googleGtagConfig->isGoogleAnalyticsAvailable() |
| 197 | + ]; |
| 198 | + return $this->serializer->serialize($analyticData); |
| 199 | + } |
| 200 | +} |
0 commit comments