Skip to content

Commit 0659f91

Browse files
authored
Merge branch 'main' into 8567-GTM-with-default-magento-cookie
2 parents 3c5e3bc + 998444d commit 0659f91

File tree

6 files changed

+91
-11
lines changed

6 files changed

+91
-11
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
namespace Magefan\GoogleTagManager\Block\Adminhtml\System\Config\Form;
8+
9+
use Magento\Framework\Data\Form\Element\AbstractElement;
10+
11+
/**
12+
* Admin configurations information block
13+
*/
14+
class Attention extends \Magefan\Community\Block\Adminhtml\System\Config\Form\Info
15+
{
16+
17+
/**
18+
* Return info block html
19+
*
20+
* @param \Magento\Framework\Data\Form\Element\AbstractElement $element
21+
* @return string
22+
*/
23+
public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
24+
{
25+
return '<div style="padding:10px;background-color:#ffe5e5;border:1px solid #ddd;margin-bottom:7px;">
26+
<strong>Attention!</strong> Once you made and saved all the configurations,
27+
please don\'t forget to scroll down to the "Export Container" section
28+
and click the "Generate JSON Container & Download File" button to export container data.
29+
After you save the file, <a target="_blank" title="Create GTM tags" href="https://magefan.com/blog/add-google-tag-manager-to-magento-2#5-create-gtm-tags">
30+
import it to your Google Tag Manager container.</a>
31+
</div>';
32+
}
33+
}
34+

Model/AbstractDataLayer.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Magento\Catalog\Model\Product;
1515
use Magento\Framework\Exception\NoSuchEntityException;
1616
use Magento\Store\Model\StoreManagerInterface;
17+
use Magento\Framework\App\RequestInterface;
18+
use Magento\Framework\Registry;
19+
use Magento\Framework\App\ObjectManager;
1720

1821
class AbstractDataLayer
1922
{
@@ -32,6 +35,16 @@ class AbstractDataLayer
3235
*/
3336
protected $categoryRepository;
3437

38+
/**
39+
* @var RequestInterface
40+
*/
41+
protected $request;
42+
43+
/**
44+
* @var Registry
45+
*/
46+
protected $registry;
47+
3548
/**
3649
* AbstractDataLayer constructor.
3750
*
@@ -42,11 +55,19 @@ class AbstractDataLayer
4255
public function __construct(
4356
Config $config,
4457
StoreManagerInterface $storeManager,
45-
CategoryRepositoryInterface $categoryRepository
58+
CategoryRepositoryInterface $categoryRepository,
59+
RequestInterface $request = null,
60+
Registry $registry = null
4661
) {
4762
$this->config = $config;
4863
$this->storeManager = $storeManager;
4964
$this->categoryRepository = $categoryRepository;
65+
$this->request = $request ?: ObjectManager::getInstance()->get(
66+
RequestInterface::class
67+
);
68+
$this->registry = $registry ?: ObjectManager::getInstance()->get(
69+
Registry::class
70+
);
5071
}
5172

5273
/**
@@ -58,13 +79,12 @@ public function __construct(
5879
*/
5980
protected function getCategoryNames(Product $product): array
6081
{
61-
/* Temporary disable displaying category names,
62-
* as it is optional and slows down the website speed
63-
*/
64-
return [];
65-
6682
$result = [];
6783

84+
if (!$this->config->getCategoriesAttribute()) {
85+
return $result;
86+
}
87+
6888
if ($productCategory = $this->getCategoryByProduct($product)) {
6989
$categoryIds = $productCategory->getPathIds();
7090
$number = 1;
@@ -91,8 +111,13 @@ protected function getCategoryNames(Product $product): array
91111
*/
92112
private function getCategoryByProduct(Product $product): ?CategoryInterface
93113
{
94-
$productCategory = null;
114+
if ('catalog_category_product' == $this->request->getFullActionName()) {
115+
if ($category = $this->registry->registry('current_category')) {
116+
return $category;
117+
}
118+
}
95119

120+
$productCategory = null;
96121
$categoryIds = $product->getCategoryIds();
97122

98123
if ($categoryIds) {

Model/Config.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Config
3333
*/
3434
public const XML_PATH_ATTRIBUTES_PRODUCT = 'mfgoogletagmanager/attributes/product';
3535
public const XML_PATH_ATTRIBUTES_BRAND = 'mfgoogletagmanager/attributes/brand';
36+
public const XML_PATH_ATTRIBUTES_CATEGORIES = 'mfgoogletagmanager/attributes/categories';
3637

3738
/**
3839
* Customer data protection regulation config
@@ -75,7 +76,7 @@ public function isEnabled(string $storeId = null): bool
7576
*/
7677
public function getAccountId(string $storeId = null): string
7778
{
78-
return (string)$this->getConfig(self::XML_PATH_ACCOUNT_ID, $storeId);
79+
return trim((string)$this->getConfig(self::XML_PATH_ACCOUNT_ID, $storeId));
7980
}
8081

8182
/**
@@ -86,7 +87,7 @@ public function getAccountId(string $storeId = null): string
8687
*/
8788
public function getContainerId(string $storeId = null): string
8889
{
89-
return (string)$this->getConfig(self::XML_PATH_CONTAINER_ID, $storeId);
90+
return trim((string)$this->getConfig(self::XML_PATH_CONTAINER_ID, $storeId));
9091
}
9192

9293
/**
@@ -97,7 +98,7 @@ public function getContainerId(string $storeId = null): string
9798
*/
9899
public function getPublicId(string $storeId = null): string
99100
{
100-
return (string)$this->getConfig(self::XML_PATH_PUBLIC_ID, $storeId);
101+
return trim((string)$this->getConfig(self::XML_PATH_PUBLIC_ID, $storeId));
101102
}
102103

103104
/**
@@ -119,7 +120,7 @@ public function isAnalyticsEnabled(string $storeId = null): bool
119120
*/
120121
public function getMeasurementId(string $storeId = null): string
121122
{
122-
return (string)$this->getConfig(self::XML_PATH_ANALYTICS_MEASUREMENT_ID, $storeId);
123+
return trim((string)$this->getConfig(self::XML_PATH_ANALYTICS_MEASUREMENT_ID, $storeId));
123124
}
124125

125126
/**
@@ -167,6 +168,16 @@ public function isCookieRestrictionModeEnabled(string $storeId = null)
167168
return (bool)$this->getConfig(Custom::XML_PATH_WEB_COOKIE_RESTRICTION, $storeId);
168169
}
169170

171+
* Retrieve Magento product categories
172+
*
173+
* @param string|null $storeId
174+
* @return string
175+
*/
176+
public function getCategoriesAttribute(string $storeId = null): string
177+
{
178+
return (string)$this->getConfig(self::XML_PATH_ATTRIBUTES_CATEGORIES, $storeId);
179+
}
180+
170181
/**
171182
* Retrieve store config value
172183
*

etc/adminhtml/system.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
<field id="version" translate="label comment" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
1919
<frontend_model>Magefan\GoogleTagManager\Block\Adminhtml\System\Config\Form\Info</frontend_model>
2020
</field>
21+
<field id="attention" translate="label comment" type="text" sortOrder="9999" showInDefault="1" showInWebsite="1" showInStore="1">
22+
<frontend_model>Magefan\GoogleTagManager\Block\Adminhtml\System\Config\Form\Attention</frontend_model>
23+
</field>
2124
<field id="enabled" translate="label comment" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
2225
<label>Enable Extension</label>
2326
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
@@ -97,6 +100,11 @@ height=&quot;0&quot; width=&quot;0&quot; style=&quot;display:none;visibility:hid
97100
<label>Brand Identifier</label>
98101
<source_model>Magefan\GoogleTagManager\Model\Config\Source\BrandAttribute</source_model>
99102
</field>
103+
<field id="categories" translate="label comment" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
104+
<label>Include Categories</label>
105+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
106+
<comment><![CDATA[<strong class="colorRed">Warning!</strong> Enabling this option may cause the performance impact.]]></comment>
107+
</field>
100108
</group>
101109
<group id="container" translate="label" type="text" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="1">
102110
<label>Export Container</label>

etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<attributes>
2424
<product>sku</product>
2525
<brand/>
26+
<categories/>
2627
</attributes>
2728
<customer_data>
2829
<protect>1</protect>

view/adminhtml/templates/system/config/event/list.phtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ $eventsExtra = [
3333
'Sign In (login)',
3434
'Create an Account (sign_up)',
3535
'Search (search)',
36+
'Create New Order In Admin Panel (purchase)'
3637
/*
3738
'Earn Virtual Currency (earn_virtual_currency)',
3839
'Spend Virtual Currency (spend_virtual_currency)'

0 commit comments

Comments
 (0)