Skip to content

Commit 8b5a36a

Browse files
committed
Implement proper fail backloading
1 parent 7d1ca46 commit 8b5a36a

4 files changed

Lines changed: 90 additions & 68 deletions

File tree

classes/Database/Install.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public function installTables()
8787
public function setDefaultConfiguration()
8888
{
8989
Configuration::updateValue('GA_CANCELLED_STATES', json_encode([Configuration::get('PS_OS_CANCELED')]));
90+
Configuration::updateValue('GA_BACKLOAD_ENABLED', false);
91+
Configuration::updateValue('GA_BACKLOAD_DAYS', 30);
9092

9193
return true;
9294
}

classes/Form/ConfigurationForm.php

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public function generate()
9595
'type' => 'switch',
9696
'label' => $this->module->getTranslator()->trans('Enable User ID tracking', [], 'Modules.Googleanalytics.Admin'),
9797
'name' => 'GA_USERID_ENABLED',
98+
'desc' => $this->module->getTranslator()->trans('This option adds unique user ID to the tag to better track the customer. Use this option only if it complies with laws in your country.', [], 'Modules.Googleanalytics.Admin'),
9899
'values' => [
99100
[
100101
'id' => 'ga_userid_enabled',
@@ -112,7 +113,7 @@ public function generate()
112113
'type' => 'switch',
113114
'label' => $this->module->getTranslator()->trans('Anonymize IP', [], 'Modules.Googleanalytics.Admin'),
114115
'name' => 'GA_ANONYMIZE_ENABLED',
115-
'hint' => $this->module->getTranslator()->trans('Use this option to anonymize the visitor’s IP to comply with data privacy laws in some countries', [], 'Modules.Googleanalytics.Admin'),
116+
'desc' => $this->module->getTranslator()->trans('Use this option to anonymize the visitor’s IP to comply with data privacy laws in some countries', [], 'Modules.Googleanalytics.Admin'),
116117
'values' => [
117118
[
118119
'id' => 'ga_anonymize_enabled',
@@ -130,7 +131,7 @@ public function generate()
130131
'type' => 'switch',
131132
'label' => $this->module->getTranslator()->trans('Enable Back Office Tracking', [], 'Modules.Googleanalytics.Admin'),
132133
'name' => 'GA_TRACK_BACKOFFICE_ENABLED',
133-
'hint' => $this->module->getTranslator()->trans('Use this option to enable the tracking inside the Back Office', [], 'Modules.Googleanalytics.Admin'),
134+
'desc' => $this->module->getTranslator()->trans('Use this option to enable the tracking inside the Back Office', [], 'Modules.Googleanalytics.Admin'),
134135
'values' => [
135136
[
136137
'id' => 'ga_track_backoffice',
@@ -157,6 +158,32 @@ public function generate()
157158
'name' => 'name',
158159
],
159160
],
161+
[
162+
'type' => 'switch',
163+
'label' => $this->module->getTranslator()->trans('Re-send failed orders', [], 'Modules.Googleanalytics.Admin'),
164+
'name' => 'GA_BACKLOAD_ENABLED',
165+
'desc' => $this->module->getTranslator()->trans('This option will resend all orders that failed to be sent normally in front-office, due to failures or ad-blockers.', [], 'Modules.Googleanalytics.Admin'),
166+
'values' => [
167+
[
168+
'id' => 'ga_backload_enabled',
169+
'value' => 1,
170+
'label' => $this->module->getTranslator()->trans('Yes', [], 'Modules.Googleanalytics.Admin'),
171+
],
172+
[
173+
'id' => 'ga_backload_enabled',
174+
'value' => 0,
175+
'label' => $this->module->getTranslator()->trans('No', [], 'Modules.Googleanalytics.Admin'),
176+
],
177+
],
178+
],
179+
[
180+
'type' => 'text',
181+
'label' => $this->module->getTranslator()->trans('Failed orders period', [], 'Modules.Googleanalytics.Admin'),
182+
'name' => 'GA_BACKLOAD_DAYS',
183+
'class' => 'input fixed-width-md',
184+
'suffix' => 'days',
185+
'desc' => $this->module->getTranslator()->trans('If you want to resend failed orders, specify how many days back the module should look for them. Default: 30.', [], 'Modules.Googleanalytics.Admin'),
186+
],
160187
],
161188
'submit' => [
162189
'title' => $this->module->getTranslator()->trans('Save', [], 'Modules.Googleanalytics.Admin'),
@@ -165,10 +192,12 @@ public function generate()
165192

166193
// Load current value
167194
$helper->fields_value['GA_ACCOUNT_ID'] = Configuration::get('GA_ACCOUNT_ID');
168-
$helper->fields_value['GA_USERID_ENABLED'] = Configuration::get('GA_USERID_ENABLED');
169-
$helper->fields_value['GA_ANONYMIZE_ENABLED'] = Configuration::get('GA_ANONYMIZE_ENABLED');
170-
$helper->fields_value['GA_TRACK_BACKOFFICE_ENABLED'] = Configuration::get('GA_TRACK_BACKOFFICE_ENABLED');
195+
$helper->fields_value['GA_USERID_ENABLED'] = (bool) Configuration::get('GA_USERID_ENABLED');
196+
$helper->fields_value['GA_ANONYMIZE_ENABLED'] = (bool) Configuration::get('GA_ANONYMIZE_ENABLED');
197+
$helper->fields_value['GA_TRACK_BACKOFFICE_ENABLED'] = (bool) Configuration::get('GA_TRACK_BACKOFFICE_ENABLED');
171198
$helper->fields_value['GA_CANCELLED_STATES[]'] = json_decode(Configuration::get('GA_CANCELLED_STATES'), true);
199+
$helper->fields_value['GA_BACKLOAD_ENABLED'] = (bool) Configuration::get('GA_BACKLOAD_ENABLED');
200+
$helper->fields_value['GA_BACKLOAD_DAYS'] = (int) Configuration::get('GA_BACKLOAD_DAYS');
172201

173202
return $helper->generateForm($fields_form);
174203
}
@@ -185,6 +214,8 @@ public function treat()
185214
$gaAnonymizeEnabled = Tools::getValue('GA_ANONYMIZE_ENABLED');
186215
$gaTrackBackOffice = Tools::getValue('GA_TRACK_BACKOFFICE_ENABLED');
187216
$gaCancelledStates = Tools::getValue('GA_CANCELLED_STATES');
217+
$gaBackloadEnabled = Tools::getValue('GA_BACKLOAD_ENABLED');
218+
$gaBackloadDays = Tools::getValue('GA_BACKLOAD_DAYS');
188219

189220
if (!empty($gaAccountId)) {
190221
Configuration::updateValue('GA_ACCOUNT_ID', $gaAccountId);
@@ -203,6 +234,14 @@ public function treat()
203234
Configuration::updateValue('GA_TRACK_BACKOFFICE_ENABLED', (bool) $gaTrackBackOffice);
204235
}
205236

237+
if (null !== $gaBackloadEnabled) {
238+
Configuration::updateValue('GA_BACKLOAD_ENABLED', (bool) $gaBackloadEnabled);
239+
}
240+
241+
if (null !== $gaBackloadDays) {
242+
Configuration::updateValue('GA_BACKLOAD_DAYS', (int) $gaBackloadDays);
243+
}
244+
206245
if ($gaCancelledStates === false) {
207246
Configuration::updateValue('GA_CANCELLED_STATES', '');
208247
} else {

classes/Hook/HookDisplayBackOfficeHeader.php

Lines changed: 39 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020

2121
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
2222

23+
use Cart;
2324
use Configuration;
2425
use Context;
2526
use Currency;
27+
use Db;
2628
use Order;
2729
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler;
2830
use PrestaShop\Module\Ps_Googleanalytics\Repository\GanalyticsRepository;
@@ -60,70 +62,13 @@ public function run()
6062
// Render base tag using displayHeader hook with backoffice parameter
6163
$gaScripts .= $this->module->hookDisplayHeader(null, true);
6264

63-
// Load up our handlers and repositories
64-
$ganalyticsRepository = new GanalyticsRepository();
65-
$gaTagHandler = new GanalyticsJsHandler($this->module, $this->context);
66-
$productWrapper = new ProductWrapper($this->context);
67-
$orderWrapper = new OrderWrapper($this->context);
68-
6965
// Process manual orders instantly, we have their IDs in cookie
7066
$gaScripts .= $this->processManualOrders();
7167

72-
// TEMP RETURN
73-
return $gaTagHandler->generate($gaScripts);
68+
// Backload old orders that failed to load normally
69+
$gaScripts .= $this->backloadFailedOrders();
7470

75-
return;
76-
dump($this->context);
77-
dump($_POST);
78-
dump(Tools::getValue('id_order'));
79-
dump($_GET);
80-
die;
81-
82-
// TEMP RETURN
83-
84-
// Backload all unsuccessfully sent orders into our database
85-
if ($this->context->controller->controller_name == 'AdminOrders') {
86-
if (Tools::getValue('id_order')) {
87-
$order = new Order((int) Tools::getValue('id_order'));
88-
if (Validate::isLoadedObject($order) && strtotime('+1 day', strtotime($order->date_add)) > time()) {
89-
$gaOrderSent = $ganalyticsRepository->findGaOrderByOrderId((int) Tools::getValue('id_order'));
90-
if ($gaOrderSent === false) {
91-
// Add order to repository, so we can later mark it as sent
92-
$ganalyticsRepository->addOrder((int) $order->id, (int) $order->id_shop);
93-
}
94-
}
95-
} else {
96-
$gaOrderRecords = $ganalyticsRepository->findAllByShopIdAndDateAdd((int) $this->context->shop->id);
97-
98-
if ($gaOrderRecords) {
99-
foreach ($gaOrderRecords as $row) {
100-
$orderData = $orderWrapper->wrapOrder($row['id_order']);
101-
if (!empty($orderData)) {
102-
// Mark it as successfully sent
103-
$ganalyticsRepository->markOrderAsSent((int) $row['id_order']);
104-
105-
// Add payment data
106-
$gaScripts .= $this->module->getTools()->renderEvent(
107-
'add_payment_info',
108-
[
109-
'currency' => $orderData['currency'],
110-
'payment_type' => $orderData['payment_type'],
111-
]
112-
);
113-
114-
// Render purchase
115-
$gaScripts .= $this->module->getTools()->renderPurchaseEvent(
116-
[],
117-
$orderData,
118-
$this->context->link->getAdminLink('AdminGanalyticsAjax')
119-
);
120-
}
121-
}
122-
}
123-
}
124-
}
125-
126-
return $gaTagHandler->generate($gaScripts);
71+
return $gaScripts;
12772
}
12873

12974
/**
@@ -134,19 +79,51 @@ public function setParams($params)
13479
$this->params = $params;
13580
}
13681

82+
/**
83+
* Checks if there are any orders that failed to be sent normally through front office and processes them
84+
*/
85+
protected function backloadFailedOrders()
86+
{
87+
$gaScripts = '';
88+
if (empty(Configuration::get('GA_BACKLOAD_ENABLED'))) {
89+
return $gaScripts;
90+
}
91+
92+
// Check for value on how long back we will get them
93+
$backloadDays = (int) Configuration::get('GA_BACKLOAD_DAYS');
94+
if ($backloadDays < 1) {
95+
return $gaScripts;
96+
}
97+
98+
// Get all failed orders (either not present in our table or not sent)
99+
// We go GA_BACKLOAD_DAYS into the past and at least 30 minutes old
100+
$failedOrders = Db::getInstance()->ExecuteS(
101+
'SELECT DISTINCT o.id_order, g.sent FROM `' . _DB_PREFIX_ . 'orders` o
102+
LEFT JOIN `' . _DB_PREFIX_ . GanalyticsRepository::TABLE_NAME . '` g ON o.id_order = g.id_order
103+
WHERE (g.sent IS NULL OR g.sent = 0) AND o.date_add BETWEEN NOW() - INTERVAL ' . $backloadDays .' DAY AND NOW() - INTERVAL 30 MINUTE'
104+
);
105+
106+
// Process each failed order
107+
foreach ($failedOrders as $idOrder) {
108+
$gaScripts .= $this->processOrder((int) $idOrder);
109+
}
110+
111+
return $gaScripts;
112+
}
113+
137114
/**
138115
* Checks if there are any manual orders in cookie and processes them
139116
*/
140117
protected function processManualOrders()
141118
{
119+
$gaScripts = '';
142120
$adminOrders = $this->context->cookie->__get('ga_admin_order');
143121
if (empty($adminOrders)) {
144-
return;
122+
return $gaScripts;
145123
}
146124

147125
// Separate them by IDs and process one by one
148126
$adminOrders = explode(",", $adminOrders);
149-
$gaScripts = '';
150127
foreach ($adminOrders as $idOrder) {
151128
$gaScripts .= $this->processOrder((int) $idOrder);
152129
}

upgrade/upgrade-5.0.0.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,9 @@
2626
*/
2727
function upgrade_module_5_0_0($object)
2828
{
29-
return Configuration::deleteByName('GA_V4_ENABLED') && $object->registerHook('actionValidateOrder');
29+
return
30+
Configuration::deleteByName('GA_V4_ENABLED') &&
31+
$object->registerHook('actionValidateOrder') &&
32+
Configuration::updateValue('GA_BACKLOAD_ENABLED', false) &&
33+
Configuration::updateValue('GA_BACKLOAD_DAYS', 30);
3034
}

0 commit comments

Comments
 (0)