Skip to content

Commit 227ac72

Browse files
authored
Merge pull request #116 from Progi1984/issue28004
Moved some code from footer to head tag
2 parents 90afb05 + e23a6ce commit 227ac72

11 files changed

Lines changed: 88 additions & 96 deletions

classes/Database/Install.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ public function registerHooks()
101101
return $this->module->registerHook('displayHeader') &&
102102
$this->module->registerHook('displayAdminOrder') &&
103103
$this->module->registerHook('displayFooter') &&
104-
$this->module->registerHook('displayHome') &&
105104
$this->module->registerHook('displayFooterProduct') &&
106105
$this->module->registerHook('displayOrderConfirmation') &&
107106
$this->module->registerHook('actionProductCancel') &&

classes/Hook/HookDisplayHeader.php

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,31 @@
2020

2121
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
2222

23+
use Category;
2324
use Configuration;
2425
use Context;
26+
use Customer;
27+
use PrestaShop\Module\Ps_Googleanalytics\GoogleAnalyticsTools;
28+
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler;
29+
use PrestaShop\Module\Ps_Googleanalytics\Handler\ModuleHandler;
30+
use PrestaShop\Module\Ps_Googleanalytics\Wrapper\ProductWrapper;
2531
use Ps_Googleanalytics;
2632
use Shop;
2733
use Tools;
2834

2935
class HookDisplayHeader implements HookInterface
3036
{
37+
/**
38+
* @var Ps_Googleanalytics
39+
*/
3140
private $module;
41+
/**
42+
* @var Context
43+
*/
3244
private $context;
45+
/**
46+
* @var mixed
47+
*/
3348
private $params;
3449

3550
/**
@@ -44,9 +59,7 @@ public function __construct(Ps_Googleanalytics $module, Context $context)
4459
}
4560

4661
/**
47-
* run
48-
*
49-
* @return void|string
62+
* @return false|string
5063
*/
5164
public function run()
5265
{
@@ -62,8 +75,9 @@ public function run()
6275
$userId = null;
6376
$gaCrossdomainEnabled = false;
6477

65-
if (Configuration::get('GA_USERID_ENABLED') &&
66-
$this->context->customer && $this->context->customer->isLogged()
78+
if (Configuration::get('GA_USERID_ENABLED')
79+
&& $this->context->customer instanceof Customer
80+
&& $this->context->customer->isLogged()
6781
) {
6882
$userId = (int) $this->context->customer->id;
6983
}
@@ -91,6 +105,38 @@ public function run()
91105
return $this->module->display(
92106
$this->module->getLocalPath() . $this->module->name,
93107
'ps_googleanalytics.tpl'
108+
) . $this->displayGaTag();
109+
}
110+
111+
private function displayGaTag()
112+
{
113+
$moduleHandler = new ModuleHandler();
114+
$gaTools = new GoogleAnalyticsTools();
115+
$gaTagHandler = new GanalyticsJsHandler($this->module, $this->context);
116+
$gaScripts = '';
117+
118+
// Home featured products
119+
if ($moduleHandler->isModuleEnabledAndHookedOn('ps_featuredproducts', 'displayHome')
120+
&& $this->context->customer instanceof Customer) {
121+
$category = new Category($this->context->shop->getCategory(), $this->context->language->id);
122+
$productWrapper = new ProductWrapper($this->context);
123+
$homeFeaturedProducts = $productWrapper->wrapProductList(
124+
$category->getProducts(
125+
(int) $this->context->language->id,
126+
1,
127+
(Configuration::get('HOME_FEATURED_NBR') ? (int) Configuration::get('HOME_FEATURED_NBR') : 8),
128+
'position'
129+
),
130+
[],
131+
true
132+
);
133+
$gaScripts .= $gaTools->addProductImpression($homeFeaturedProducts) . $gaTools->addProductClick($homeFeaturedProducts);
134+
}
135+
136+
$this->module->js_state = 1;
137+
138+
return $gaTagHandler->generate(
139+
$gaTools->filter($gaScripts, $this->module->filterable)
94140
);
95141
}
96142

@@ -101,7 +147,7 @@ public function run()
101147
*/
102148
public function setParams($params)
103149
{
104-
$this->module->params = $params;
150+
$this->params = $params;
105151
}
106152

107153
/**

classes/Hook/HookDisplayHome.php

Lines changed: 0 additions & 78 deletions
This file was deleted.

ps_googleanalytics.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,6 @@ public function hookDisplayFooter()
116116
return $hook->run();
117117
}
118118

119-
/**
120-
* Homepage hook.
121-
* This function is run to manage analytics for product list associated to home featured, news products and best sellers Modules
122-
*/
123-
public function hookDisplayHome()
124-
{
125-
$hook = new PrestaShop\Module\Ps_Googleanalytics\Hooks\HookDisplayHome($this, $this->context);
126-
127-
return $hook->run();
128-
}
129-
130119
/**
131120
* Product page footer hook
132121
* This function is run to load JS for product details view

tests/phpstan/phpstan-1.7.1.2.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ includes:
44
parameters:
55
ignoreErrors:
66
- '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#'
7+
- '#Parameter \#1 \$idCategory of class Category constructor expects null, int given.#'

tests/phpstan/phpstan-1.7.2.5.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ includes:
44
parameters:
55
ignoreErrors:
66
- '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#'
7+
- '#Parameter \#1 \$idCategory of class Category constructor expects null, int given.#'

tests/phpstan/phpstan-1.7.3.4.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ includes:
44
parameters:
55
ignoreErrors:
66
- '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#'
7+
- '#Parameter \#1 \$idCategory of class Category constructor expects null, int given.#'

tests/phpstan/phpstan-1.7.4.4.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ includes:
44
parameters:
55
ignoreErrors:
66
- '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#'
7+
- '#Parameter \#1 \$idCategory of class Category constructor expects null, int given.#'

tests/phpstan/phpstan-1.7.5.1.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ includes:
44
parameters:
55
ignoreErrors:
66
- '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#'
7+
- '#Parameter \#1 \$idCategory of class Category constructor expects null, int given.#'

tests/phpstan/phpstan-1.7.6.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ includes:
44
parameters:
55
ignoreErrors:
66
- '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#'
7+
- '#Parameter \#1 \$idCategory of class Category constructor expects null, int given.#'

0 commit comments

Comments
 (0)