Skip to content

Commit

Permalink
Merge pull request #92 from PrestaShop/dev
Browse files Browse the repository at this point in the history
Release v4.1.0
  • Loading branch information
matks authored Jul 28, 2021
2 parents d64569e + bc563ec commit 735c6ee
Show file tree
Hide file tree
Showing 18 changed files with 302 additions and 61 deletions.
15 changes: 11 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@

Gain clear insights into important metrics about your customers, using Google Analytics.

## Notes
To use it, you will need to create a Google Analytics account and insert your Google Analytics Identifier into the Module configuration page.

Enhanced Ecommerce must be enabled in Google Analytics settings for full functionality. Otherwise, some data (refunds etc.) will not be visible. Follow [instructions][4].
### Notes

Google Tag Assistant will report "No HTTP Response detected" error in Prestashop back-office. **This is not a bug.** This happens, because the module does not send page views in BO, not to influence statistics. Only events, like refunds, are sent from BO.
Enhanced Ecommerce must be enabled in Google Analytics settings for full functionality. Otherwise, some data (refunds etc.) will not be visible. Follow [the related instructions][4].

### Configure

1. Install the module into your shop.
2. Create an account on Google Analytics if you do not have one.
3. Go on the "Configure" page of the module to insert your Google Analytics Identifier.
4. The data will then be sent to Google Analytics and you can monitor/explore it.

## Contributing

PrestaShop modules are open-source extensions to the PrestaShop e-commerce solution. Everyone is welcome and even encouraged to contribute with their own improvements.

Google Analytics is compatible with all versions of PrestaShop 1.7 and 1.6
Google Analytics is compatible with all versions of PrestaShop 1.7 and 1.6.

### Requirements

Expand Down
15 changes: 15 additions & 0 deletions classes/Database/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace PrestaShop\Module\Ps_Googleanalytics\Database;

use Configuration;
use Db;
use Ps_Googleanalytics;
use Shop;
Expand Down Expand Up @@ -55,6 +56,7 @@ public function installTables()
`id_customer` int(10) NOT NULL,
`id_shop` int(11) NOT NULL,
`sent` tinyint(1) DEFAULT NULL,
`refund_sent` tinyint(1) DEFAULT NULL,
`date_add` datetime DEFAULT NULL,
PRIMARY KEY (`id_google_analytics`),
KEY `id_order` (`id_order`),
Expand All @@ -77,6 +79,18 @@ public function installTables()
return true;
}

/**
* Insert default data to database
*
* @return bool
*/
public function setDefaultConfiguration()
{
Configuration::updateValue('GA_CANCELLED_STATES', json_encode([Configuration::get('PS_OS_CANCELED')]));

return true;
}

/**
* Register Module hooks
*
Expand All @@ -91,6 +105,7 @@ public function registerHooks()
$this->module->registerHook('displayFooterProduct') &&
$this->module->registerHook('displayOrderConfirmation') &&
$this->module->registerHook('actionProductCancel') &&
$this->module->registerHook('actionOrderStatusPostUpdate') &&
$this->module->registerHook('actionCartSave') &&
$this->module->registerHook('displayBackOfficeHeader') &&
$this->module->registerHook('actionCarrierProcess');
Expand Down
61 changes: 55 additions & 6 deletions classes/Form/ConfigurationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@

use AdminController;
use Configuration;
use Context;
use HelperForm;
use OrderState;
use Ps_Googleanalytics;
use Shop;
use Tools;
Expand Down Expand Up @@ -101,12 +103,12 @@ public function generate()
[
'id' => 'ga_userid_enabled',
'value' => 1,
'label' => $this->module->l('Enabled'),
'label' => $this->module->l('Yes'),
],
[
'id' => 'ga_userid_disabled',
'value' => 0,
'label' => $this->module->l('Disabled'),
'label' => $this->module->l('No'),
], ],
],
[
Expand All @@ -118,15 +120,46 @@ public function generate()
[
'id' => 'ga_anonymize_enabled',
'value' => 1,
'label' => $this->module->l('Enabled'),
'label' => $this->module->l('Yes'),
],
[
'id' => 'ga_anonymize_disabled',
'value' => 0,
'label' => $this->module->l('Disabled'),
'label' => $this->module->l('No'),
],
],
],
[
'type' => 'switch',
'label' => $this->module->l('Enable Back Office Tracking'),
'name' => 'GA_TRACK_BACKOFFICE_ENABLED',
'hint' => $this->module->l('Use this option to enable the tracking inside the Back Office'),
'values' => [
[
'id' => 'ga_track_backoffice',
'value' => 1,
'label' => $this->module->l('Yes'),
],
[
'id' => 'ga_do_not_track_backoffice',
'value' => 0,
'label' => $this->module->l('No'),
],
],
],
[
'type' => 'select',
'label' => $this->module->l('Cancelled order states'),
'name' => 'GA_CANCELLED_STATES',
'desc' => $this->module->l('Choose order states, in which you consider the given order cancelled. This will be usually only the default "Cancelled" state, but some shops may have extra states like "Returned" etc.'),
'class' => 'chosen',
'multiple' => true,
'options' => [
'query' => OrderState::getOrderStates((int) Context::getContext()->language->id),
'id' => 'id_order_state',
'name' => 'name',
],
],
],
'submit' => [
'title' => $this->module->l('Save'),
Expand All @@ -142,12 +175,12 @@ public function generate()
[
'id' => 'ga_crossdomain_enabled',
'value' => 1,
'label' => $this->module->l('Enabled'),
'label' => $this->module->l('Yes'),
],
[
'id' => 'ga_crossdomain_disabled',
'value' => 0,
'label' => $this->module->l('Disabled'),
'label' => $this->module->l('No'),
],
],
];
Expand All @@ -158,6 +191,8 @@ public function generate()
$helper->fields_value['GA_USERID_ENABLED'] = Configuration::get('GA_USERID_ENABLED');
$helper->fields_value['GA_CROSSDOMAIN_ENABLED'] = Configuration::get('GA_CROSSDOMAIN_ENABLED');
$helper->fields_value['GA_ANONYMIZE_ENABLED'] = Configuration::get('GA_ANONYMIZE_ENABLED');
$helper->fields_value['GA_TRACK_BACKOFFICE_ENABLED'] = Configuration::get('GA_TRACK_BACKOFFICE_ENABLED');
$helper->fields_value['GA_CANCELLED_STATES[]'] = json_decode(Configuration::get('GA_CANCELLED_STATES'), true);

return $helper->generateForm($fields_form);
}
Expand All @@ -174,6 +209,8 @@ public function treat()
$gaUserIdEnabled = Tools::getValue('GA_USERID_ENABLED');
$gaCrossdomainEnabled = Tools::getValue('GA_CROSSDOMAIN_ENABLED');
$gaAnonymizeEnabled = Tools::getValue('GA_ANONYMIZE_ENABLED');
$gaTrackBackOffice = Tools::getValue('GA_TRACK_BACKOFFICE_ENABLED');
$gaCancelledStates = Tools::getValue('GA_CANCELLED_STATES');

if (!empty($gaAccountId)) {
Configuration::updateValue('GA_ACCOUNT_ID', $gaAccountId);
Expand All @@ -196,6 +233,18 @@ public function treat()
$treatmentResult .= $this->module->displayConfirmation($this->module->l('Settings for Anonymize IP updated successfully'));
}

if (null !== $gaTrackBackOffice) {
Configuration::updateValue('GA_TRACK_BACKOFFICE_ENABLED', (bool) $gaTrackBackOffice);
$treatmentResult .= $this->module->displayConfirmation($this->module->l('Settings for Enable Back Office tracking updated successfully'));
}

if ($gaCancelledStates === false) {
Configuration::updateValue('GA_CANCELLED_STATES', '');
} else {
Configuration::updateValue('GA_CANCELLED_STATES', json_encode($gaCancelledStates));
}
$treatmentResult .= $this->module->displayConfirmation($this->module->l('Settings for cancelled order states updated successfully'));

return $treatmentResult;
}
}
21 changes: 17 additions & 4 deletions classes/Handler/ModuleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
class ModuleHandler
{
/**
* isModuleEnabled
*
* @param string $moduleName
*
* @return bool
Expand All @@ -47,12 +45,27 @@ public function isModuleEnabled($moduleName)
return false;
}

return $module->registerHook('displayHome');
return true;
}

/**
* uninstallModule
* @param string $moduleName
* @param string $hookName
*
* @return bool
*/
public function isModuleEnabledAndHookedOn($moduleName, $hookName)
{
$module = Module::getInstanceByName($moduleName);

if (false === $this->isModuleEnabled($moduleName)) {
return false;
}

return $module->isRegisteredInHook($hookName);
}

/**
* @param string $moduleName
*
* @return bool
Expand Down
96 changes: 96 additions & 0 deletions classes/Hook/HookActionOrderStatusPostUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/**
* 2007-2020 PrestaShop 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.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 [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2020 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 Configuration;
use Context;
use Db;
use Ps_Googleanalytics;

class HookActionOrderStatusPostUpdate implements HookInterface
{
/**
* @var Ps_Googleanalytics
*/
private $module;

/**
* @var Context
*/
private $context;

/**
* @var array
*/
private $params;

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

/**
* run
*
* @return void
*/
public function run()
{
// If we do not have an order or a new order status, we return
if (empty($this->params['id_order']) || empty($this->params['newOrderStatus']->id)) {
return;
}

// We get all states in which the merchant want to have refund sent and check if the new state being set belongs there
$gaCancelledStates = json_decode(Configuration::get('GA_CANCELLED_STATES'), true);
if (empty($gaCancelledStates) || !in_array($this->params['newOrderStatus']->id, $gaCancelledStates)) {
return;
}

// We check if the refund was already sent to Google Analytics
$gaRefundSent = Db::getInstance()->getValue(
'SELECT id_order FROM `' . _DB_PREFIX_ . 'ganalytics` WHERE id_order = ' . (int) $this->params['id_order'] . ' AND refund_sent = 1'
);

// If it was not already refunded
if ($gaRefundSent === false) {
// We refund it and set the "sent" flag to true
$this->context->cookie->__set('ga_admin_refund', 'MBG.refundByOrderId(' . json_encode(['id' => $this->params['id_order']]) . ');');
$this->context->cookie->write();

// We save this information to database
Db::getInstance()->execute(
'UPDATE `' . _DB_PREFIX_ . 'ganalytics` SET refund_sent = 1 WHERE id_order = ' . (int) $this->params['id_order']
);
}
}

/**
* setParams
*
* @param array $params
*/
public function setParams($params)
{
$this->params = $params;
}
}
2 changes: 1 addition & 1 deletion classes/Hook/HookDisplayBackOfficeHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function run()
'id_order' => (int) Tools::getValue('id_order'),
'id_shop' => (int) $this->context->shop->id,
'sent' => 0,
'date_add' => 'NOW()',
'date_add' => ['value' => 'NOW()', 'type' => 'sql'],
]
);
}
Expand Down
7 changes: 7 additions & 0 deletions classes/Hook/HookDisplayFooter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler;
use PrestaShop\Module\Ps_Googleanalytics\Wrapper\ProductWrapper;
use Ps_Googleanalytics;
use RecursiveArrayIterator;
use RecursiveIteratorIterator;
use Tools;

class HookDisplayFooter implements HookInterface
Expand Down Expand Up @@ -70,6 +72,11 @@ public function run()
$gacart['quantity'] = abs($gacart['quantity']);
$gaScripts .= 'MBG.removeFromCart(' . json_encode($gacart) . ');';
}
} elseif (is_array($gacart)) {
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($gacart));
foreach ($it as $v) {
$gaScripts .= $v;
}
} else {
$gaScripts .= $gacart;
}
Expand Down
Loading

0 comments on commit 735c6ee

Please sign in to comment.