forked from PrestaShop/ps_googleanalytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps_googleanalytics.php
329 lines (291 loc) · 10 KB
/
ps_googleanalytics.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?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 [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA <[email protected]>
* @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;
}
$autoloadPath = __DIR__ . '/vendor/autoload.php';
if (file_exists($autoloadPath)) {
require_once $autoloadPath;
}
class Ps_Googleanalytics extends Module
{
public $name;
public $tab;
public $version;
public $ps_versions_compliancy;
public $author;
public $module_key;
public $bootstrap;
public $displayName;
public $description;
public $confirmUninstall;
public $products = [];
public $_debug = 0;
private $tools = null;
private $dataHandler = null;
public function __construct()
{
$this->name = 'ps_googleanalytics';
$this->tab = 'analytics_stats';
$this->version = '5.0.2';
$this->ps_versions_compliancy = ['min' => '1.7.7', 'max' => _PS_VERSION_];
$this->author = 'PrestaShop';
$this->module_key = 'fd2aaefea84ac1bb512e6f1878d990b8';
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->trans('Google Analytics', [], 'Modules.Googleanalytics.Admin');
$this->description = $this->trans('Gain clear insights into important metrics about your customers, using Google Analytics', [], 'Modules.Googleanalytics.Admin');
$this->confirmUninstall = $this->trans('Are you sure you want to uninstall Google Analytics? You will lose all the data related to this module.', [], 'Modules.Googleanalytics.Admin');
}
/**
* Back office module configuration page content
*/
public function getContent()
{
$configurationForm = new PrestaShop\Module\Ps_Googleanalytics\Form\ConfigurationForm($this);
$output = '';
if (Tools::isSubmit('submit' . $this->name)) {
$output .= $configurationForm->treat();
}
$output .= $this->display(__FILE__, './views/templates/admin/configuration.tpl');
$output .= $configurationForm->generate();
return $output;
}
public function hookDisplayHeader($params, $back_office = false)
{
try {
$hook = new PrestaShop\Module\Ps_Googleanalytics\Hooks\HookDisplayHeader($this, $this->context);
$hook->setBackOffice($back_office);
return $hook->run();
} catch (Exception $e) {
error_log($e->getMessage());
}
}
/**
* Confirmation page hook.
* This function is run to track transactions.
*/
public function hookDisplayOrderConfirmation($params)
{
try {
$hook = new PrestaShop\Module\Ps_Googleanalytics\Hooks\HookDisplayOrderConfirmation($this, $this->context);
$hook->setParams($params);
return $hook->run();
} catch (Exception $e) {
error_log($e->getMessage());
}
}
/**
* Footer hook
* This function is run to load JS script for standards actions such as product clicks
*/
public function hookDisplayBeforeBodyClosingTag()
{
try {
$hook = new PrestaShop\Module\Ps_Googleanalytics\Hooks\HookDisplayBeforeBodyClosingTag($this, $this->context);
return $hook->run();
} catch (Exception $e) {
error_log($e->getMessage());
}
}
/**
* Product page footer hook
* This function is run to load JS for product details view
*/
public function hookDisplayFooterProduct()
{
try {
$hook = new PrestaShop\Module\Ps_Googleanalytics\Hooks\HookDisplayFooterProduct($this, $this->context);
return $hook->run();
} catch (Exception $e) {
error_log($e->getMessage());
}
}
/**
* Hook admin order.
* This function is run to send transactions and refunds details
*/
public function hookDisplayAdminOrder()
{
try {
$gaTagHandler = new PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler($this, $this->context);
$output = $gaTagHandler->generate($this->context->cookie->ga_admin_refund);
unset($this->context->cookie->ga_admin_refund);
$this->context->cookie->write();
return $output;
} catch (Exception $e) {
error_log($e->getMessage());
}
}
/**
* Admin office header hook.
* This function is run to add Google Analytics JavaScript
*/
public function hookDisplayBackOfficeHeader()
{
try {
$hook = new PrestaShop\Module\Ps_Googleanalytics\Hooks\HookDisplayBackOfficeHeader($this, $this->context);
return $hook->run();
} catch (Exception $e) {
error_log($e->getMessage());
}
}
/**
* Product cancel action hook (in Back office).
* This function is run to add Google Analytics JavaScript
*/
public function hookActionProductCancel($params)
{
try {
$hook = new PrestaShop\Module\Ps_Googleanalytics\Hooks\HookActionProductCancel($this, $this->context);
$hook->setParams($params);
$hook->run();
} catch (Exception $e) {
error_log($e->getMessage());
}
}
/**
* Hook used to detect backoffice orders and store their IDs into cookie.
*/
public function hookActionValidateOrder($params)
{
try {
$hook = new PrestaShop\Module\Ps_Googleanalytics\Hooks\HookActionValidateOrder($this, $this->context);
$hook->setParams($params);
$hook->run();
} catch (Exception $e) {
error_log($e->getMessage());
}
}
/**
* Hook called after order status change, used to "refund" order after cancelling it
*/
public function hookActionOrderStatusPostUpdate($params)
{
try {
$hook = new PrestaShop\Module\Ps_Googleanalytics\Hooks\HookActionOrderStatusPostUpdate($this, $this->context);
$hook->setParams($params);
$hook->run();
} catch (Exception $e) {
error_log($e->getMessage());
}
}
/**
* Hook to process add and remove items from cart events
* This function is run to implement 'add to cart' and 'remove from cart' functionalities
*/
public function hookActionCartUpdateQuantityBefore($params)
{
try {
$hook = new PrestaShop\Module\Ps_Googleanalytics\Hooks\HookActionCartUpdateQuantityBefore($this, $this->context);
$hook->setParams($params);
$hook->run();
} catch (Exception $e) {
error_log($e->getMessage());
}
}
/**
* Hook to process remove items from cart events
* This function is run to implement 'remove from cart' functionalities
*/
public function hookActionObjectProductInCartDeleteBefore($params)
{
try {
$hook = new PrestaShop\Module\Ps_Googleanalytics\Hooks\HookActionObjectProductInCartDeleteBefore($this, $this->context);
$hook->setParams($params);
$hook->run();
} catch (Exception $e) {
error_log($e->getMessage());
}
}
public function hookActionCarrierProcess($params)
{
try {
$hook = new PrestaShop\Module\Ps_Googleanalytics\Hooks\HookActionCarrierProcess($this, $this->context);
$hook->setParams($params);
$hook->run();
} catch (Exception $e) {
error_log($e->getMessage());
}
}
protected function _debugLog($function, $log)
{
if (!$this->_debug) {
return true;
}
$myFile = _PS_MODULE_DIR_ . $this->name . '/logs/analytics.log';
$fh = fopen($myFile, 'a');
fwrite($fh, date('F j, Y, g:i a') . ' ' . $function . "\n");
fwrite($fh, print_r($log, true) . "\n\n");
fclose($fh);
}
/**
* This method is triggered at the installation of the module
* - it installs all module tables
* - it registers the hooks used by this module
*
* @return bool
*/
public function install()
{
$database = new PrestaShop\Module\Ps_Googleanalytics\Database\Install($this);
return parent::install() &&
$database->registerHooks() &&
$database->setDefaultConfiguration() &&
$database->installTab() &&
$database->installTables();
}
/**
* Triggered at the uninstall of the module
* - erases this module SQL tables
*
* @return bool
*/
public function uninstall()
{
$database = new PrestaShop\Module\Ps_Googleanalytics\Database\Uninstall();
return parent::uninstall() &&
$database->uninstallTab() &&
$database->uninstallTables();
}
/**
* Returns instance of GoogleAnalyticsTools
*/
public function getTools()
{
if ($this->tools === null) {
$this->tools = new PrestaShop\Module\Ps_Googleanalytics\GoogleAnalyticsTools();
}
return $this->tools;
}
/**
* Returns instance of GanalyticsDataHandler
*/
public function getDataHandler()
{
if ($this->dataHandler === null) {
$this->dataHandler = new PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsDataHandler(
$this->context->cart->id,
$this->context->shop->id
);
}
return $this->dataHandler;
}
}