forked from PrestaShop/ganalytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathganalytics.php
executable file
·669 lines (573 loc) · 20.9 KB
/
ganalytics.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
<?php
/**
* 2007-2014 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (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:
* http://opensource.org/licenses/afl-3.0.php
* 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 license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2014 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_PS_VERSION_'))
exit;
class Ganalytics extends Module
{
protected $js_state = 0;
protected $eligible = 0;
protected $filterable = 1;
public function __construct()
{
$this->name = 'ganalytics';
$this->tab = 'analytics_stats';
$this->version = '2.0.3';
$this->author = 'PrestaShop';
$this->module_key = 'fd2aaefea84ac1bb512e6f1878d990b8';
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Google Analytics');
$this->description = $this->l('Gain clear insights into important metrics about your customers, using Google Analytics');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall Google Analytics? You will lose all the data related to this module.');
/* Backward compatibility */
if (version_compare(_PS_VERSION_, '1.5', '<'))
require(_PS_MODULE_DIR_.$this->name.'/backward_compatibility/backward.php');
}
public function install()
{
if (version_compare(_PS_VERSION_, '1.5', '>=') && Shop::isFeatureActive())
Shop::setContext(Shop::CONTEXT_ALL);
if (!parent::install() || !$this->registerHook('header') || !$this->registerHook('adminOrder')
|| !$this->registerHook('footer') || !$this->registerHook('home')
|| !$this->registerHook('productfooter') || !$this->registerHook('top')
|| !$this->registerHook('backOfficeHeader'))
return false;
if (version_compare(_PS_VERSION_, '1.5', '>=')
&& (!$this->registerHook('actionProductCancel') || !$this->registerHook('actionCartSave')))
return false;
Db::getInstance()->Execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'ganalytics`');
if (!Db::getInstance()->Execute('
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'ganalytics` (
`id_google_analytics` int(11) NOT NULL AUTO_INCREMENT,
`id_order` int(11) NOT NULL,
`sent` tinyint(1) DEFAULT NULL,
`date_add` datetime DEFAULT NULL,
PRIMARY KEY (`id_google_analytics`),
KEY `id_order` (`id_order`),
KEY `sent` (`sent`)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 AUTO_INCREMENT=1'))
return $this->uninstall();
return true;
}
public function uninstall()
{
if (!parent::uninstall())
return false;
return Db::getInstance()->Execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'ganalytics`');
}
public function displayForm()
{
// Get default language
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$helper = new HelperForm();
// Module, token and currentIndex
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
// Language
$helper->default_form_language = $default_lang;
$helper->allow_employee_form_lang = $default_lang;
// Title and toolbar
$helper->title = $this->displayName;
$helper->show_toolbar = true; // false -> remove toolbar
$helper->toolbar_scroll = true; // yes - > Toolbar is always visible on the top of the screen.
$helper->submit_action = 'submit'.$this->name;
$helper->toolbar_btn = array(
'save' =>
array(
'desc' => $this->l('Save'),
'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
'&token='.Tools::getAdminTokenLite('AdminModules'),
),
'back' => array(
'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
'desc' => $this->l('Back to list')
)
);
$fields_form = array();
// Init Fields form array
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Settings'),
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Google Analytics Tracking ID'),
'name' => 'GA_ACCOUNT_ID',
'size' => 20,
'required' => true,
'hint' => $this->l('This information is available in your Google Analytics account')
),
),
'submit' => array(
'title' => $this->l('Save'),
)
);
// Load current value
$helper->fields_value['GA_ACCOUNT_ID'] = Configuration::get('GA_ACCOUNT_ID');
return $helper->generateForm($fields_form);
}
/**
* back office module configuration page content
*/
public function getContent()
{
$output = '';
if (Tools::isSubmit('submit'.$this->name))
{
$ga_account_id = Tools::getValue('GA_ACCOUNT_ID');
if (!empty($ga_account_id))
{
Configuration::updateValue('GA_ACCOUNT_ID', $ga_account_id);
$output .= $this->displayConfirmation($this->l('Settings updated successfully'));
}
}
if (version_compare(_PS_VERSION_, '1.5', '>='))
$output .= $this->displayForm();
else
{
$this->context->smarty->assign(array(
'account_id' => Configuration::get('GA_ACCOUNT_ID'),
));
$output .= $this->display(__FILE__, 'views/templates/admin/form-ps14.tpl');
}
return $this->display(__FILE__, 'views/templates/admin/configuration.tpl').$output;
}
private function _getGoogleAnalyticsTag($back_office = false)
{
return '
<script type="text/javascript">
(window.gaDevIds=window.gaDevIds||[]).push(\'d6YPbH\');
(function(i,s,o,g,r,a,m){i[\'GoogleAnalyticsObject\']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,\'script\',\'//www.google-analytics.com/analytics.js\',\'ga\');
ga(\'create\', \''.Tools::safeOutput(Configuration::get('GA_ACCOUNT_ID')).'\', \'auto\');
'.($back_office ? 'ga(\'require\', \'ecommerce\');' : '').'
ga(\'require\', \'ec\');
</script>';
}
public function hookHeader()
{
if (Configuration::get('GA_ACCOUNT_ID'))
{
$this->context->controller->addJs($this->_path.'views/js/GoogleAnalyticActionLib.js');
return $this->_getGoogleAnalyticsTag();
}
}
/**
* Return a detailed transaction for Google Analytics
*/
public function wrapOrder($id_order)
{
$order = new Order((int)$id_order);
if (Validate::isLoadedObject($order))
return array(
'orderid' => $id_order,
'storename' => $this->context->shop->name,
'grandtotal' => $order->total_paid,
'shipping' => $order->total_shipping,
'tax' => $order->total_paid_tax_incl,
'url' => $this->context->link->getModuleLink('ganalytics', 'ajax'));
}
/**
* To track transactions
*/
public function hookTop()
{
// Add Google Analytics order - Only on Order's confirmation page
$controller_name = Tools::getValue('controller');
if ($controller_name == 'order-confirmation')
{
$ga_order_sent = Db::getInstance()->getValue('SELECT sent FROM `'._DB_PREFIX_.'ganalytics` WHERE id_order = '.(int)$this->context->controller->id_order);
if ($ga_order_sent === false)
{
$order = new Order($this->context->controller->id_order);
if (!Validate::isLoadedObject($order))
return;
$order_products = array();
foreach ($order->getProducts() as $order_product)
$order_products[] = $this->wrapProduct((int)$order_product['product_id'], array('qty' => $order_product['product_quantity']), 0, true);
Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'ganalytics` (id_order, sent, date_add) VALUES ('.(int)$this->context->controller->id_order.', 0, NOW())');
$ga_order_sent = 0;
$transaction = array(
'orderid' => $order->reference,
'affiliation' => $this->context->shop->name,
'revenue' => $order->total_paid,
'shipping' => $order->total_shipping,
'tax' => $order->total_paid_tax_incl - $order->total_paid_tax_excl,
'url' => $this->context->link->getModuleLink('ganalytics', 'ajax'));
$ga_scripts = $this->addTransaction($order_products, $transaction);
return $this->_runJs($ga_scripts);
}
}
}
/**
* hook footer to load JS script for standards actions such as product clicks
*/
public function hookFooter()
{
$ga_scripts = '';
if (isset($this->context->cookie->ga_cart))
{
$this->filterable = 0;
$ga_scripts .= $this->context->cookie->ga_cart;
unset($this->context->cookie->ga_cart);
}
$controller_name = Tools::getValue('controller');
if ($controller_name == 'order')
{
$this->eligible = 1;
$step = Tools::getValue('step');
if (empty($step))
$step = 0;
$products = $this->wrapProducts($this->context->smarty->getTemplateVars('products'), array(), true);
$ga_scripts .= $this->addProductFromCheckout($products, $step);
$ga_scripts .= 'MBG.addCheckout(\''.(int)$step.'\');';
}
if ($controller_name == 'order-confirmation')
$this->eligible = 1;
if (isset($products) && count($products))
{
if ($this->eligible == 0)
$ga_scripts .= $this->addProductImpression($products);
$ga_scripts .= $this->addProductClick($products);
}
return $this->_runJs($ga_scripts);
}
protected function filter($ga_scripts)
{
if ($this->filterable = 1)
return implode(';', array_unique(explode(';', $ga_scripts)));
return $ga_scripts;
}
/**
* hook home to display generate the product list associated to home featured, news products and best sellers Modules
*/
public function hookHome()
{
$ga_scripts = '';
// Home featured products
if ($this->isModuleEnabled('homefeatured'))
{
$category = new Category($this->context->shop->getCategory(), $this->context->language->id);
$home_featured_products = $this->wrapProducts($category->getProducts((int)Context::getContext()->language->id, 1,
(Configuration::get('HOME_FEATURED_NBR') ? (int)Configuration::get('HOME_FEATURED_NBR') : 8), 'position'), array(), true);
$ga_scripts .= $this->addProductImpression($home_featured_products).$this->addProductClick($home_featured_products);
}
// New products
if ($this->isModuleEnabled('blocknewproducts') && (Configuration::get('PS_NB_DAYS_NEW_PRODUCT') || Configuration::get('PS_BLOCK_NEWPRODUCTS_DISPLAY')))
{
$new_products = Product::getNewProducts((int)$this->context->language->id, 0, (int)Configuration::get('NEW_PRODUCTS_NBR'));
$new_products_list = $this->wrapProducts($new_products, array(), true);
$ga_scripts .= $this->addProductImpression($new_products_list).$this->addProductClick($new_products_list);
}
// Best Sellers
if ($this->isModuleEnabled('blockbestsellers') && (!Configuration::get('PS_CATALOG_MODE') || Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY')))
{
$ga_homebestsell_product_list = $this->wrapProducts(ProductSale::getBestSalesLight((int)$this->context->language->id, 0, 8), array(), true);
$ga_scripts .= $this->addProductImpression($ga_homebestsell_product_list).$this->addProductClick($ga_homebestsell_product_list);
}
return $this->_runJs($this->filter($ga_scripts));
}
/**
* hook home to display generate the product list associated to home featured, news products and best sellers Modules
*/
public function isModuleEnabled($name)
{
if (version_compare(_PS_VERSION_, '1.5', '>='))
return Module::isEnabled($name);
else
{
$module = Module::getInstanceByName($name);
return ($module && $module->active === true);
}
}
/**
* wrap products to provide a standard products information for google analytics script
*/
public function wrapProducts($products, $extras = array(), $full = false)
{
$result_products = array();
if (!is_array($products))
return;
$currency = new Currency($this->context->currency->id);
$usetax = (Product::getTaxCalculationMethod((int)$this->context->customer->id) != PS_TAX_EXC);
foreach ($products as $index => $product)
{
if (!isset($product['price']))
$product['price'] = (float)Tools::displayPrice(Product::getPriceStatic((int)$product['id_product'], $usetax), $currency);
$result_products[] = $this->wrapProduct($product, $extras, $index, $full);
}
return $result_products;
}
/**
* wrap product to provide a standard product information for google analytics script
*/
public function wrapProduct($product, $extras, $index = 0, $full = false)
{
$ga_product = '';
$variant = null;
if (isset($product['attributes_small']))
$variant = $product['attributes_small'];
elseif (isset($extras['attributes_small']))
$variant = $extras['attributes_small'];
$product_qty = 1;
if (isset($extras['qty']))
$product_qty = $extras['qty'];
elseif (isset($product['cart_quantity']))
$product_qty = $product['cart_quantity'];
if ($full)
{
$product_type = 'typical';
if (isset($product['pack']) && $product['pack'] == 1)
$product_type = 'pack';
elseif (isset($product['virtual']) && $product['virtual'] == 1)
$product_type = 'virtual';
$ga_product = array(
'id' => isset($product['reference']) ? $product['reference'] : $product['id_product'],
'name' => $product['name'],
'category' => $product['category'],
'brand' => isset($product['manufacturer_name']) ? $product['manufacturer_name'] : '',
'variant' => $variant,
'type' => $product_type,
'position' => $index ? $index : '0',
'quantity' => $product_qty,
'list' => Tools::getValue('controller'),
'url' => isset($product['link']) ? $product['link'] : '',
'price' => number_format($product['price'], '2')
);
}
return $ga_product;
}
/**
* add order transaction
*/
public function addTransaction($products, $order)
{
if (!is_array($products))
return;
$js = '';
foreach ($products as $product)
$js .= 'MBG.add('.Tools::jsonEncode($product).');';
return $js.'MBG.addTransaction('.Tools::jsonEncode($order).');';
}
/**
* add product impression js and product click js
*/
public function addProductImpression($products)
{
if (!is_array($products))
return;
$js = '';
foreach ($products as $product)
$js .= 'MBG.add('.Tools::jsonEncode($product).",'',true);";
return $js;
}
public function addProductClick($products)
{
if (!is_array($products))
return;
$js = '';
foreach ($products as $product)
$js .= 'MBG.addProductClick('.Tools::jsonEncode($product).');';
return $js;
}
public function addProductClickByHttpReferal($products)
{
if (!is_array($products))
return;
$js = '';
foreach ($products as $product)
$js .= 'MBG.addProductClickByHttpReferal('.Tools::jsonEncode($product).');';
return $js;
}
/**
* Add product checkout info
*/
public function addProductFromCheckout($products)
{
if (!is_array($products))
return;
$js = '';
foreach ($products as $product)
$js .= 'MBG.add('.Tools::jsonEncode($product).');';
return $js;
}
/**
* hook product page footer to load JS for product details view
*/
public function hookFooterProduct($params)
{
$controller_name = Tools::getValue('controller');
if ($controller_name == 'product')
{
// Add product view
$ga_product = $this->wrapProduct((array)$params['product'], null, 0, true);
$js = 'MBG.addProductDetailView('.Tools::jsonEncode($ga_product).');';
if (isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) > 0)
$js .= $this->addProductClickByHttpReferal(array($ga_product));
return $this->_runJs($js);
}
}
/**
* Generate Google Analytics js
*/
private function _runJs($js_code)
{
if (Configuration::get('GA_ACCOUNT_ID'))
{
if ($this->js_state != 1 && !defined('_PS_ADMIN_DIR_'))
$js_code .= 'ga(\'send\', \'pageview\');';
return '
<script type="text/javascript">
jQuery(document).ready(function(){
var MBG = GoogleAnalyticEnhancedECommerce;
MBG.setCurrency(\''.Tools::safeOutput($this->context->currency->iso_code).'\');
'.$js_code.'
});
</script>';
}
}
/**
* Hook admin order to send transactions and refunds details
*/
public function hookAdminOrder()
{
echo $this->_runJs($this->context->cookie->ga_admin_refund);
unset($this->context->cookie->ga_admin_refund);
}
/**
* admin office header to add google analytics js
*/
public function hookBackOfficeHeader()
{
$js = '';
if (strcmp(Tools::getValue('configure'), $this->name) === 0)
{
if (version_compare(_PS_VERSION_, '1.5', '>') == true)
{
$this->context->controller->addCSS($this->_path.'views/css/ganalytics.css');
if (version_compare(_PS_VERSION_, '1.6', '<') == true)
$this->context->controller->addCSS($this->_path.'views/css/ganalytics-nobootstrap.css');
}
else
{
$js .= '<link rel="stylesheet" href="'.$this->_path.'views/css/ganalytics.css" type="text/css" />'.
'<link rel="stylesheet" href="'.$this->_path.'views/css/ganalytics-nobootstrap.css" type="text/css" />';
}
}
$ga_account_id = Configuration::get('GA_ACCOUNT_ID');
if (!empty($ga_account_id))
{
if (version_compare(_PS_VERSION_, '1.5', '>=') == true)
$this->context->controller->addJs($this->_path.'views/js/GoogleAnalyticActionLib.js');
else
$js .= '<script type="text/javascript" src="'.$this->_path.'views/js/GoogleAnalyticActionLib.js"></script>';
$this->context->smarty->assign('GA_ACCOUNT_ID', $ga_account_id);
$ga_scripts = '';
$ga_order_records = Db::getInstance()->ExecuteS('SELECT * FROM `'._DB_PREFIX_.'ganalytics` WHERE sent = 0');
if ($ga_order_records)
foreach ($ga_order_records as $row)
{
$transaction = $this->wrapOrder($row['id_order']);
if (!empty($transaction))
{
$transaction = Tools::jsonEncode($transaction);
$ga_scripts .= 'MBG.addTransaction('.$transaction.');';
}
}
return $js.$this->_getGoogleAnalyticsTag(true).$this->_runJs($ga_scripts);
}
else return $js;
}
/**
* Hook admin office header to add google analytics js
*/
public function hookActionProductCancel($params)
{
$qty_refunded = Tools::getValue('cancelQuantity');
$ga_scripts = '';
foreach ($qty_refunded as $orderdetail_id => $qty)
{
// Display GA refund product
$order_detail = new OrderDetail($orderdetail_id);
$ga_scripts .= 'MBG.add('.Tools::jsonEncode(array('id' => $order_detail->product_reference, 'quantity' => $qty)).');';
}
$this->context->cookie->ga_admin_refund = $ga_scripts.'MBG.refundByProduct('.Tools::jsonEncode(array('id' => $params['order']->reference)).');';
}
/**
* hook save cart event to implement addtocart and remove from cart functionality
*/
public function hookActionCartSave()
{
if (!isset($this->context->cart))
return;
$ga_scripts = '';
$cart = array(
'controller' => Tools::getValue('controller'),
'addAction' => Tools::getValue('add') ? 'add' : '',
'removeAction' => Tools::getValue('delete') ? 'delete' : '',
'extraAction' => Tools::getValue('op'),
'qty' => (int)Tools::getValue('qty', 1)
);
$cart_products = $this->context->cart->getProducts();
if (isset($cart_products) && count($cart_products))
foreach ($cart_products as $cart_product)
if ($cart_product['id_product'] == Tools::getValue('id_product'))
$add_product = $cart_product;
if ($cart['removeAction'] == 'delete')
{
$add_product_object = new Product((int)Tools::getValue('id_product'), true, (int)Configuration::get('PS_LANG_DEFAULT'));
if (Validate::isLoadedObject($add_product_object))
{
$add_product['name'] = $add_product_object->name;
$add_product['manufacturer_name'] = $add_product_object->manufacturer_name;
$add_product['category'] = $add_product_object->category;
$add_product['reference'] = $add_product_object->reference;
$add_product['link_rewrite'] = $add_product_object->link_rewrite;
$add_product['link'] = $add_product_object->link_rewrite;
$add_product['price'] = $add_product_object->price;
$add_product['ean13'] = $add_product_object->ean13;
$add_product['id_product'] = Tools::getValue('id_product');
$add_product['id_category_default'] = $add_product_object->id_category_default;
$add_product['out_of_stock'] = $add_product_object->out_of_stock;
$add_product = Product::getProductProperties((int)Configuration::get('PS_LANG_DEFAULT'), $add_product);
}
}
if (isset($add_product))
{
$ga_products = $this->wrapProduct($add_product, array(), 0, true);
if ($cart['removeAction'] == 'delete' || $cart['extraAction'] == 'down')
$ga_scripts .= 'MBG.removeFromCart('.Tools::jsonEncode($ga_products).');';
elseif (Tools::getValue('step') <= 0) // Sometimes cartsave is called in checkout
$ga_scripts .= 'MBG.addToCart('.Tools::jsonEncode($ga_products).');';
$this->context->cookie->ga_cart .= $ga_scripts;
}
}
}