Skip to content

Commit b5f9bef

Browse files
[*] MO : Remove usage of cookie - adding anonymizeIp option
1 parent 311dcc0 commit b5f9bef

1 file changed

Lines changed: 73 additions & 22 deletions

File tree

ps_googleanalytics.php

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* 2007-2017 PrestaShop
3+
* 2007-2018 PrestaShop
44
*
55
* NOTICE OF LICENSE
66
*
@@ -19,7 +19,7 @@
1919
* needs please refer to http://www.prestashop.com for more information.
2020
*
2121
* @author PrestaShop SA <contact@prestashop.com>
22-
* @copyright 2007-2017 PrestaShop SA
22+
* @copyright 2007-2018 PrestaShop SA
2323
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
2424
* International Registered Trademark & Property of PrestaShop SA
2525
*/
@@ -40,7 +40,7 @@ public function __construct()
4040
{
4141
$this->name = 'ps_googleanalytics';
4242
$this->tab = 'analytics_stats';
43-
$this->version = '3.0.3';
43+
$this->version = '3.1.0';
4444
$this->ps_versions_compliancy = array('min' => '1.7.0.0', 'max' => _PS_VERSION_);
4545
$this->author = 'PrestaShop';
4646
$this->module_key = 'fd2aaefea84ac1bb512e6f1878d990b8';
@@ -167,7 +167,7 @@ public function displayForm()
167167
'hint' => $this->l('This information is available in your Google Analytics account')
168168
),
169169
array(
170-
'type' => 'radio',
170+
'type' => 'switch',
171171
'label' => $this->l('Enable User ID tracking'),
172172
'name' => 'GA_USERID_ENABLED',
173173
'hint' => $this->l('The User ID is set at the property level. To find a property, click Admin, then select an account and a property. From the Property column, click Tracking Info then User ID'),
@@ -184,6 +184,24 @@ public function displayForm()
184184
),
185185
),
186186
),
187+
array(
188+
'type' => 'switch',
189+
'label' => $this->l('Anonymize IP'),
190+
'name' => 'GA_ANONYMIZE_ENABLED',
191+
'hint' => $this->l('Use this option to anonymize the visitor’s IP to comply with data privacy laws in some countries'),
192+
'values' => array(
193+
array(
194+
'id' => 'ga_anonymize_enabled',
195+
'value' => 1,
196+
'label' => $this->l('Enabled')
197+
),
198+
array(
199+
'id' => 'ga_anonymize_disabled',
200+
'value' => 0,
201+
'label' => $this->l('Disabled')
202+
),
203+
),
204+
),
187205
),
188206
'submit' => array(
189207
'title' => $this->l('Save'),
@@ -193,6 +211,7 @@ public function displayForm()
193211
// Load current value
194212
$helper->fields_value['GA_ACCOUNT_ID'] = Configuration::get('GA_ACCOUNT_ID');
195213
$helper->fields_value['GA_USERID_ENABLED'] = Configuration::get('GA_USERID_ENABLED');
214+
$helper->fields_value['GA_ANONYMIZE_ENABLED'] = Configuration::get('GA_ANONYMIZE_ENABLED');
196215

197216
return $helper->generateForm($fields_form);
198217
}
@@ -215,6 +234,11 @@ public function getContent()
215234
Configuration::updateValue('GA_USERID_ENABLED', (bool)$ga_userid_enabled);
216235
$output .= $this->displayConfirmation($this->trans('Settings for User ID updated successfully', array(), 'Modules.GAnalytics.Admin'));
217236
}
237+
$ga_anonymize_enabled = Tools::getValue('GA_ANONYMIZE_ENABLED');
238+
if (null !== $ga_anonymize_enabled) {
239+
Configuration::updateValue('GA_ANONYMIZE_ENABLED', (bool)$ga_anonymize_enabled);
240+
$output .= $this->displayConfirmation($this->trans('Settings for Anonymize IP updated successfully', array(), 'Modules.GAnalytics.Admin'));
241+
}
218242
}
219243

220244
$output .= $this->displayForm();
@@ -242,6 +266,7 @@ protected function _getGoogleAnalyticsTag($back_office = false)
242266
ga(\'require\', \'ec\');'
243267
.(($user_id && !$back_office) ? 'ga(\'set\', \'userId\', \''.$user_id.'\');': '')
244268
.($back_office ? 'ga(\'set\', \'nonInteraction\', true);' : '')
269+
.(Configuration::get('GA_ANONYMIZE_ENABLED') ? 'ga(\'set\', \'anonymizeIp\', true);' : '')
245270
.'</script>';
246271
}
247272

@@ -317,21 +342,19 @@ public function hookdisplayFooter()
317342
$ga_scripts = '';
318343
$this->js_state = 0;
319344

320-
if (isset($this->context->cookie->ga_cart)) {
345+
$gacarts = $this->_manageData("", "R");
346+
if (count($gacarts)>0) {
321347
$this->filterable = 0;
322348

323-
$gacarts = json_decode($this->context->cookie->ga_cart, true);
324-
if (is_array($gacarts)) {
325-
foreach ($gacarts as $gacart) {
326-
if ($gacart['quantity'] > 0) {
327-
$ga_scripts .= 'MBG.addToCart('.json_encode($gacart).');';
328-
} elseif ($gacart['quantity'] < 0) {
329-
$gacart['quantity'] = abs($gacart['quantity']);
330-
$ga_scripts .= 'MBG.removeFromCart('.json_encode($gacart).');';
331-
}
349+
foreach ($gacarts as $gacart) {
350+
if ($gacart['quantity'] > 0) {
351+
$ga_scripts .= 'MBG.addToCart('.json_encode($gacart).');';
352+
} elseif ($gacart['quantity'] < 0) {
353+
$gacart['quantity'] = abs($gacart['quantity']);
354+
$ga_scripts .= 'MBG.removeFromCart('.json_encode($gacart).');';
332355
}
333356
}
334-
unset($this->context->cookie->ga_cart);
357+
$gacarts = $this->_manageData("", "D");
335358
}
336359

337360
$controller_name = Tools::getValue('controller');
@@ -637,6 +660,38 @@ protected function _runJs($js_code, $backoffice = 0)
637660
}
638661
}
639662

663+
/**
664+
* Manage data
665+
* @param string $action "R" read data from DB, "W" write data, "A" append data, D" delete data
666+
* @return array dans le cas du R, sinon true
667+
*/
668+
protected function _manageData($data, $action)
669+
{
670+
if ($action == 'R') {
671+
$dataretour = Db::getInstance()->getValue('SELECT data FROM `'._DB_PREFIX_.'ganalytics_data` WHERE id_cart = \''.(int)$this->context->cart->id.'\' AND id_shop = \''.(int)$this->context->shop->id.'\'');
672+
if ($dataretour === false)
673+
return array();
674+
else
675+
return json_decode($dataretour,true);
676+
}
677+
if ($action == 'W') {
678+
return Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'ganalytics_data` (id_cart, id_shop, data) VALUES(\''.(int)$this->context->cart->id.'\',\''.(int)$this->context->shop->id.'\',\''.json_encode($data).'\') ON DUPLICATE KEY UPDATE data =\''.json_encode($data).'\' ;');
679+
}
680+
if ($action == 'A') {
681+
$dataretour = Db::getInstance()->getValue('SELECT data FROM `'._DB_PREFIX_.'ganalytics_data` WHERE id_cart = \''.(int)$this->context->cart->id.'\' AND id_shop = \''.(int)$this->context->shop->id.'\'');
682+
if ($dataretour === false)
683+
$datanew = array($data);
684+
else {
685+
$datanew = json_decode($dataretour,true);
686+
$datanew[] = $data;
687+
}
688+
return Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'ganalytics_data` (id_cart, id_shop, data) VALUES(\''.(int)$this->context->cart->id.'\',\''.(int)$this->context->shop->id.'\',\''.json_encode($datanew).'\') ON DUPLICATE KEY UPDATE data =\''.serialize($datanew).'\' ;');
689+
}
690+
if ($action == 'D') {
691+
Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'ganalytics_data` WHERE id_cart = \''.(int)$this->context->cart->id.'\' AND id_shop = \''.(int)$this->context->shop->id.'\'');
692+
}
693+
}
694+
640695
/**
641696
* Hook admin order to send transactions and refunds details
642697
*/
@@ -774,11 +829,7 @@ public function hookactionCartSave()
774829
$id_product = Tools::getValue('id_product');
775830
}
776831

777-
if (isset($this->context->cookie->ga_cart)) {
778-
$gacart = json_decode($this->context->cookie->ga_cart, true);
779-
} else {
780-
$gacart = array();
781-
}
832+
$gacart = $this->_manageData("", "R");
782833

783834
if ($cart['removeAction'] == 'delete') {
784835
$ga_products['quantity'] = -1;
@@ -795,15 +846,15 @@ public function hookactionCartSave()
795846
}
796847

797848
$gacart[$id_product] = $ga_products;
798-
$this->context->cookie->ga_cart = json_encode($gacart);
849+
$this->_manageData($gacart, 'W');
799850
}
800851
}
801852

802853
public function hookactionCarrierProcess($params)
803854
{
804855
if (isset($params['cart']->id_carrier)) {
805856
$carrier_name = Db::getInstance()->getValue('SELECT name FROM `'._DB_PREFIX_.'carrier` WHERE id_carrier = '.(int)$params['cart']->id_carrier);
806-
$this->context->cookie->ga_cart .= 'MBG.addCheckoutOption(2,\''.$carrier_name.'\');';
857+
$this->_manageData('MBG.addCheckoutOption(2,\''.$carrier_name.'\');', 'A');
807858
}
808859
}
809860

0 commit comments

Comments
 (0)