Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved some code from footer to head tag #116

Merged
merged 1 commit into from Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
58 changes: 52 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,38 @@ 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')
&& $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)
);
}

Expand All @@ -101,7 +147,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');
}