From f36964d5315c88afd975554253c131d8096c011e Mon Sep 17 00:00:00 2001 From: Codencode Date: Tue, 17 Dec 2024 10:31:55 +0100 Subject: [PATCH 1/4] Added queue clearing JavaScript events considered: - updateCart - changedCheckoutStep - updatedProduct - updateProductList --- controllers/front/ajax.php | 14 +++++++++++ ps_googleanalytics.php | 7 ++++++ views/js/ganalytics.js | 50 ++++++++++++++++++++++++++++++++++++++ views/js/index.php | 28 +++++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 views/js/ganalytics.js create mode 100644 views/js/index.php diff --git a/controllers/front/ajax.php b/controllers/front/ajax.php index c77698e..0076554 100644 --- a/controllers/front/ajax.php +++ b/controllers/front/ajax.php @@ -24,6 +24,11 @@ class ps_GoogleanalyticsAjaxModuleFrontController extends ModuleFrontController { public $ssl = true; + /** + * @var Ps_Googleanalytics + */ + public $module; + /* * @see FrontController::initContent() */ @@ -31,6 +36,15 @@ public function initContent() { parent::initContent(); + if (Tools::getValue('action') == 'flushQueue') { + $output = $this->module->getDataHandler()->readData(); + + $this->module->getDataHandler()->deleteData(); + + $this->ajaxRender(json_encode($output)); + exit; + } + $orderId = (int) Tools::getValue('orderid'); $order = new Order($orderId); diff --git a/ps_googleanalytics.php b/ps_googleanalytics.php index af89082..e1df6a1 100644 --- a/ps_googleanalytics.php +++ b/ps_googleanalytics.php @@ -80,6 +80,13 @@ public function getContent() public function hookDisplayHeader($params, $back_office = false) { + if ($back_office === false) { + Media::addJsDef([ + 'psGoogleAnalyticsAjaxUrl' => $this->context->link->getModuleLink($this->name, 'ajax', [], true), + ]); + $this->context->controller->registerJavascript('modules-psgoogleanalytics', 'modules/' . $this->name . '/views/js/ganalytics.js'); + } + $hook = new PrestaShop\Module\Ps_Googleanalytics\Hooks\HookDisplayHeader($this, $this->context); $hook->setBackOffice($back_office); diff --git a/views/js/ganalytics.js b/views/js/ganalytics.js new file mode 100644 index 0000000..5082c56 --- /dev/null +++ b/views/js/ganalytics.js @@ -0,0 +1,50 @@ +/** + * 2007-2020 PrestaShop. + * + * 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. + * + * DISCLAIMER + * + * Do not edit or add to this file if you wish to upgrade PrestaShop to newer + * versions in the future. If you wish to customize PrestaShop for your + * needs please refer to http://www.prestashop.com for more information. + * + * @author PrestaShop SA + * @copyright 2007-2020 PrestaShop SA + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) + * International Registered Trademark & Property of PrestaShop SA + */ + +$(document).ready(function () { + prestashop.on('updateCart', flushQueue); + prestashop.on('changedCheckoutStep', flushQueue); + prestashop.on('clickQuickView', flushQueue); + prestashop.on('updatedProduct', flushQueue); + prestashop.on('updateProductList', flushQueue); + + function flushQueue() { + $.ajax({ + type: "POST", + url: psGoogleAnalyticsAjaxUrl, + dataType: "json", + data: { + ajax: true, + action: "flushQueue", + }, + success: function (response) { + response.forEach(code => { + const fn = new Function(code); + fn(); + }); + } + }); + } +}); \ No newline at end of file diff --git a/views/js/index.php b/views/js/index.php new file mode 100644 index 0000000..098efd9 --- /dev/null +++ b/views/js/index.php @@ -0,0 +1,28 @@ + + * @copyright Since 2007 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 + */ +header('Expires: Mon, 26 Jul 1998 05:00:00 GMT'); +header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); + +header('Cache-Control: no-store, no-cache, must-revalidate'); +header('Cache-Control: post-check=0, pre-check=0', false); +header('Pragma: no-cache'); + +header('Location: ../'); +exit; From b33ac2a2dd4e4673f1989369531dc0915550a7a6 Mon Sep 17 00:00:00 2001 From: Codencode Date: Tue, 17 Dec 2024 14:54:19 +0100 Subject: [PATCH 2/4] Update controllers/front/ajax.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Nicolas Lœuillet --- controllers/front/ajax.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/front/ajax.php b/controllers/front/ajax.php index 0076554..2fa455c 100644 --- a/controllers/front/ajax.php +++ b/controllers/front/ajax.php @@ -36,7 +36,7 @@ public function initContent() { parent::initContent(); - if (Tools::getValue('action') == 'flushQueue') { + if (Tools::getValue('action') === 'flushQueue') { $output = $this->module->getDataHandler()->readData(); $this->module->getDataHandler()->deleteData(); From 9943ce23acb1212c20cbcc753fa88e9b37faf285 Mon Sep 17 00:00:00 2001 From: Codencode Date: Thu, 16 Jan 2025 11:32:04 +0100 Subject: [PATCH 3/4] Update ajax.php --- controllers/front/ajax.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/controllers/front/ajax.php b/controllers/front/ajax.php index 2fa455c..f5b3e6a 100644 --- a/controllers/front/ajax.php +++ b/controllers/front/ajax.php @@ -24,11 +24,6 @@ class ps_GoogleanalyticsAjaxModuleFrontController extends ModuleFrontController { public $ssl = true; - /** - * @var Ps_Googleanalytics - */ - public $module; - /* * @see FrontController::initContent() */ From 482149113ede0f6d04bfa2f7cea2604420392919 Mon Sep 17 00:00:00 2001 From: Codencode Date: Tue, 13 May 2025 10:19:39 +0200 Subject: [PATCH 4/4] phpstan: added ignore rule for unrecognized getDataHandler() and ajaxRender() methods --- tests/php/phpstan/phpstan-1.7.7.neon | 2 ++ tests/php/phpstan/phpstan-1.7.8.neon | 1 + tests/php/phpstan/phpstan-8.0.neon | 1 + tests/php/phpstan/phpstan-8.1.7.neon | 3 ++- tests/php/phpstan/phpstan-8.2.x.neon | 3 ++- tests/php/phpstan/phpstan-9.0.x.neon | 3 ++- tests/php/phpstan/phpstan-9.1.x.neon | 3 ++- tests/php/phpstan/phpstan-develop.neon | 3 ++- tests/php/phpstan/phpstan-latest.neon | 1 + 9 files changed, 15 insertions(+), 5 deletions(-) diff --git a/tests/php/phpstan/phpstan-1.7.7.neon b/tests/php/phpstan/phpstan-1.7.7.neon index caca568..b9f9b15 100644 --- a/tests/php/phpstan/phpstan-1.7.7.neon +++ b/tests/php/phpstan/phpstan-1.7.7.neon @@ -7,3 +7,5 @@ parameters: - '#Access to an undefined property Cookie\:\:\$ga_admin_order.#' - '#Access to an undefined property Cookie\:\:\$ga_admin_refund.#' - '#Parameter \#1 \$value of method ControllerCore::ajaxRender\(\) expects null, string given.#' + - '#Parameter \#1 \$value of method ControllerCore::ajaxRender\(\) expects null, string\|false given.#' + - '#Call to an undefined method Module::getDataHandler\(\).#' diff --git a/tests/php/phpstan/phpstan-1.7.8.neon b/tests/php/phpstan/phpstan-1.7.8.neon index 364a960..382b8c1 100644 --- a/tests/php/phpstan/phpstan-1.7.8.neon +++ b/tests/php/phpstan/phpstan-1.7.8.neon @@ -6,3 +6,4 @@ parameters: - '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#' - '#Access to an undefined property Cookie\:\:\$ga_admin_order.#' - '#Access to an undefined property Cookie\:\:\$ga_admin_refund.#' + - '#Call to an undefined method Module::getDataHandler\(\).#' diff --git a/tests/php/phpstan/phpstan-8.0.neon b/tests/php/phpstan/phpstan-8.0.neon index 364a960..382b8c1 100644 --- a/tests/php/phpstan/phpstan-8.0.neon +++ b/tests/php/phpstan/phpstan-8.0.neon @@ -6,3 +6,4 @@ parameters: - '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#' - '#Access to an undefined property Cookie\:\:\$ga_admin_order.#' - '#Access to an undefined property Cookie\:\:\$ga_admin_refund.#' + - '#Call to an undefined method Module::getDataHandler\(\).#' diff --git a/tests/php/phpstan/phpstan-8.1.7.neon b/tests/php/phpstan/phpstan-8.1.7.neon index c02374b..da679c9 100644 --- a/tests/php/phpstan/phpstan-8.1.7.neon +++ b/tests/php/phpstan/phpstan-8.1.7.neon @@ -7,4 +7,5 @@ parameters: - '#Access to an undefined property Cookie\:\:\$ga_admin_order.#' - '#Access to an undefined property Cookie\:\:\$ga_admin_refund.#' - '#Property PrestaShop\\Module\\Ps_Googleanalytics\\Hooks\\HookActionValidateOrder::\$module is never read, only written\.#' - - '#Method PrestaShop\\Module\\Ps_Googleanalytics\\Hooks\\HookDisplayBeforeBodyClosingTag::renderCartpage\(\) is unused\.#' \ No newline at end of file + - '#Method PrestaShop\\Module\\Ps_Googleanalytics\\Hooks\\HookDisplayBeforeBodyClosingTag::renderCartpage\(\) is unused\.#' + - '#Call to an undefined method Module::getDataHandler\(\).#' \ No newline at end of file diff --git a/tests/php/phpstan/phpstan-8.2.x.neon b/tests/php/phpstan/phpstan-8.2.x.neon index c02374b..da679c9 100644 --- a/tests/php/phpstan/phpstan-8.2.x.neon +++ b/tests/php/phpstan/phpstan-8.2.x.neon @@ -7,4 +7,5 @@ parameters: - '#Access to an undefined property Cookie\:\:\$ga_admin_order.#' - '#Access to an undefined property Cookie\:\:\$ga_admin_refund.#' - '#Property PrestaShop\\Module\\Ps_Googleanalytics\\Hooks\\HookActionValidateOrder::\$module is never read, only written\.#' - - '#Method PrestaShop\\Module\\Ps_Googleanalytics\\Hooks\\HookDisplayBeforeBodyClosingTag::renderCartpage\(\) is unused\.#' \ No newline at end of file + - '#Method PrestaShop\\Module\\Ps_Googleanalytics\\Hooks\\HookDisplayBeforeBodyClosingTag::renderCartpage\(\) is unused\.#' + - '#Call to an undefined method Module::getDataHandler\(\).#' \ No newline at end of file diff --git a/tests/php/phpstan/phpstan-9.0.x.neon b/tests/php/phpstan/phpstan-9.0.x.neon index 00dcfae..dd41fa5 100644 --- a/tests/php/phpstan/phpstan-9.0.x.neon +++ b/tests/php/phpstan/phpstan-9.0.x.neon @@ -6,4 +6,5 @@ parameters: - '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#' - '#Access to an undefined property Cookie\:\:\$ga_admin_order.#' - '#Access to an undefined property Cookie\:\:\$ga_admin_refund.#' - - '#Property PrestaShop\\Module\\Ps_Googleanalytics\\Hooks\\HookActionValidateOrder::\$module is never read, only written\.#' \ No newline at end of file + - '#Property PrestaShop\\Module\\Ps_Googleanalytics\\Hooks\\HookActionValidateOrder::\$module is never read, only written\.#' + - '#Call to an undefined method Module::getDataHandler\(\).#' \ No newline at end of file diff --git a/tests/php/phpstan/phpstan-9.1.x.neon b/tests/php/phpstan/phpstan-9.1.x.neon index 00dcfae..dd41fa5 100644 --- a/tests/php/phpstan/phpstan-9.1.x.neon +++ b/tests/php/phpstan/phpstan-9.1.x.neon @@ -6,4 +6,5 @@ parameters: - '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#' - '#Access to an undefined property Cookie\:\:\$ga_admin_order.#' - '#Access to an undefined property Cookie\:\:\$ga_admin_refund.#' - - '#Property PrestaShop\\Module\\Ps_Googleanalytics\\Hooks\\HookActionValidateOrder::\$module is never read, only written\.#' \ No newline at end of file + - '#Property PrestaShop\\Module\\Ps_Googleanalytics\\Hooks\\HookActionValidateOrder::\$module is never read, only written\.#' + - '#Call to an undefined method Module::getDataHandler\(\).#' \ No newline at end of file diff --git a/tests/php/phpstan/phpstan-develop.neon b/tests/php/phpstan/phpstan-develop.neon index 00dcfae..dd41fa5 100644 --- a/tests/php/phpstan/phpstan-develop.neon +++ b/tests/php/phpstan/phpstan-develop.neon @@ -6,4 +6,5 @@ parameters: - '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#' - '#Access to an undefined property Cookie\:\:\$ga_admin_order.#' - '#Access to an undefined property Cookie\:\:\$ga_admin_refund.#' - - '#Property PrestaShop\\Module\\Ps_Googleanalytics\\Hooks\\HookActionValidateOrder::\$module is never read, only written\.#' \ No newline at end of file + - '#Property PrestaShop\\Module\\Ps_Googleanalytics\\Hooks\\HookActionValidateOrder::\$module is never read, only written\.#' + - '#Call to an undefined method Module::getDataHandler\(\).#' \ No newline at end of file diff --git a/tests/php/phpstan/phpstan-latest.neon b/tests/php/phpstan/phpstan-latest.neon index 364a960..382b8c1 100644 --- a/tests/php/phpstan/phpstan-latest.neon +++ b/tests/php/phpstan/phpstan-latest.neon @@ -6,3 +6,4 @@ parameters: - '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#' - '#Access to an undefined property Cookie\:\:\$ga_admin_order.#' - '#Access to an undefined property Cookie\:\:\$ga_admin_refund.#' + - '#Call to an undefined method Module::getDataHandler\(\).#'