Skip to content

Commit 4a4ba83

Browse files
authored
Merge branch 'main' into 10738-Third-Party-Google-Analytics-and-AdWords
2 parents 4326b67 + 4d9179e commit 4a4ba83

File tree

15 files changed

+397
-111
lines changed

15 files changed

+397
-111
lines changed

Block/AbstractDataLayer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ protected function _toHtml(): string
5252
if ($this->config->isEnabled()) {
5353
$dataLayer = $this->getDataLayer();
5454
if ($dataLayer) {
55+
$json = json_encode($dataLayer);
56+
$json = str_replace('"getMfGtmCustomerIdentifier()"', 'getMfGtmCustomerIdentifier()', $json);
5557
//style always should be displayed none, since some sliders add a class that makes the script display flex/block
5658
return '<script style="display: none;">
5759
window.dataLayer = window.dataLayer || [];
58-
window.dataLayer.push(' . json_encode($dataLayer) . ');
60+
window.dataLayer.push(' . $json . ');
5961
</script>';
6062
}
6163
}

Model/AbstractDataLayer.php

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,36 @@ public function __construct(
106106
*/
107107
public function getEcommPageType(): string
108108
{
109+
if ('other' === $this->ecommPageType) {
110+
$fullActionName = $this->request->getFullActionName();
111+
switch ($fullActionName) {
112+
case 'cms_index_index':
113+
$this->ecommPageType = 'home';
114+
break;
115+
case 'catalog_category_view':
116+
$this->ecommPageType = 'category';
117+
break;
118+
case 'catalog_product_view':
119+
$this->ecommPageType = 'product';
120+
break;
121+
case 'checkout_cart_index':
122+
$this->ecommPageType = 'cart';
123+
break;
124+
case 'checkout_index_index':
125+
$this->ecommPageType = 'checkout';
126+
break;
127+
case 'contact_index_index':
128+
$this->ecommPageType = 'contact';
129+
break;
130+
case 'catalogsearch_result_index':
131+
$this->ecommPageType = 'searchresults';
132+
break;
133+
case 'cms_page_view':
134+
$this->ecommPageType = 'cmspage';
135+
break;
136+
}
137+
}
138+
109139
return $this->ecommPageType;
110140
}
111141

@@ -220,7 +250,7 @@ protected function formatPrice(float $price): float
220250

221251
/**
222252
* Get product price
223-
*
253+
* @deprecated
224254
* @param Product $product
225255
* @return float
226256
*/
@@ -231,6 +261,22 @@ protected function getPrice(Product $product): float
231261
return $this->formatPrice($price);
232262
}
233263

264+
/**
265+
* @param $product
266+
* @return float
267+
*/
268+
protected function getProductValue($product): float
269+
{
270+
$priceInfo = $product->getPriceInfo()->getPrice('final_price')->getAmount();
271+
if (!$this->config->isPurchaseTaxEnabled()) {
272+
$value = $priceInfo->getValue('tax');
273+
} else {
274+
$value = $priceInfo->getValue();
275+
}
276+
277+
return $this->formatPrice($value);
278+
}
279+
234280
/**
235281
* @param Product $product
236282
* @param string $attributeCode
@@ -292,6 +338,7 @@ protected function eventWrap(array $data): array
292338
$data = $this->addCustomerGroup($data);
293339
$data = $this->addMfUniqueEventId($data);
294340
$data = $this->addEcommPageType($data);
341+
$data = $this->addCustomerIdentifier($data);
295342

296343
return $data;
297344
}
@@ -332,4 +379,16 @@ protected function addEcommPageType(array $data): array
332379

333380
return $data;
334381
}
382+
383+
/**
384+
* @param array $data
385+
* @return array
386+
*/
387+
protected function addCustomerIdentifier(array $data): array
388+
{
389+
if (empty($data['customer_identifier'])) {
390+
$data['customer_identifier'] = 'getMfGtmCustomerIdentifier()';
391+
}
392+
return $data;
393+
}
335394
}

Model/DataLayer/AbstractOrder.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magefan\GoogleTagManager\Model\DataLayer;
10+
11+
use Magefan\GoogleTagManager\Model\AbstractDataLayer;
12+
use Magefan\GoogleTagManager\Model\Config;
13+
use Magento\Catalog\Api\CategoryRepositoryInterface;
14+
use Magento\Sales\Model\Order;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
use Magefan\GoogleTagManager\Api\DataLayer\Order\ItemInterface;
17+
18+
abstract class AbstractOrder extends AbstractDataLayer
19+
{
20+
/**
21+
* @var ItemInterface
22+
*/
23+
private $gtmItem;
24+
25+
/**
26+
* Purchase constructor.
27+
*
28+
* @param Config $config
29+
* @param StoreManagerInterface $storeManager
30+
* @param CategoryRepositoryInterface $categoryRepository
31+
* @param ItemInterface $gtmItem
32+
*/
33+
public function __construct(
34+
Config $config,
35+
StoreManagerInterface $storeManager,
36+
CategoryRepositoryInterface $categoryRepository,
37+
ItemInterface $gtmItem
38+
) {
39+
$this->gtmItem = $gtmItem;
40+
parent::__construct($config, $storeManager, $categoryRepository);
41+
}
42+
43+
/**
44+
* @inheritDoc
45+
*/
46+
public function get(Order $order, string $requester = ''): array
47+
{
48+
if ($order) {
49+
$items = [];
50+
foreach ($order->getAllVisibleItems() as $item) {
51+
$items[] = $this->gtmItem->get($item);
52+
}
53+
54+
return $this->eventWrap([
55+
'event' => $this->getEventName(),
56+
'ecommerce' => [
57+
'transaction_id' => $order->getIncrementId(),
58+
'value' => $this->getValue($order),
59+
'tax' => $this->formatPrice((float)$order->getTaxAmount()),
60+
'shipping' => $this->formatPrice((float)$order->getShippingAmount()),
61+
'currency' => $this->getCurrentCurrencyCode(),
62+
'coupon' => $order->getCouponCode() ?: '',
63+
'items' => $items
64+
],
65+
'is_virtual' => (bool)$order->getIsVirtual(),
66+
'shipping_description' => $order->getShippingDescription(),
67+
'customer_is_guest' => (bool)$order->getCustomerIsGuest(),
68+
'customer_identifier' => hash('sha256', (string)$order->getCustomerEmail()),
69+
]);
70+
}
71+
72+
return [];
73+
}
74+
75+
/**
76+
* @param Order $order
77+
* @return float
78+
*/
79+
protected function getValue(Order $order): float
80+
{
81+
$orderValue = (float)$order->getGrandTotal();
82+
83+
if (!$this->config->isPurchaseTaxEnabled()) {
84+
$orderValue -= $order->getTaxAmount();
85+
}
86+
87+
if (!$this->config->isPurchaseShippingEnabled()) {
88+
$orderValue -= $order->getShippingAmount();
89+
}
90+
91+
return $this->formatPrice($orderValue);
92+
}
93+
94+
/**
95+
* @return string
96+
*/
97+
abstract protected function getEventName(): string;
98+
}

Model/DataLayer/BeginCheckout.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818

1919
class BeginCheckout extends AbstractDataLayer implements BeginCheckoutInterface
2020
{
21+
/**
22+
* @var string
23+
*/
24+
protected $ecommPageType = 'checkout';
25+
2126
/**
2227
* @var ItemInterface
2328
*/
@@ -47,19 +52,23 @@ public function __construct(
4752
public function get(Quote $quote): array
4853
{
4954
$items = [];
55+
$value = 0;
5056

51-
foreach ($quote->getAllVisibleItems() as $item) {
52-
$items[] = $this->gtmItem->get($item);
57+
foreach ($quote->getAllVisibleItems() as $quoteItem) {
58+
$item = $this->gtmItem->get($quoteItem);
59+
$items[] = $item;
60+
$value += $item['price'] * $item['quantity'];
5361
}
5462

5563
return $this->eventWrap([
5664
'event' => 'begin_checkout',
5765
'ecommerce' => [
5866
'currency' => $this->getCurrentCurrencyCode(),
59-
'value' => $this->formatPrice((float)$quote->getGrandTotal()),
67+
'value' => $this->formatPrice($value),
6068
'coupon' => $quote->getCouponCode() ?: '',
6169
'items' => $items
62-
]
70+
],
71+
'customer_identifier' => $quote->getCustomerEmail() ? hash('sha256', (string)$quote->getCustomerEmail()) : ''
6372
]);
6473
}
6574
}

Model/DataLayer/Cart/Item.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010

1111
use Magefan\GoogleTagManager\Api\DataLayer\Cart\ItemInterface;
1212
use Magefan\GoogleTagManager\Model\AbstractDataLayer;
13+
use Magento\Quote\Model\Quote\Item as QuoteItem;
1314

1415
class Item extends AbstractDataLayer implements ItemInterface
1516
{
1617
/**
1718
* @inheritDoc
1819
*/
19-
public function get(\Magento\Quote\Model\Quote\Item $quoteItem): array
20+
public function get(QuoteItem $quoteItem): array
2021
{
2122
$product = $quoteItem->getProduct();
2223
$categoryNames = $this->getCategoryNames($product);
@@ -26,9 +27,30 @@ public function get(\Magento\Quote\Model\Quote\Item $quoteItem): array
2627
: $this->getProductAttributeValue($product, $this->config->getProductAttribute()),
2728
'item_name' => $quoteItem->getName(),
2829
'discount' => $this->formatPrice((float)$quoteItem->getDiscountAmount()),
30+
'coupon_code' => $quoteItem->getQuote()->getCouponCode() ?: '',
2931
'item_brand' => $this->getProductAttributeValue($product, $this->config->getBrandAttribute()),
30-
'price' => $this->formatPrice((float)$quoteItem->getPriceInclTax()),
32+
'price' => $this->getValue($quoteItem),
3133
'quantity' => $quoteItem->getQty() * 1
3234
]), $categoryNames);
3335
}
36+
37+
/**
38+
* @param $quoteItem
39+
* @return float
40+
*/
41+
protected function getValue(QuoteItem $quoteItem): float
42+
{
43+
if ($this->config->isPurchaseTaxEnabled()) {
44+
$value = (float)$quoteItem->getPriceInclTax();
45+
} else {
46+
$value = (float)$quoteItem->getPrice();
47+
}
48+
49+
//fix for magento 2.3.2 - module-quote/Model/Quote/Item/Processor.php prepareItem does not set price to quote item
50+
if (!$value && ($quoteItemProduct = $quoteItem->getProduct())) {
51+
return $this->getProductValue($quoteItemProduct);
52+
} else {
53+
return $this->formatPrice($value);
54+
}
55+
}
3456
}

Model/DataLayer/Order/Item.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,23 @@ public function get(OrderItemInterface $orderItem): array
2828
'item_name' => $orderItem->getName(),
2929
'discount' => $this->formatPrice((float)$orderItem->getDiscountAmount()),
3030
'item_brand' => $this->getProductAttributeValue($product, $this->config->getBrandAttribute()),
31-
'price' => $this->formatPrice((float)$orderItem->getPriceInclTax()),
31+
'price' => $this->getValue($orderItem),
3232
'quantity' => $orderItem->getQtyOrdered() * 1
3333
]), $categoryNames);
3434
}
35+
36+
/**
37+
* @param $quoteItem
38+
* @return float
39+
*/
40+
protected function getValue(OrderItemInterface $orderItem): float
41+
{
42+
if ($this->config->isPurchaseTaxEnabled()) {
43+
$value = (float)$orderItem->getPriceInclTax();
44+
} else {
45+
$value = (float)$orderItem->getPrice();
46+
}
47+
48+
return $this->formatPrice($value);
49+
}
3550
}

Model/DataLayer/Product/Item.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function get(Product $product): array
2424
'item_id' => $this->getProductAttributeValue($product, $this->config->getProductAttribute()),
2525
'item_name' => $product->getName(),
2626
'item_brand' => $this->getProductAttributeValue($product, $this->config->getBrandAttribute()),
27-
'price' => $this->getPrice($product)
27+
'price' => $this->getProductValue($product)
2828
]), $categoryNames);
2929
}
3030
}

0 commit comments

Comments
 (0)