diff --git a/googleanalytics.admin.inc b/googleanalytics.admin.inc index d4174f5..f0db964 100644 --- a/googleanalytics.admin.inc +++ b/googleanalytics.admin.inc @@ -340,6 +340,15 @@ function googleanalytics_admin_settings_form($form_state) { '#collapsed' => TRUE, ); + $form['advanced']['googleanalytics_tracking_library'] = array( + '#type' => 'radios', + '#title' => t('Which tracking library should be used?'), + '#options' => array( + 0 => t('Google Analytics Library (default)') . '
' . t('Use the standard Google Analytics tracking library') . '
', + 1 => t('Google Doubleclick Library') . '
' . t('Use the alternative Google Doubleclick tracking library to enable AdWords remarketing features. If you choose this option you will need to update your privacy policy.', array('@doubleclick' => 'http://support.google.com/analytics/bin/answer.py?hl=en&answer=2444872', '@privacy' => 'http://support.google.com/analytics/bin/answer.py?hl=en&answer=2636405')) . '
'), + '#default_value' => variable_get('googleanalytics_tracking_library', 0), + ); + $form['advanced']['googleanalytics_tracker_anonymizeip'] = array( '#type' => 'checkbox', '#title' => t('Anonymize visitors IP address'), diff --git a/googleanalytics.install b/googleanalytics.install index d63b042..566bfdf 100644 --- a/googleanalytics.install +++ b/googleanalytics.install @@ -54,6 +54,7 @@ function googleanalytics_uninstall() { variable_del('googleanalytics_tracker_anonymizeip'); variable_del('googleanalytics_trackfiles'); variable_del('googleanalytics_trackfiles_extensions'); + variable_del('googleanalytics_tracking_library'); variable_del('googleanalytics_trackmailto'); variable_del('googleanalytics_trackoutbound'); variable_del('googleanalytics_trackoutboundaspageview'); diff --git a/googleanalytics.module b/googleanalytics.module index 96edfa7..5be7f8e 100644 --- a/googleanalytics.module +++ b/googleanalytics.module @@ -259,8 +259,16 @@ function googleanalytics_page_alter(&$page) { $script .= 'ga.type = "text/javascript";'; $script .= 'ga.async = true;'; + // Which version of the tracking library should be used? + $library = '.google-analytics.com/ga.js'; + $library_cache_path = 'www' . $library; + if(variable_get('googleanalytics_tracking_library', 0)) { + $library = 'stats.g.doubleclick.net/dc.js'; + $library_cache_path = $library; + } + // Should a local cached copy of ga.js be used? - if (variable_get('googleanalytics_cache', 0) && $url = _googleanalytics_cache('http://www.google-analytics.com/ga.js')) { + if (variable_get('googleanalytics_cache', 0) && $url = _googleanalytics_cache('http://' . $library_cache_path)) { // A dummy query-string is added to filenames, to gain control over // browser-caching. The string changes on every update or full cache // flush, forcing browsers to load a new copy of the files, as the @@ -270,7 +278,12 @@ function googleanalytics_page_alter(&$page) { $script .= 'ga.src = "' . $url . $query_string . '";'; } else { - $script .= 'ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";'; + // Library paths do not follow the same naming convention + if(variable_get('googleanalytics_tracking_library', 0)) { + $script .= 'ga.src = ("https:" == document.location.protocol ? "https://" : "http://") + "'. $library .'";'; + } else { + $script .= 'ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + "'. $library .'";'; + } } $script .= 'var s = document.getElementsByTagName("script")[0];'; $script .= 's.parentNode.insertBefore(ga, s);'; diff --git a/googleanalytics.test b/googleanalytics.test index 9f8aec9..c6eb3df 100644 --- a/googleanalytics.test +++ b/googleanalytics.test @@ -118,6 +118,11 @@ class GoogleAnalyticsBasicTest extends DrupalWebTestCase { $this->drupalGet(''); $this->assertRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsTrackingCode]: Latest tracking code used.'); + // Test whether the alternate doubleclick library is used + variable_set('googleanalytics_tracking_library', 1); + $this->drupalGet(''); + $this->assertRaw('stats.g.doubleclick.net/dc.js', '[testGoogleAnalyticsTrackingCode]: Doubleclick tracking code used.'); + // Test whether anonymize visitors IP address feature has been enabled. $this->drupalGet(''); $this->assertNoRaw('_gaq.push(["_gat._anonymizeIp"]);', '[testGoogleAnalyticsTrackingCode]: Anonymize visitors IP address not found on frontpage.');