Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions classes/Database/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public function registerHooks()
$this->module->registerHook('displayAdminOrder') &&
$this->module->registerHook('displayBeforeBodyClosingTag') &&
$this->module->registerHook('displayFooterProduct') &&
$this->module->registerHook('displayProductAdditionalInfo') &&
$this->module->registerHook('displayOrderConfirmation') &&
$this->module->registerHook('actionProductCancel') &&
$this->module->registerHook('actionValidateOrder') &&
Expand Down
29 changes: 28 additions & 1 deletion classes/Handler/GanalyticsJsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,33 @@ public function __construct(Ps_Googleanalytics $module, Context $context)
* @return void|string
*/
public function generate($jsCode)
{
return $this->render($jsCode, 'ga_tag.tpl');
}

/**
* Generate Google Analytics js that runs as soon as it is parsed.
*
* ga_tag.tpl defers to DOMContentLoaded, which never fires again once the page has loaded, so
* anything injected into an already-rendered page - a block swapped in by an AJAX refresh -
* has to run immediately or not at all.
*
* @param string $jsCode
*
* @return void|string
*/
public function generateImmediate($jsCode)
{
return $this->render($jsCode, 'ga_tag_immediate.tpl');
}

/**
* @param string $jsCode
* @param string $template
*
* @return void|string
*/
private function render($jsCode, $template)
{
if (Configuration::get('GA_ACCOUNT_ID')) {
$this->context->smarty->assign(
Expand All @@ -53,7 +80,7 @@ public function generate($jsCode)

return $this->module->display(
$this->module->getLocalPath() . $this->module->name,
'ga_tag.tpl'
$template
);
}
}
Expand Down
101 changes: 101 additions & 0 deletions classes/Hook/HookDisplayProductAdditionalInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
*
* 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.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 <contact@prestashop.com>
* @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
*/

namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;

use Context;
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler;
use PrestaShop\Module\Ps_Googleanalytics\Wrapper\ProductWrapper;
use Ps_Googleanalytics;
use Tools;

class HookDisplayProductAdditionalInfo implements HookInterface
{
private $module;
private $context;

public function __construct(Ps_Googleanalytics $module, Context $context)
{
$this->module = $module;
$this->context = $context;
}

/**
* run
*
* Re-sends view_item after the shopper picks another combination.
*
* The product page swaps a handful of blocks over AJAX and pushes a new URL instead of
* reloading, so displayFooterProduct - which carries view_item - is never rendered again and
* Google Analytics keeps reporting the combination the page was opened with. This partial IS
* one of the blocks ProductController returns for that refresh, so hooking it here reports the
* combination the shopper is actually looking at.
*
* @return string|void
*/
public function run()
{
/*
* Only the product refresh, nothing else. On a normal page render displayFooterProduct has
* already sent view_item, and emitting here as well would double every product view.
*/
if (!Tools::getValue('ajax') || Tools::getValue('action') !== 'refresh') {
return;
}

// Quickview has never reported to Google Analytics; its refresh should not either.
if (Tools::getValue('quickview')) {
return;
}

if ($this->context->controller->php_self !== 'product') {
return;
}

$product = $this->context->smarty->getTemplateVars('product');
if (empty($product)) {
return;
}

/*
* The 'product' template variable is expected to be a ProductLazyArray (array-accessible).
* Some themes or modules assign a raw Product object instead, which prepareItemFromProduct()
* cannot consume. Skip tracking in that case rather than breaking the refresh.
*/
if (!is_array($product) && !$product instanceof \ArrayAccess) {
return;
}

$productWrapper = new ProductWrapper($this->context);
$item = $productWrapper->prepareItemFromProduct($product);

$jsCode = $this->module->getTools()->renderEvent(
'view_item',
[
'currency' => $this->context->currency->iso_code,
'value' => $item['price'],
'items' => [$item],
]
);

// Injected into a page that has already loaded, so it must not wait for DOMContentLoaded.
return (new GanalyticsJsHandler($this->module, $this->context))->generateImmediate($jsCode);
}
}
2 changes: 1 addition & 1 deletion config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<module>
<name>ps_googleanalytics</name>
<displayName><![CDATA[Google Analytics]]></displayName>
<version><![CDATA[6.0.0]]></version>
<version><![CDATA[6.1.0]]></version>
<description><![CDATA[Gain clear insights into important metrics about your customers, using Google Analytics]]></description>
<author><![CDATA[PrestaShop]]></author>
<tab><![CDATA[analytics_stats]]></tab>
Expand Down
14 changes: 13 additions & 1 deletion ps_googleanalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct()
{
$this->name = 'ps_googleanalytics';
$this->tab = 'analytics_stats';
$this->version = '6.0.0';
$this->version = '6.1.0';
$this->ps_versions_compliancy = ['min' => '8.2.0', 'max' => _PS_VERSION_];
$this->author = 'PrestaShop';
$this->module_key = 'fd2aaefea84ac1bb512e6f1878d990b8';
Expand Down Expand Up @@ -120,6 +120,18 @@ public function hookDisplayFooterProduct()
return $hook->run();
}

/**
* Product page additional-info hook
* This block is re-rendered when the shopper picks another combination, which is where
* view_item has to be sent again
*/
public function hookDisplayProductAdditionalInfo()
{
$hook = new PrestaShop\Module\Ps_Googleanalytics\Hooks\HookDisplayProductAdditionalInfo($this, $this->context);

return $hook->run();
}

/**
* Hook admin order.
* This function is run to send transactions and refunds details
Expand Down
32 changes: 32 additions & 0 deletions upgrade/upgrade-6.1.0.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
*
* 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.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 <contact@prestashop.com>
* @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
*/
if (!defined('_PS_VERSION_')) {
exit;
}

/**
* @param Ps_Googleanalytics $object
*
* @return bool
*/
function upgrade_module_6_1_0($object)
{
return $object->registerHook('displayProductAdditionalInfo');
}
26 changes: 26 additions & 0 deletions views/templates/hook/ga_tag_immediate.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{**
* Copyright since 2007 PrestaShop SA and Contributors
*
* 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.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 <contact@prestashop.com>
* @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
*}

{* Same as ga_tag.tpl, without the DOMContentLoaded wrapper: this block is injected into a page
that has already loaded, so waiting for that event would mean the code never runs. *}
{if (!empty($jsCode))}
<script type="text/javascript">
{$jsCode nofilter}
</script>
{/if}