Skip to content

Commit 3b0f2a3

Browse files
AC-2574: Commerce Integrated Google Modules Upgrade - Added new module Google Gtag
1 parent ac0c588 commit 3b0f2a3

File tree

39 files changed

+2040
-17
lines changed

39 files changed

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

0 commit comments

Comments
 (0)