Skip to content

Commit c80b709

Browse files
authored
Merge pull request #150 from Hlavtox/fix-ga4
Fix remaining issues in order tracking
2 parents 2da9939 + 2ebb024 commit c80b709

21 files changed

Lines changed: 468 additions & 193 deletions

classes/Database/Install.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222

2323
use Configuration;
2424
use Db;
25+
use Language;
2526
use Ps_Googleanalytics;
2627
use Shop;
28+
use Tab;
2729

2830
class Install
2931
{
@@ -87,6 +89,8 @@ public function installTables()
8789
public function setDefaultConfiguration()
8890
{
8991
Configuration::updateValue('GA_CANCELLED_STATES', json_encode([Configuration::get('PS_OS_CANCELED')]));
92+
Configuration::updateValue('GA_BACKLOAD_ENABLED', false);
93+
Configuration::updateValue('GA_BACKLOAD_DAYS', 30);
9094

9195
return true;
9296
}
@@ -104,9 +108,30 @@ public function registerHooks()
104108
$this->module->registerHook('displayFooterProduct') &&
105109
$this->module->registerHook('displayOrderConfirmation') &&
106110
$this->module->registerHook('actionProductCancel') &&
111+
$this->module->registerHook('actionValidateOrder') &&
107112
$this->module->registerHook('actionOrderStatusPostUpdate') &&
108113
$this->module->registerHook('actionCartSave') &&
109114
$this->module->registerHook('displayBackOfficeHeader') &&
110115
$this->module->registerHook('actionCarrierProcess');
111116
}
117+
118+
/**
119+
* Installs hidden tab for our ajax controller
120+
*
121+
* @return bool
122+
*/
123+
public function installTab()
124+
{
125+
$tab = new Tab();
126+
$tab->class_name = 'AdminGanalyticsAjax';
127+
$tab->module = $this->module->name;
128+
$tab->active = true;
129+
$tab->id_parent = -1;
130+
$tab->name = array_fill_keys(
131+
Language::getIDs(false),
132+
$this->module->displayName
133+
);
134+
135+
return $tab->add();
136+
}
112137
}

classes/Database/Uninstall.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
namespace PrestaShop\Module\Ps_Googleanalytics\Database;
2222

2323
use Db;
24+
use Tab;
25+
use Validate;
2426

2527
class Uninstall
2628
{
@@ -42,4 +44,21 @@ public function uninstallTables()
4244

4345
return true;
4446
}
47+
48+
/**
49+
* uninstall tab
50+
*
51+
* @return bool
52+
*/
53+
public function uninstallTab()
54+
{
55+
$result = true;
56+
$id_tab = (int) Tab::getIdFromClassName('AdminGanalyticsAjax');
57+
$tab = new Tab($id_tab);
58+
if (Validate::isLoadedObject($tab)) {
59+
$result = $tab->delete();
60+
}
61+
62+
return $result;
63+
}
4564
}

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/GoogleAnalyticsTools.php

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,38 +42,39 @@ public function filter($gaScripts, $filterable)
4242
}
4343

4444
/**
45-
* add order transaction
45+
* Renders purchase event for order
4646
*
47-
* @param array $products
48-
* @param array $transaction
47+
* @param array $orderProducts
48+
* @param array $orderData
49+
* @param string $callbackUrl
4950
*
5051
* @return string|void
5152
*/
52-
public function addTransaction($products, $transaction)
53+
public function renderPurchaseEvent($orderProducts, $orderData, $callbackUrl)
5354
{
54-
if (!is_array($products)) {
55+
if (!is_array($orderProducts)) {
5556
return;
5657
}
5758

5859
$callbackData = [
59-
'orderid' => $transaction['id'],
60-
'customer' => $transaction['customer'],
60+
'orderid' => $orderData['id'],
61+
'customer' => $orderData['customer'],
6162
];
6263

6364
$eventData = [
64-
'transaction_id' => (int) $transaction['id'],
65-
'affiliation' => $transaction['affiliation'],
66-
'value' => (float) $transaction['revenue'],
67-
'tax' => (float) $transaction['tax'],
68-
'shipping' => (float) $transaction['shipping'],
69-
'currency' => $transaction['currency'],
65+
'transaction_id' => (int) $orderData['id'],
66+
'affiliation' => $orderData['affiliation'],
67+
'value' => (float) $orderData['revenue'],
68+
'tax' => (float) $orderData['tax'],
69+
'shipping' => (float) $orderData['shipping'],
70+
'currency' => $orderData['currency'],
7071
'items' => [],
7172
'event_callback' => "function() {
72-
$.get('" . $transaction['url'] . "', " . json_encode($callbackData, JSON_UNESCAPED_UNICODE) . ');
73+
$.get('" . $callbackUrl . "', " . json_encode($callbackData, JSON_UNESCAPED_UNICODE) . ');
7374
}',
7475
];
7576

76-
foreach ($products as $product) {
77+
foreach ($orderProducts as $product) {
7778
$eventData['items'][] = [
7879
'item_id' => (int) $product['id'],
7980
'item_name' => $product['name'],
@@ -82,13 +83,11 @@ public function addTransaction($products, $transaction)
8283
];
8384
}
8485

85-
$js = $this->renderEvent(
86+
return $this->renderEvent(
8687
'purchase',
8788
$eventData,
8889
['event_callback']
8990
);
90-
91-
return $js;
9291
}
9392

9493
/**

classes/Hook/HookActionOrderStatusPostUpdate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function run()
7575
if ($gaRefundSent === false) {
7676
// We refund it and set the "sent" flag to true
7777
$js = $this->getGoogleAnalytics4($this->params['id_order']);
78-
$this->context->cookie->__set('ga_admin_refund', $js);
78+
$this->context->cookie->ga_admin_refund = $js;
7979
$this->context->cookie->write();
8080

8181
// We save this information to database

classes/Hook/HookActionProductCancel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function run()
7171
$orderDetail->product_name
7272
);
7373

74-
$this->context->cookie->__set('ga_admin_refund', $js);
74+
$this->context->cookie->ga_admin_refund = $js;
7575
$this->context->cookie->write();
7676
}
7777

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA 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.md.
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 Since 2007 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 Context;
24+
use Ps_Googleanalytics;
25+
26+
class HookActionValidateOrder implements HookInterface
27+
{
28+
/**
29+
* @var Ps_Googleanalytics
30+
*/
31+
private $module;
32+
33+
/**
34+
* @var Context
35+
*/
36+
private $context;
37+
38+
/**
39+
* @var array
40+
*/
41+
private $params;
42+
43+
public function __construct(Ps_Googleanalytics $module, Context $context)
44+
{
45+
$this->module = $module;
46+
$this->context = $context;
47+
}
48+
49+
/**
50+
* run
51+
*
52+
* @return void
53+
*/
54+
public function run()
55+
{
56+
// Check if we are creating backoffice order
57+
if ($this->context->controller->controller_name != 'AdminOrders' && $this->context->controller->controller_name != 'Admin') {
58+
return;
59+
}
60+
61+
// Mark this ID to immediately display it on next page load
62+
$order = $this->params['order'];
63+
64+
// We are checking this, because in case of multishipping, there could be multiple orders
65+
if (!empty($this->context->cookie->ga_admin_order)) {
66+
$ga_admin_order = sprintf(
67+
'%1$s,%2$s',
68+
$this->context->cookie->ga_admin_order,
69+
$order->id
70+
);
71+
} else {
72+
$ga_admin_order = $order->id;
73+
}
74+
$this->context->cookie->ga_admin_order = $ga_admin_order;
75+
$this->context->cookie->write();
76+
}
77+
78+
/**
79+
* setParams
80+
*
81+
* @param array $params
82+
*/
83+
public function setParams($params)
84+
{
85+
$this->params = $params;
86+
}
87+
}

0 commit comments

Comments
 (0)