Skip to content

Commit 4ae2510

Browse files
committed
[MO] Added cross-domain tracking for multistore configurations
1 parent 109a279 commit 4ae2510

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

ps_googleanalytics.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,22 @@ public function displayForm()
182182
'label' => $this->l('Disabled')
183183
))
184184
),
185+
array(
186+
'type' => 'switch',
187+
'label' => $this->l('Enable Cross-Domain tracking'),
188+
'name' => 'GA_CROSSDOMAIN_ENABLED',
189+
'values' => array(
190+
array(
191+
'id' => 'ga_crossdomain_enabled',
192+
'value' => 1,
193+
'label' => $this->l('Enabled')
194+
),
195+
array(
196+
'id' => 'ga_crossdomain_disabled',
197+
'value' => 0,
198+
'label' => $this->l('Disabled')
199+
))
200+
),
185201
),
186202
'submit' => array(
187203
'title' => $this->l('Save'),
@@ -191,6 +207,7 @@ public function displayForm()
191207
// Load current value
192208
$helper->fields_value['GA_ACCOUNT_ID'] = Configuration::get('GA_ACCOUNT_ID');
193209
$helper->fields_value['GA_USERID_ENABLED'] = Configuration::get('GA_USERID_ENABLED');
210+
$helper->fields_value['GA_CROSSDOMAIN_ENABLED'] = Configuration::get('GA_CROSSDOMAIN_ENABLED');
194211

195212
return $helper->generateForm($fields_form);
196213
}
@@ -213,6 +230,11 @@ public function getContent()
213230
Configuration::updateValue('GA_USERID_ENABLED', (bool)$ga_userid_enabled);
214231
$output .= $this->displayConfirmation($this->trans('Settings for User ID updated successfully', array(), 'Modules.GAnalytics.Admin'));
215232
}
233+
$ga_crossdomain_enabled = Tools::getValue('GA_CROSSDOMAIN_ENABLED');
234+
if (null !== $ga_crossdomain_enabled) {
235+
Configuration::updateValue('GA_CROSSDOMAIN_ENABLED', (bool)$ga_crossdomain_enabled);
236+
$output .= $this->displayConfirmation($this->trans('Settings for User ID updated successfully', array(), 'Modules.GAnalytics.Admin'));
237+
}
216238
}
217239

218240
$output .= $this->displayForm();
@@ -229,22 +251,29 @@ public function hookdisplayHeader($params, $back_office = false)
229251
$shops = Shop::getShops();
230252
$is_multistore_active = Shop::isFeatureActive();
231253

254+
$current_shop_id = (int)Context::getContext()->shop->id;
255+
232256
$user_id = null;
257+
$ga_crossdomain_enabled = false;
233258

234259
if (Configuration::get('GA_USERID_ENABLED') &&
235260
$this->context->customer && $this->context->customer->isLogged()
236261
) {
237262
$user_id = (int)$this->context->customer->id;
238263
}
239264

240-
265+
if ((int)Configuration::get('GA_CROSSDOMAIN_ENABLED') && $is_multistore_active) {
266+
$ga_crossdomain_enabled = true;
267+
}
268+
241269
$this->smarty->assign(
242270
array(
243271
'backOffice' => $back_office,
272+
'currentShopId' => $current_shop_id,
244273
'userId' => $user_id,
245274
'gaAccountId' => Tools::safeOutput(Configuration::get('GA_ACCOUNT_ID')),
246-
'multiStoreActive' => $is_multistore_active,
247-
'shops' => $shops
275+
'shops' => $shops,
276+
'gaCrossdomainEnabled' => $ga_crossdomain_enabled
248277
)
249278
);
250279
return $this->display(__FILE__, 'ps_googleanalytics.tpl');

views/templates/hook/ps_googleanalytics.tpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@
3232
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
3333
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
3434
{/literal}
35-
{if $multiStoreActive}
35+
{if $gaCrossdomainEnabled}
3636
ga('create', '{$gaAccountId}', 'auto', {literal}{'allowLinker': true}{/literal});
3737
ga('require', 'linker');
3838
ga('linker:autoLink', [
3939
{foreach from=$shops item=shop}
40+
{if $shop.id_shop != $currentShopId}
4041
'{if $use_secure_more}{$shop.domain_ssl}{else}{$shop.domain}{/if}',
42+
{/if}
4143
{/foreach}
4244
]);
4345
{else}

0 commit comments

Comments
 (0)