diff --git a/controllers/front/ajax.php b/controllers/front/ajax.php index c77698e4..ea92b0b3 100644 --- a/controllers/front/ajax.php +++ b/controllers/front/ajax.php @@ -31,6 +31,17 @@ public function initContent() { parent::initContent(); + if (Tools::getValue('action') === 'flushQueue') { + /** @var Ps_Googleanalytics $module */ + $module = $this->module; + + $output = $module->getDataHandler()->readData(); + $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 fa3cfee7..d45d5ac9 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 && Configuration::get('GA_ACCOUNT_ID')) { + 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 00000000..5082c560 --- /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 00000000..098efd94 --- /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;