Skip to content

Commit

Permalink
Moved some code from footer to head tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Apr 4, 2022
1 parent 90afb05 commit 95e8092
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 96 deletions.
1 change: 0 additions & 1 deletion classes/Database/Install.php
Expand Up @@ -101,7 +101,6 @@ public function registerHooks()
return $this->module->registerHook('displayHeader') &&
$this->module->registerHook('displayAdminOrder') &&
$this->module->registerHook('displayFooter') &&
$this->module->registerHook('displayHome') &&
$this->module->registerHook('displayFooterProduct') &&
$this->module->registerHook('displayOrderConfirmation') &&
$this->module->registerHook('actionProductCancel') &&
Expand Down
57 changes: 51 additions & 6 deletions classes/Hook/HookDisplayHeader.php
Expand Up @@ -20,16 +20,31 @@

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;

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

/**
* run
*
* @return void|string
* @return false|string
*/
public function run()
{
Expand All @@ -62,8 +75,9 @@ public function run()
$userId = null;
$gaCrossdomainEnabled = false;

if (Configuration::get('GA_USERID_ENABLED') &&
$this->context->customer && $this->context->customer->isLogged()
if (Configuration::get('GA_USERID_ENABLED')
&& $this->context->customer instanceof Customer
&& $this->context->customer->isLogged()
) {
$userId = (int) $this->context->customer->id;
}
Expand Down Expand Up @@ -91,6 +105,37 @@ public function run()
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')) {
$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)
);
}

Expand All @@ -101,7 +146,7 @@ public function run()
*/
public function setParams($params)
{
$this->module->params = $params;
$this->params = $params;
}

/**
Expand Down
78 changes: 0 additions & 78 deletions classes/Hook/HookDisplayHome.php

This file was deleted.

11 changes: 0 additions & 11 deletions ps_googleanalytics.php
Expand Up @@ -116,17 +116,6 @@ public function hookDisplayFooter()
return $hook->run();
}

/**
* Homepage hook.
* This function is run to manage analytics for product list associated to home featured, news products and best sellers Modules
*/
public function hookDisplayHome()
{
$hook = new PrestaShop\Module\Ps_Googleanalytics\Hooks\HookDisplayHome($this, $this->context);

return $hook->run();
}

/**
* Product page footer hook
* This function is run to load JS for product details view
Expand Down
1 change: 1 addition & 0 deletions tests/phpstan/phpstan-1.7.1.2.neon
Expand Up @@ -4,3 +4,4 @@ includes:
parameters:
ignoreErrors:
- '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#'
- '#Parameter \#1 \$idCategory of class Category constructor expects null, int given.#'
1 change: 1 addition & 0 deletions tests/phpstan/phpstan-1.7.2.5.neon
Expand Up @@ -4,3 +4,4 @@ includes:
parameters:
ignoreErrors:
- '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#'
- '#Parameter \#1 \$idCategory of class Category constructor expects null, int given.#'
1 change: 1 addition & 0 deletions tests/phpstan/phpstan-1.7.3.4.neon
Expand Up @@ -4,3 +4,4 @@ includes:
parameters:
ignoreErrors:
- '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#'
- '#Parameter \#1 \$idCategory of class Category constructor expects null, int given.#'
1 change: 1 addition & 0 deletions tests/phpstan/phpstan-1.7.4.4.neon
Expand Up @@ -4,3 +4,4 @@ includes:
parameters:
ignoreErrors:
- '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#'
- '#Parameter \#1 \$idCategory of class Category constructor expects null, int given.#'
1 change: 1 addition & 0 deletions tests/phpstan/phpstan-1.7.5.1.neon
Expand Up @@ -4,3 +4,4 @@ includes:
parameters:
ignoreErrors:
- '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#'
- '#Parameter \#1 \$idCategory of class Category constructor expects null, int given.#'
1 change: 1 addition & 0 deletions tests/phpstan/phpstan-1.7.6.neon
Expand Up @@ -4,3 +4,4 @@ includes:
parameters:
ignoreErrors:
- '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#'
- '#Parameter \#1 \$idCategory of class Category constructor expects null, int given.#'
30 changes: 30 additions & 0 deletions upgrade/upgrade-4.1.3.php
@@ -0,0 +1,30 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* 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 and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
if (!defined('_PS_VERSION_')) {
exit;
}

/**
* @param Ps_Googleanalytics $object
*/
function upgrade_module_4_1_3($object)
{
return $object->unregisterHook('displayHome');
}

0 comments on commit 95e8092

Please sign in to comment.