Skip to content

Commit 4738768

Browse files
Merge pull request #5 from manfield/dev
MO: Introduced Cross Domain tracking
2 parents 7052236 + 55e4904 commit 4738768

3 files changed

Lines changed: 158 additions & 32 deletions

File tree

ps_googleanalytics.php

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ protected function deleteTables()
118118

119119
public function displayForm()
120120
{
121+
// Check if multistore is active
122+
$is_multistore_active = Shop::isFeatureActive();
123+
121124
// Get default language
122125
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
123126

@@ -167,11 +170,10 @@ public function displayForm()
167170
'hint' => $this->l('This information is available in your Google Analytics account')
168171
),
169172
array(
170-
'type' => 'radio',
173+
'type' => 'switch',
171174
'label' => $this->l('Enable User ID tracking'),
172175
'name' => 'GA_USERID_ENABLED',
173-
'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'),
174-
'values' => array(
176+
'values' => array(
175177
array(
176178
'id' => 'ga_userid_enabled',
177179
'value' => 1,
@@ -181,18 +183,38 @@ public function displayForm()
181183
'id' => 'ga_userid_disabled',
182184
'value' => 0,
183185
'label' => $this->l('Disabled')
184-
),
185-
),
186+
))
186187
),
187188
),
188189
'submit' => array(
189190
'title' => $this->l('Save'),
190191
)
191192
);
193+
194+
if ($is_multistore_active) {
195+
$fields_form[0]['form']['input'][] = array(
196+
'type' => 'switch',
197+
'label' => $this->l('Enable Cross-Domain tracking'),
198+
'name' => 'GA_CROSSDOMAIN_ENABLED',
199+
'values' => array(
200+
array(
201+
'id' => 'ga_crossdomain_enabled',
202+
'value' => 1,
203+
'label' => $this->l('Enabled')
204+
),
205+
array(
206+
'id' => 'ga_crossdomain_disabled',
207+
'value' => 0,
208+
'label' => $this->l('Disabled')
209+
)
210+
)
211+
);
212+
}
192213

193214
// Load current value
194215
$helper->fields_value['GA_ACCOUNT_ID'] = Configuration::get('GA_ACCOUNT_ID');
195216
$helper->fields_value['GA_USERID_ENABLED'] = Configuration::get('GA_USERID_ENABLED');
217+
$helper->fields_value['GA_CROSSDOMAIN_ENABLED'] = Configuration::get('GA_CROSSDOMAIN_ENABLED');
196218

197219
return $helper->generateForm($fields_form);
198220
}
@@ -215,42 +237,54 @@ public function getContent()
215237
Configuration::updateValue('GA_USERID_ENABLED', (bool)$ga_userid_enabled);
216238
$output .= $this->displayConfirmation($this->trans('Settings for User ID updated successfully', array(), 'Modules.GAnalytics.Admin'));
217239
}
240+
$ga_crossdomain_enabled = Tools::getValue('GA_CROSSDOMAIN_ENABLED');
241+
if (null !== $ga_crossdomain_enabled) {
242+
Configuration::updateValue('GA_CROSSDOMAIN_ENABLED', (bool)$ga_crossdomain_enabled);
243+
$output .= $this->displayConfirmation($this->trans('Settings for User ID updated successfully', array(), 'Modules.GAnalytics.Admin'));
244+
}
218245
}
219246

220247
$output .= $this->displayForm();
221248

222249
return $this->display(__FILE__, './views/templates/admin/configuration.tpl').$output;
223250
}
224251

225-
protected function _getGoogleAnalyticsTag($back_office = false)
226-
{
227-
$user_id = null;
228-
if (Configuration::get('GA_USERID_ENABLED') &&
229-
$this->context->customer && $this->context->customer->isLogged()
230-
) {
231-
$user_id = (int)$this->context->customer->id;
232-
}
233-
234-
return '
235-
<script type="text/javascript">
236-
(window.gaDevIds=window.gaDevIds||[]).push(\'d6YPbH\');
237-
(function(i,s,o,g,r,a,m){i[\'GoogleAnalyticsObject\']=r;i[r]=i[r]||function(){
238-
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
239-
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
240-
})(window,document,\'script\',\'https://www.google-analytics.com/analytics.js\',\'ga\');
241-
ga(\'create\', \''.Tools::safeOutput(Configuration::get('GA_ACCOUNT_ID')).'\', \'auto\');
242-
ga(\'require\', \'ec\');'
243-
.(($user_id && !$back_office) ? 'ga(\'set\', \'userId\', \''.$user_id.'\');': '')
244-
.($back_office ? 'ga(\'set\', \'nonInteraction\', true);' : '')
245-
.'</script>';
246-
}
247252

248-
public function hookdisplayHeader($params)
253+
public function hookdisplayHeader($params, $back_office = false)
249254
{
250255
if (Configuration::get('GA_ACCOUNT_ID')) {
251256
$this->context->controller->addJs($this->_path.'views/js/GoogleAnalyticActionLib.js');
257+
258+
$shops = Shop::getShops();
259+
$is_multistore_active = Shop::isFeatureActive();
260+
261+
$current_shop_id = (int)Context::getContext()->shop->id;
262+
263+
$user_id = null;
264+
$ga_crossdomain_enabled = false;
265+
266+
if (Configuration::get('GA_USERID_ENABLED') &&
267+
$this->context->customer && $this->context->customer->isLogged()
268+
) {
269+
$user_id = (int)$this->context->customer->id;
270+
}
271+
272+
if ((int)Configuration::get('GA_CROSSDOMAIN_ENABLED') && $is_multistore_active && sizeof($shops) > 1) {
273+
$ga_crossdomain_enabled = true;
274+
}
252275

253-
return $this->_getGoogleAnalyticsTag();
276+
$this->smarty->assign(
277+
array(
278+
'backOffice' => $back_office,
279+
'currentShopId' => $current_shop_id,
280+
'userId' => $user_id,
281+
'gaAccountId' => Tools::safeOutput(Configuration::get('GA_ACCOUNT_ID')),
282+
'shops' => $shops,
283+
'gaCrossdomainEnabled' => $ga_crossdomain_enabled,
284+
'useSecureMode' => Configuration::get('PS_SSL_ENABLED')
285+
)
286+
);
287+
return $this->display(__FILE__, 'ps_googleanalytics.tpl');
254288
}
255289
}
256290

@@ -316,11 +350,11 @@ public function hookdisplayFooter()
316350
{
317351
$ga_scripts = '';
318352
$this->js_state = 0;
319-
320353
if (isset($this->context->cookie->ga_cart)) {
321354
$this->filterable = 0;
322-
355+
323356
$gacarts = json_decode($this->context->cookie->ga_cart, true);
357+
324358
if (is_array($gacarts)) {
325359
foreach ($gacarts as $gacart) {
326360
if ($gacart['quantity'] > 0) {
@@ -688,7 +722,7 @@ public function hookdisplayBackOfficeHeader()
688722
}
689723
}
690724
}
691-
return $js.$this->_getGoogleAnalyticsTag(true).$this->_runJs($ga_scripts, 1);
725+
return $js.$this->hookdisplayHeader(null, true).$this->_runJs($ga_scripts, 1);
692726
} else {
693727
return $js;
694728
}

views/templates/hook/index.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/*
3+
* 2007-2015 PrestaShop
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Academic Free License (AFL 3.0)
8+
* that is bundled with this package in the file LICENSE.txt.
9+
* It is also available through the world-wide-web at this URL:
10+
* http://opensource.org/licenses/afl-3.0.php
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+
* DISCLAIMER
16+
*
17+
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
18+
* versions in the future. If you wish to customize PrestaShop for your
19+
* needs please refer to http://www.prestashop.com for more information.
20+
*
21+
* @author PrestaShop SA <contact@prestashop.com>
22+
* @copyright 2007-2015 PrestaShop SA
23+
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
24+
* International Registered Trademark & Property of PrestaShop SA
25+
*/
26+
27+
header('Expires: Mon, 26 Jul 1998 05:00:00 GMT');
28+
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
29+
30+
header('Cache-Control: no-store, no-cache, must-revalidate');
31+
header('Cache-Control: post-check=0, pre-check=0', false);
32+
header('Pragma: no-cache');
33+
34+
header('Location: ../');
35+
exit;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{*
2+
* 2007-2017 PrestaShop
3+
*
4+
* NOTICE OF LICENSE
5+
*
6+
* This source file is subject to the Academic Free License (AFL 3.0)
7+
* that is bundled with this package in the file LICENSE.txt.
8+
* It is also available through the world-wide-web at this URL:
9+
* http://opensource.org/licenses/afl-3.0.php
10+
* If you did not receive a copy of the license and are unable to
11+
* obtain it through the world-wide-web, please send an email
12+
* to license@prestashop.com so we can send you a copy immediately.
13+
*
14+
* DISCLAIMER
15+
*
16+
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
17+
* versions in the future. If you wish to customize PrestaShop for your
18+
* needs please refer to http://www.prestashop.com for more information.
19+
*
20+
* @author PrestaShop SA <contact@prestashop.com>
21+
* @copyright 2007-2017 PrestaShop SA
22+
* @version Release: $Revision:7040 $
23+
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
24+
* International Registered Trademark & Property of PrestaShop SA
25+
*}
26+
27+
{literal}
28+
<script type="text/javascript">
29+
(window.gaDevIds=window.gaDevIds||[]).push('d6YPbH');
30+
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
31+
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
32+
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
33+
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
34+
{/literal}
35+
{if $gaCrossdomainEnabled}
36+
ga('create', '{$gaAccountId|escape:'htmlall':'UTF-8'}', 'auto', {literal}{'allowLinker': true}{/literal});
37+
ga('require', 'linker');
38+
ga('linker:autoLink', [
39+
{foreach from=$shops item=shop}
40+
{if $shop.id_shop != $currentShopId}
41+
{if $useSecureMode}'{$shop.domain_ssl|escape:'htmlall':'UTF-8'}'{else}'{$shop.domain|escape:'htmlall':'UTF-8'}'{/if},
42+
{/if}
43+
{/foreach}
44+
]);
45+
{else}
46+
ga('create', '{$gaAccountId|escape:'htmlall':'UTF-8'}', 'auto');
47+
{/if}
48+
{if $userId && !$backOffice}
49+
ga('set', 'userId', '{$userId|escape:'htmlall':'UTF-8'}');
50+
{/if}
51+
{if $backOffice}
52+
ga('set', 'nonInteraction', true);
53+
{/if}
54+
{literal}
55+
ga('require', 'ec');
56+
</script>
57+
{/literal}

0 commit comments

Comments
 (0)