Skip to content

Commit 735c6ee

Browse files
authored
Merge pull request #92 from PrestaShop/dev
Release v4.1.0
2 parents d64569e + bc563ec commit 735c6ee

18 files changed

Lines changed: 302 additions & 61 deletions

Readme.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,24 @@
44

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

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

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

11-
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.
11+
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].
12+
13+
### Configure
14+
15+
1. Install the module into your shop.
16+
2. Create an account on Google Analytics if you do not have one.
17+
3. Go on the "Configure" page of the module to insert your Google Analytics Identifier.
18+
4. The data will then be sent to Google Analytics and you can monitor/explore it.
1219

1320
## Contributing
1421

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

17-
Google Analytics is compatible with all versions of PrestaShop 1.7 and 1.6
24+
Google Analytics is compatible with all versions of PrestaShop 1.7 and 1.6.
1825

1926
### Requirements
2027

classes/Database/Install.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
namespace PrestaShop\Module\Ps_Googleanalytics\Database;
2222

23+
use Configuration;
2324
use Db;
2425
use Ps_Googleanalytics;
2526
use Shop;
@@ -55,6 +56,7 @@ public function installTables()
5556
`id_customer` int(10) NOT NULL,
5657
`id_shop` int(11) NOT NULL,
5758
`sent` tinyint(1) DEFAULT NULL,
59+
`refund_sent` tinyint(1) DEFAULT NULL,
5860
`date_add` datetime DEFAULT NULL,
5961
PRIMARY KEY (`id_google_analytics`),
6062
KEY `id_order` (`id_order`),
@@ -77,6 +79,18 @@ public function installTables()
7779
return true;
7880
}
7981

82+
/**
83+
* Insert default data to database
84+
*
85+
* @return bool
86+
*/
87+
public function setDefaultConfiguration()
88+
{
89+
Configuration::updateValue('GA_CANCELLED_STATES', json_encode([Configuration::get('PS_OS_CANCELED')]));
90+
91+
return true;
92+
}
93+
8094
/**
8195
* Register Module hooks
8296
*
@@ -91,6 +105,7 @@ public function registerHooks()
91105
$this->module->registerHook('displayFooterProduct') &&
92106
$this->module->registerHook('displayOrderConfirmation') &&
93107
$this->module->registerHook('actionProductCancel') &&
108+
$this->module->registerHook('actionOrderStatusPostUpdate') &&
94109
$this->module->registerHook('actionCartSave') &&
95110
$this->module->registerHook('displayBackOfficeHeader') &&
96111
$this->module->registerHook('actionCarrierProcess');

classes/Form/ConfigurationForm.php

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222

2323
use AdminController;
2424
use Configuration;
25+
use Context;
2526
use HelperForm;
27+
use OrderState;
2628
use Ps_Googleanalytics;
2729
use Shop;
2830
use Tools;
@@ -101,12 +103,12 @@ public function generate()
101103
[
102104
'id' => 'ga_userid_enabled',
103105
'value' => 1,
104-
'label' => $this->module->l('Enabled'),
106+
'label' => $this->module->l('Yes'),
105107
],
106108
[
107109
'id' => 'ga_userid_disabled',
108110
'value' => 0,
109-
'label' => $this->module->l('Disabled'),
111+
'label' => $this->module->l('No'),
110112
], ],
111113
],
112114
[
@@ -118,15 +120,46 @@ public function generate()
118120
[
119121
'id' => 'ga_anonymize_enabled',
120122
'value' => 1,
121-
'label' => $this->module->l('Enabled'),
123+
'label' => $this->module->l('Yes'),
122124
],
123125
[
124126
'id' => 'ga_anonymize_disabled',
125127
'value' => 0,
126-
'label' => $this->module->l('Disabled'),
128+
'label' => $this->module->l('No'),
127129
],
128130
],
129131
],
132+
[
133+
'type' => 'switch',
134+
'label' => $this->module->l('Enable Back Office Tracking'),
135+
'name' => 'GA_TRACK_BACKOFFICE_ENABLED',
136+
'hint' => $this->module->l('Use this option to enable the tracking inside the Back Office'),
137+
'values' => [
138+
[
139+
'id' => 'ga_track_backoffice',
140+
'value' => 1,
141+
'label' => $this->module->l('Yes'),
142+
],
143+
[
144+
'id' => 'ga_do_not_track_backoffice',
145+
'value' => 0,
146+
'label' => $this->module->l('No'),
147+
],
148+
],
149+
],
150+
[
151+
'type' => 'select',
152+
'label' => $this->module->l('Cancelled order states'),
153+
'name' => 'GA_CANCELLED_STATES',
154+
'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.'),
155+
'class' => 'chosen',
156+
'multiple' => true,
157+
'options' => [
158+
'query' => OrderState::getOrderStates((int) Context::getContext()->language->id),
159+
'id' => 'id_order_state',
160+
'name' => 'name',
161+
],
162+
],
130163
],
131164
'submit' => [
132165
'title' => $this->module->l('Save'),
@@ -142,12 +175,12 @@ public function generate()
142175
[
143176
'id' => 'ga_crossdomain_enabled',
144177
'value' => 1,
145-
'label' => $this->module->l('Enabled'),
178+
'label' => $this->module->l('Yes'),
146179
],
147180
[
148181
'id' => 'ga_crossdomain_disabled',
149182
'value' => 0,
150-
'label' => $this->module->l('Disabled'),
183+
'label' => $this->module->l('No'),
151184
],
152185
],
153186
];
@@ -158,6 +191,8 @@ public function generate()
158191
$helper->fields_value['GA_USERID_ENABLED'] = Configuration::get('GA_USERID_ENABLED');
159192
$helper->fields_value['GA_CROSSDOMAIN_ENABLED'] = Configuration::get('GA_CROSSDOMAIN_ENABLED');
160193
$helper->fields_value['GA_ANONYMIZE_ENABLED'] = Configuration::get('GA_ANONYMIZE_ENABLED');
194+
$helper->fields_value['GA_TRACK_BACKOFFICE_ENABLED'] = Configuration::get('GA_TRACK_BACKOFFICE_ENABLED');
195+
$helper->fields_value['GA_CANCELLED_STATES[]'] = json_decode(Configuration::get('GA_CANCELLED_STATES'), true);
161196

162197
return $helper->generateForm($fields_form);
163198
}
@@ -174,6 +209,8 @@ public function treat()
174209
$gaUserIdEnabled = Tools::getValue('GA_USERID_ENABLED');
175210
$gaCrossdomainEnabled = Tools::getValue('GA_CROSSDOMAIN_ENABLED');
176211
$gaAnonymizeEnabled = Tools::getValue('GA_ANONYMIZE_ENABLED');
212+
$gaTrackBackOffice = Tools::getValue('GA_TRACK_BACKOFFICE_ENABLED');
213+
$gaCancelledStates = Tools::getValue('GA_CANCELLED_STATES');
177214

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

236+
if (null !== $gaTrackBackOffice) {
237+
Configuration::updateValue('GA_TRACK_BACKOFFICE_ENABLED', (bool) $gaTrackBackOffice);
238+
$treatmentResult .= $this->module->displayConfirmation($this->module->l('Settings for Enable Back Office tracking updated successfully'));
239+
}
240+
241+
if ($gaCancelledStates === false) {
242+
Configuration::updateValue('GA_CANCELLED_STATES', '');
243+
} else {
244+
Configuration::updateValue('GA_CANCELLED_STATES', json_encode($gaCancelledStates));
245+
}
246+
$treatmentResult .= $this->module->displayConfirmation($this->module->l('Settings for cancelled order states updated successfully'));
247+
199248
return $treatmentResult;
200249
}
201250
}

classes/Handler/ModuleHandler.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
class ModuleHandler
2626
{
2727
/**
28-
* isModuleEnabled
29-
*
3028
* @param string $moduleName
3129
*
3230
* @return bool
@@ -47,12 +45,27 @@ public function isModuleEnabled($moduleName)
4745
return false;
4846
}
4947

50-
return $module->registerHook('displayHome');
48+
return true;
5149
}
5250

5351
/**
54-
* uninstallModule
52+
* @param string $moduleName
53+
* @param string $hookName
5554
*
55+
* @return bool
56+
*/
57+
public function isModuleEnabledAndHookedOn($moduleName, $hookName)
58+
{
59+
$module = Module::getInstanceByName($moduleName);
60+
61+
if (false === $this->isModuleEnabled($moduleName)) {
62+
return false;
63+
}
64+
65+
return $module->isRegisteredInHook($hookName);
66+
}
67+
68+
/**
5669
* @param string $moduleName
5770
*
5871
* @return bool
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* 2007-2020 PrestaShop and Contributors
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
8+
* that is bundled with this package in the file LICENSE.txt.
9+
* It is also available through the world-wide-web at this URL:
10+
* https://opensource.org/licenses/AFL-3.0
11+
* If you did not receive a copy of the license and are unable to
12+
* obtain it through the world-wide-web, please send an email
13+
* to license@prestashop.com so we can send you a copy immediately.
14+
*
15+
* @author PrestaShop SA <contact@prestashop.com>
16+
* @copyright 2007-2020 PrestaShop SA and Contributors
17+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
18+
* International Registered Trademark & Property of PrestaShop SA
19+
*/
20+
21+
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
22+
23+
use Configuration;
24+
use Context;
25+
use Db;
26+
use Ps_Googleanalytics;
27+
28+
class HookActionOrderStatusPostUpdate implements HookInterface
29+
{
30+
/**
31+
* @var Ps_Googleanalytics
32+
*/
33+
private $module;
34+
35+
/**
36+
* @var Context
37+
*/
38+
private $context;
39+
40+
/**
41+
* @var array
42+
*/
43+
private $params;
44+
45+
public function __construct(Ps_Googleanalytics $module, Context $context)
46+
{
47+
$this->module = $module;
48+
$this->context = $context;
49+
}
50+
51+
/**
52+
* run
53+
*
54+
* @return void
55+
*/
56+
public function run()
57+
{
58+
// If we do not have an order or a new order status, we return
59+
if (empty($this->params['id_order']) || empty($this->params['newOrderStatus']->id)) {
60+
return;
61+
}
62+
63+
// We get all states in which the merchant want to have refund sent and check if the new state being set belongs there
64+
$gaCancelledStates = json_decode(Configuration::get('GA_CANCELLED_STATES'), true);
65+
if (empty($gaCancelledStates) || !in_array($this->params['newOrderStatus']->id, $gaCancelledStates)) {
66+
return;
67+
}
68+
69+
// We check if the refund was already sent to Google Analytics
70+
$gaRefundSent = Db::getInstance()->getValue(
71+
'SELECT id_order FROM `' . _DB_PREFIX_ . 'ganalytics` WHERE id_order = ' . (int) $this->params['id_order'] . ' AND refund_sent = 1'
72+
);
73+
74+
// If it was not already refunded
75+
if ($gaRefundSent === false) {
76+
// We refund it and set the "sent" flag to true
77+
$this->context->cookie->__set('ga_admin_refund', 'MBG.refundByOrderId(' . json_encode(['id' => $this->params['id_order']]) . ');');
78+
$this->context->cookie->write();
79+
80+
// We save this information to database
81+
Db::getInstance()->execute(
82+
'UPDATE `' . _DB_PREFIX_ . 'ganalytics` SET refund_sent = 1 WHERE id_order = ' . (int) $this->params['id_order']
83+
);
84+
}
85+
}
86+
87+
/**
88+
* setParams
89+
*
90+
* @param array $params
91+
*/
92+
public function setParams($params)
93+
{
94+
$this->params = $params;
95+
}
96+
}

classes/Hook/HookDisplayBackOfficeHeader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function run()
7575
'id_order' => (int) Tools::getValue('id_order'),
7676
'id_shop' => (int) $this->context->shop->id,
7777
'sent' => 0,
78-
'date_add' => 'NOW()',
78+
'date_add' => ['value' => 'NOW()', 'type' => 'sql'],
7979
]
8080
);
8181
}

classes/Hook/HookDisplayFooter.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler;
2828
use PrestaShop\Module\Ps_Googleanalytics\Wrapper\ProductWrapper;
2929
use Ps_Googleanalytics;
30+
use RecursiveArrayIterator;
31+
use RecursiveIteratorIterator;
3032
use Tools;
3133

3234
class HookDisplayFooter implements HookInterface
@@ -70,6 +72,11 @@ public function run()
7072
$gacart['quantity'] = abs($gacart['quantity']);
7173
$gaScripts .= 'MBG.removeFromCart(' . json_encode($gacart) . ');';
7274
}
75+
} elseif (is_array($gacart)) {
76+
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($gacart));
77+
foreach ($it as $v) {
78+
$gaScripts .= $v;
79+
}
7380
} else {
7481
$gaScripts .= $gacart;
7582
}

0 commit comments

Comments
 (0)