forked from PrestaShop/ps_googleanalytics
/
HookDisplayHeader.php
168 lines (148 loc) · 5.14 KB
/
HookDisplayHeader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
use Category;
use Configuration;
use Context;
use Customer;
use PrestaShop\Module\Ps_Googleanalytics\GoogleAnalyticsTools;
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler;
use PrestaShop\Module\Ps_Googleanalytics\Handler\ModuleHandler;
use PrestaShop\Module\Ps_Googleanalytics\Wrapper\ProductWrapper;
use Ps_Googleanalytics;
use Shop;
use Tools;
class HookDisplayHeader implements HookInterface
{
/**
* @var Ps_Googleanalytics
*/
private $module;
/**
* @var Context
*/
private $context;
/**
* @var mixed
*/
private $params;
/**
* @var bool
*/
private $backOffice;
public function __construct(Ps_Googleanalytics $module, Context $context)
{
$this->module = $module;
$this->context = $context;
}
/**
* @return false|string
*/
public function run()
{
if (!Configuration::get('GA_ACCOUNT_ID')) {
return '';
}
$this->context->controller->addJs($this->module->getPathUri() . 'views/js/GoogleAnalyticActionLib.js');
$shops = Shop::getShops();
$isMultistoreActive = Shop::isFeatureActive();
$currentShopId = (int) Context::getContext()->shop->id;
$userId = null;
$gaCrossdomainEnabled = false;
if (Configuration::get('GA_USERID_ENABLED')
&& $this->context->customer instanceof Customer
&& $this->context->customer->isLogged()
) {
$userId = (int) $this->context->customer->id;
}
$gaAnonymizeEnabled = Configuration::get('GA_ANONYMIZE_ENABLED');
if ((int) Configuration::get('GA_CROSSDOMAIN_ENABLED') && $isMultistoreActive && count($shops) > 1) {
$gaCrossdomainEnabled = true;
}
$this->context->smarty->assign(
[
'backOffice' => $this->backOffice,
'trackBackOffice' => Configuration::get('GA_TRACK_BACKOFFICE_ENABLED'),
'currentShopId' => $currentShopId,
'userId' => $userId,
'gaAccountId' => Tools::safeOutput(Configuration::get('GA_ACCOUNT_ID')),
'shops' => $shops,
'gaCrossdomainEnabled' => $gaCrossdomainEnabled,
'gaAnonymizeEnabled' => $gaAnonymizeEnabled,
'useSecureMode' => Configuration::get('PS_SSL_ENABLED'),
]
);
return $this->module->display(
$this->module->getLocalPath() . $this->module->name,
'ps_googleanalytics.tpl'
) . $this->displayGaTag();
}
private function displayGaTag()
{
$moduleHandler = new ModuleHandler();
$gaTools = new GoogleAnalyticsTools();
$gaTagHandler = new GanalyticsJsHandler($this->module, $this->context);
$gaScripts = '';
// Home featured products
if ($moduleHandler->isModuleEnabledAndHookedOn('ps_featuredproducts', 'displayHome')
&& $this->context->customer instanceof Customer) {
$category = new Category($this->context->shop->getCategory(), $this->context->language->id);
$productWrapper = new ProductWrapper($this->context);
$homeFeaturedProducts = $productWrapper->wrapProductList(
$category->getProducts(
(int) $this->context->language->id,
1,
(Configuration::get('HOME_FEATURED_NBR') ? (int) Configuration::get('HOME_FEATURED_NBR') : 8),
'position'
),
[],
true
);
$gaScripts .= $gaTools->addProductImpression($homeFeaturedProducts) . $gaTools->addProductClick($homeFeaturedProducts);
}
$this->module->js_state = 1;
return $gaTagHandler->generate(
$gaTools->filter($gaScripts, $this->module->filterable)
);
}
/**
* setParams
*
* @param array $params
*/
public function setParams($params)
{
$this->params = $params;
}
/**
* @param bool $backOffice
*/
public function setBackOffice($backOffice)
{
$this->acknowledgeBackOfficeContext($backOffice);
}
/**
* @param bool $isBackOffice
*/
public function acknowledgeBackOfficeContext($isBackOffice)
{
$this->backOffice = $isBackOffice;
}
}