@@ -136,6 +136,9 @@ protected function deleteTables()
136136
137137 public function displayForm ()
138138 {
139+ // Check if multistore is active
140+ $ is_multistore_active = Shop::isFeatureActive ();
141+
139142 // Get default language
140143 $ default_lang = (int )Configuration::get ('PS_LANG_DEFAULT ' );
141144
@@ -188,8 +191,7 @@ public function displayForm()
188191 'type ' => 'switch ' ,
189192 'label ' => $ this ->l ('Enable User ID tracking ' ),
190193 'name ' => 'GA_USERID_ENABLED ' ,
191- '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 ' ),
192- 'values ' => array (
194+ 'values ' => array (
193195 array (
194196 'id ' => 'ga_userid_enabled ' ,
195197 'value ' => 1 ,
@@ -199,8 +201,7 @@ public function displayForm()
199201 'id ' => 'ga_userid_disabled ' ,
200202 'value ' => 0 ,
201203 'label ' => $ this ->l ('Disabled ' )
202- ),
203- ),
204+ ))
204205 ),
205206 array (
206207 'type ' => 'switch ' ,
@@ -225,10 +226,31 @@ public function displayForm()
225226 'title ' => $ this ->l ('Save ' ),
226227 )
227228 );
229+
230+ if ($ is_multistore_active ) {
231+ $ fields_form [0 ]['form ' ]['input ' ][] = array (
232+ 'type ' => 'switch ' ,
233+ 'label ' => $ this ->l ('Enable Cross-Domain tracking ' ),
234+ 'name ' => 'GA_CROSSDOMAIN_ENABLED ' ,
235+ 'values ' => array (
236+ array (
237+ 'id ' => 'ga_crossdomain_enabled ' ,
238+ 'value ' => 1 ,
239+ 'label ' => $ this ->l ('Enabled ' )
240+ ),
241+ array (
242+ 'id ' => 'ga_crossdomain_disabled ' ,
243+ 'value ' => 0 ,
244+ 'label ' => $ this ->l ('Disabled ' )
245+ )
246+ )
247+ );
248+ }
228249
229250 // Load current value
230251 $ helper ->fields_value ['GA_ACCOUNT_ID ' ] = Configuration::get ('GA_ACCOUNT_ID ' );
231252 $ helper ->fields_value ['GA_USERID_ENABLED ' ] = Configuration::get ('GA_USERID_ENABLED ' );
253+ $ helper ->fields_value ['GA_CROSSDOMAIN_ENABLED ' ] = Configuration::get ('GA_CROSSDOMAIN_ENABLED ' );
232254 $ helper ->fields_value ['GA_ANONYMIZE_ENABLED ' ] = Configuration::get ('GA_ANONYMIZE_ENABLED ' );
233255
234256 return $ helper ->generateForm ($ fields_form );
@@ -252,6 +274,13 @@ public function getContent()
252274 Configuration::updateValue ('GA_USERID_ENABLED ' , (bool )$ ga_userid_enabled );
253275 $ output .= $ this ->displayConfirmation ($ this ->trans ('Settings for User ID updated successfully ' , array (), 'Modules.GAnalytics.Admin ' ));
254276 }
277+
278+ $ ga_crossdomain_enabled = Tools::getValue ('GA_CROSSDOMAIN_ENABLED ' );
279+ if (null !== $ ga_crossdomain_enabled ) {
280+ Configuration::updateValue ('GA_CROSSDOMAIN_ENABLED ' , (bool )$ ga_crossdomain_enabled );
281+ $ output .= $ this ->displayConfirmation ($ this ->trans ('Settings for User ID updated successfully ' , array (), 'Modules.GAnalytics.Admin ' ));
282+ }
283+
255284 $ ga_anonymize_enabled = Tools::getValue ('GA_ANONYMIZE_ENABLED ' );
256285 if (null !== $ ga_anonymize_enabled ) {
257286 Configuration::updateValue ('GA_ANONYMIZE_ENABLED ' , (bool )$ ga_anonymize_enabled );
@@ -264,36 +293,41 @@ public function getContent()
264293 return $ this ->display (__FILE__ , './views/templates/admin/configuration.tpl ' ).$ output ;
265294 }
266295
267- protected function _getGoogleAnalyticsTag ($ back_office = false )
268- {
269- $ user_id = null ;
270- if (Configuration::get ('GA_USERID_ENABLED ' ) &&
271- $ this ->context ->customer && $ this ->context ->customer ->isLogged ()
272- ) {
273- $ user_id = (int )$ this ->context ->customer ->id ;
274- }
275-
276- return '
277- <script type="text/javascript">
278- (window.gaDevIds=window.gaDevIds||[]).push( \'d6YPbH \');
279- (function(i,s,o,g,r,a,m){i[ \'GoogleAnalyticsObject \']=r;i[r]=i[r]||function(){
280- (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
281- m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
282- })(window,document, \'script \', \'https://www.google-analytics.com/analytics.js \', \'ga \');
283- ga( \'create \', \'' .Tools::safeOutput (Configuration::get ('GA_ACCOUNT_ID ' )).'\', \'auto \');
284- ga( \'require \', \'ec \'); '
285- .(($ user_id && !$ back_office ) ? 'ga( \'set \', \'userId \', \'' .$ user_id .'\'); ' : '' )
286- .($ back_office ? 'ga( \'set \', \'nonInteraction \', true); ' : '' )
287- .(Configuration::get ('GA_ANONYMIZE_ENABLED ' ) ? 'ga( \'set \', \'anonymizeIp \', true); ' : '' )
288- .'</script> ' ;
289- }
290-
291- public function hookdisplayHeader ($ params )
296+ public function hookdisplayHeader ($ params , $ back_office = false )
292297 {
293298 if (Configuration::get ('GA_ACCOUNT_ID ' )) {
294299 $ this ->context ->controller ->addJs ($ this ->_path .'views/js/GoogleAnalyticActionLib.js ' );
300+
301+ $ shops = Shop::getShops ();
302+ $ is_multistore_active = Shop::isFeatureActive ();
303+
304+ $ current_shop_id = (int )Context::getContext ()->shop ->id ;
305+
306+ $ user_id = null ;
307+ $ ga_crossdomain_enabled = false ;
308+
309+ if (Configuration::get ('GA_USERID_ENABLED ' ) &&
310+ $ this ->context ->customer && $ this ->context ->customer ->isLogged ()
311+ ) {
312+ $ user_id = (int )$ this ->context ->customer ->id ;
313+ }
314+
315+ if ((int )Configuration::get ('GA_CROSSDOMAIN_ENABLED ' ) && $ is_multistore_active && sizeof ($ shops ) > 1 ) {
316+ $ ga_crossdomain_enabled = true ;
317+ }
295318
296- return $ this ->_getGoogleAnalyticsTag ();
319+ $ this ->smarty ->assign (
320+ array (
321+ 'backOffice ' => $ back_office ,
322+ 'currentShopId ' => $ current_shop_id ,
323+ 'userId ' => $ user_id ,
324+ 'gaAccountId ' => Tools::safeOutput (Configuration::get ('GA_ACCOUNT_ID ' )),
325+ 'shops ' => $ shops ,
326+ 'gaCrossdomainEnabled ' => $ ga_crossdomain_enabled ,
327+ 'useSecureMode ' => Configuration::get ('PS_SSL_ENABLED ' )
328+ )
329+ );
330+ return $ this ->display (__FILE__ , 'ps_googleanalytics.tpl ' );
297331 }
298332 }
299333
@@ -359,7 +393,6 @@ public function hookdisplayFooter()
359393 {
360394 $ ga_scripts = '' ;
361395 $ this ->js_state = 0 ;
362-
363396 $ gacarts = $ this ->_manageData ("" , "R " );
364397 if (count ($ gacarts )>0 ) {
365398 $ this ->filterable = 0 ;
@@ -390,7 +423,6 @@ public function hookdisplayFooter()
390423 $ ga_scripts .= 'MBG.addCheckout( \'' .(int )$ step .'\'); ' ;
391424 }
392425
393-
394426 $ confirmation_hook_id = (int )Hook::getIdByName ('displayOrderConfirmation ' );
395427 if (isset (Hook::$ executed_hooks [$ confirmation_hook_id ])) {
396428 $ this ->eligible = 1 ;
@@ -761,7 +793,7 @@ public function hookdisplayBackOfficeHeader()
761793 }
762794 }
763795 }
764- return $ js .$ this ->_getGoogleAnalyticsTag ( true ).$ this ->_runJs ($ ga_scripts , 1 );
796+ return $ js .$ this ->hookdisplayHeader ( null , true ).$ this ->_runJs ($ ga_scripts , 1 );
765797 } else {
766798 return $ js ;
767799 }
0 commit comments