@@ -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 }
0 commit comments