Commit a33c820b authored by hass's avatar hass
Browse files

#1833596: Add basic support for Analytics.js (port is incomplete, lack

of API docs and features, see @todo's)
parent 70096c48
......@@ -72,5 +72,5 @@ code textarea. These can be found on the official Google Analytics pages
and a few examples at http://drupal.org/node/248699. Support is not
provided for any customisations you include.
To speed up page loading you may also cache the Analytics ga.js
To speed up page loading you may also cache the Google Analytics "analytics.js"
file locally.
......@@ -237,6 +237,7 @@ function googleanalytics_admin_settings_form($form_state) {
'#default_value' => variable_get('googleanalytics_site_search', FALSE),
'#disabled' => (module_exists('search') ? FALSE : TRUE),
);
/* @todo: not supported, https://support.google.com/analytics/bin/answer.py?hl=en&hlrm=de&answer=2795983
$form['tracking']['search_and_advertising']['googleanalytics_trackadsense'] = array(
'#type' => 'checkbox',
'#title' => t('Track AdSense ads'),
......@@ -248,7 +249,7 @@ function googleanalytics_admin_settings_form($form_state) {
'#title' => t('Track DoubleClick data'),
'#description' => t('If checked, the alternative Google <a href="@doubleclick">DoubleClick data tracking</a> is used to enable AdWords remarketing features. If you choose this option you will need to <a href="@privacy">update your privacy policy</a>.', array('@doubleclick' => url('http://support.google.com/analytics/bin/answer.py', array('query' => array('answer' => '2444872'))), '@privacy' => url('http://support.google.com/analytics/bin/answer.py', array('query' => array('answer' => '2636405'))))),
'#default_value' => variable_get('googleanalytics_trackdoubleclick', FALSE),
);
); */
// Privacy specific configurations.
$form['tracking']['privacy'] = array(
......@@ -269,6 +270,7 @@ function googleanalytics_admin_settings_form($form_state) {
);
// Custom variables.
/* @todo: Update to custom dimensions.
$form['googleanalytics_custom_var'] = array(
'#collapsed' => TRUE,
'#collapsible' => TRUE,
......@@ -339,7 +341,7 @@ function googleanalytics_admin_settings_form($form_state) {
'#theme' => 'token_tree',
'#token_types' => array('node'),
'#dialog' => TRUE,
);
); */
// Advanced feature configurations.
......@@ -367,6 +369,7 @@ function googleanalytics_admin_settings_form($form_state) {
);
}
// @todo: Update urls once they are available.
$form['advanced']['codesnippet'] = array(
'#type' => 'fieldset',
'#title' => t('Custom JavaScript code'),
......@@ -379,14 +382,14 @@ function googleanalytics_admin_settings_form($form_state) {
'#title' => t('Code snippet (before)'),
'#default_value' => variable_get('googleanalytics_codesnippet_before', ''),
'#rows' => 5,
'#description' => t("Code in this textarea will be added <strong>before</strong> _gaq.push(['_trackPageview'])."),
'#description' => t("Code in this textarea will be added <strong>before</strong> <code>ga('send', 'pageview');</code>."),
);
$form['advanced']['codesnippet']['googleanalytics_codesnippet_after'] = array(
'#type' => 'textarea',
'#title' => t('Code snippet (after)'),
'#default_value' => variable_get('googleanalytics_codesnippet_after', ''),
'#rows' => 5,
'#description' => t("Code in this textarea will be added <strong>after</strong> _gaq.push(['_trackPageview']). This is useful if you'd like to track a site in two accounts."),
'#description' => t("Code in this textarea will be added <strong>after</strong> <code>ga('send', 'pageview');</code>. This is useful if you'd like to track a site in two accounts."),
);
$form['advanced']['googleanalytics_js_scope'] = array(
......@@ -446,10 +449,10 @@ function googleanalytics_admin_settings_form_validate($form, &$form_state) {
}
// This is for the Newbie's who cannot read a text area description.
if (stristr($form_state['values']['googleanalytics_codesnippet_before'], 'google-analytics.com/ga.js')) {
if (stristr($form_state['values']['googleanalytics_codesnippet_before'], 'google-analytics.com/analytics.js')) {
form_set_error('googleanalytics_codesnippet_before', t('Do not add the tracker code provided by Google into the javascript code snippets! This module already builds the tracker code based on your Google Analytics account number and settings.'));
}
if (stristr($form_state['values']['googleanalytics_codesnippet_after'], 'google-analytics.com/ga.js')) {
if (stristr($form_state['values']['googleanalytics_codesnippet_after'], 'google-analytics.com/analytics.js')) {
form_set_error('googleanalytics_codesnippet_after', t('Do not add the tracker code provided by Google into the javascript code snippets! This module already builds the tracker code based on your Google Analytics account number and settings.'));
}
if (preg_match('/(.*)<\/?script(.*)>(.*)/i', $form_state['values']['googleanalytics_codesnippet_before'])) {
......
......@@ -87,12 +87,13 @@ Drupal.behaviors.trackingSettingsSummary = {
if ($('input#edit-googleanalytics-site-search', context).is(':checked')) {
vals.push(Drupal.t('Site search'));
}
/* @todo: Features not yet supported by analytics.js
if ($('input#edit-googleanalytics-trackadsense', context).is(':checked')) {
vals.push(Drupal.t('AdSense ads'));
}
if ($('input#edit-googleanalytics-trackdoubleclick', context).is(':checked')) {
vals.push(Drupal.t('DoubleClick data'));
}
} */
if (!vals.length) {
return Drupal.t('Not tracked');
}
......
......@@ -36,8 +36,8 @@ function googleanalytics_uninstall() {
variable_del('googleanalytics_pages');
variable_del('googleanalytics_roles');
variable_del('googleanalytics_site_search');
variable_del('googleanalytics_trackadsense');
variable_del('googleanalytics_trackdoubleclick');
variable_del('googleanalytics_trackadsense'); // @todo
variable_del('googleanalytics_trackdoubleclick'); // @todo
variable_del('googleanalytics_tracker_anonymizeip');
variable_del('googleanalytics_trackfiles');
variable_del('googleanalytics_trackfiles_extensions');
......
......@@ -26,27 +26,29 @@ $(document).ready(function() {
else if (ga.trackDownload && isDownload.test(this.href)) {
// Download link clicked.
var extension = isDownload.exec(this.href);
_gaq.push(["_trackEvent", "Downloads", extension[1].toUpperCase(), this.href.replace(isInternal, '')]);
ga("send", "event", "Downloads", extension[1].toUpperCase(), this.href.replace(isInternal, ''));
}
else if (isInternalSpecial.test(this.href)) {
// Keep the internal URL for Google Analytics website overlay intact.
_gaq.push(["_trackPageview", this.href.replace(isInternal, '')]);
ga("send", "pageview", { page: this.href.replace(isInternal, '')});
}
}
else {
if (ga.trackMailto && $(this).is("a[href^='mailto:'],area[href^='mailto:']")) {
// Mailto link clicked.
_gaq.push(["_trackEvent", "Mails", "Click", this.href.substring(7)]);
ga("send", "event", "Mails", "Click", this.href.substring(7));
}
else if (ga.trackOutbound && this.href.match(/^\w+:\/\//i)) {
if (ga.trackDomainMode == 2 && isCrossDomain($(this).attr('hostname'), ga.trackCrossDomains)) {
// Top-level cross domain clicked. document.location is handled by _link internally.
event.preventDefault();
_gaq.push(["_link", this.href]);
// @todo: unknown upgrade path
//_gaq.push(["_link", this.href]);
//ga("link", this.href); ???
}
else {
// External link clicked.
_gaq.push(["_trackEvent", "Outbound links", "Click", this.href]);
ga("send", "event", "Outbound links", "Click", this.href);
}
}
}
......@@ -58,7 +60,7 @@ $(document).ready(function() {
$(document).bind("cbox_complete", function() {
var href = $.colorbox.element().attr("href");
if (href) {
_gaq.push(["_trackPageview", href.replace(isInternal, '')]);
ga("send", "pageview", { page: href.replace(isInternal, '') });
}
});
......
......@@ -95,12 +95,14 @@ function googleanalytics_page_alter(&$page) {
if (!empty($id) && (_googleanalytics_visibility_pages() || in_array($status, $trackable_status_codes)) && _googleanalytics_visibility_user($user)) {
// We allow different scopes. Default to 'header' but allow user to override if they really need to.
// @todo: footer may no longer suppored
$scope = variable_get('googleanalytics_js_scope', 'header');
/* @todo: not supported, https://support.google.com/analytics/bin/answer.py?hl=en&hlrm=de&answer=2795983
if (variable_get('googleanalytics_trackadsense', FALSE)) {
// Custom tracking. Prepend before all other JavaScript.
drupal_add_js('window.google_analytics_uacct = ' . drupal_json_encode($id) . ';', array('type' => 'inline', 'group' => JS_LIBRARY-1));
}
//drupal_add_js('window.google_analytics_uacct = ' . drupal_json_encode($id) . ';', array('type' => 'inline', 'group' => JS_LIBRARY-1));
} */
// Add link tracking.
$link_settings = array();
......@@ -140,7 +142,8 @@ function googleanalytics_page_alter(&$page) {
// Track only the selected message types.
if (in_array($type, $message_types)) {
foreach ($messages as $message) {
$message_events .= '_gaq.push(["_trackEvent", ' . drupal_json_encode(t('Messages')) . ', ' . drupal_json_encode($status_heading[$type]) . ', ' . drupal_json_encode(strip_tags($message)) . ']);';
// @todo: Track as exceptions?
$message_events .= 'ga("send", "event", ' . drupal_json_encode(t('Messages')) . ', ' . drupal_json_encode($status_heading[$type]) . ', ' . drupal_json_encode(strip_tags($message)) . ');';
}
}
}
......@@ -181,6 +184,7 @@ function googleanalytics_page_alter(&$page) {
$codesnippet_after = variable_get('googleanalytics_codesnippet_after', '');
// Add custom variables.
/* @todo: Needs upgrade to custom dimensions
$googleanalytics_custom_vars = variable_get('googleanalytics_custom_var', array());
$custom_var = '';
for ($i = 1; $i < 6; $i++) {
......@@ -215,17 +219,47 @@ function googleanalytics_page_alter(&$page) {
$custom_var_name = drupal_json_encode($custom_var_name);
$custom_var_value = drupal_json_encode($custom_var_value);
$custom_var .= "_gaq.push(['_setCustomVar', $i, $custom_var_name, $custom_var_value, $custom_var_scope]);";
}
$custom_var .= "ga('set', 'dimension" . $i . "', $custom_var_name);"; // Max 150 bytes
$custom_var .= "ga('set', 'metric" . $i . "', $custom_var_value);";
}
} */
// Build tracker code.
$script = 'var _gaq = _gaq || [];';
$script .= '_gaq.push(["_setAccount", ' . drupal_json_encode($id) . ']);';
$script = '(function(q,u,i,c,k){';
$script .= 'window["GoogleAnalyticsObject"]=q;';
$script .= 'window[q]=window[q]||function(){';
$script .= '(window[q].q=window[q].q||[]).push(arguments)},';
$script .= 'window[q].l=1*new Date();c=i.createElement(u),k=i.getElementsByTagName(u)[0];';
$script .= 'c.async=true;';
// Which version of the tracking library should be used?
$library_tracker_url = '//www.google-analytics.com/analytics.js';
$library_cache_url = 'http:' . $library_tracker_url;
// Should a local cached copy of analytics.js be used?
if (variable_get('googleanalytics_cache', 0) && $url = _googleanalytics_cache($library_cache_url)) {
// 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
// URL changed.
$query_string = '?' . variable_get('css_js_query_string', '0');
$script .= 'c.src="' . $url . $query_string . '";';
}
else {
$script .= 'c.src="' . $library_tracker_url . '";';
}
$script .= 'k.parentNode.insertBefore(c,k)})("ga","script",document);';
// Create a tracker.
$script .= 'ga("create", ' . drupal_json_encode($id) . ');';
if (variable_get('googleanalytics_tracker_anonymizeip', 0)) {
// @todo: unknown
// FIXME: The Google API is currently broken and "_gat._anonymizeIp" is only
// a workaround until "_anonymizeIp" has been implemented/fixed.
$script .= '_gaq.push(["_gat._anonymizeIp"]);';
$script .= 'ga("set", "anonymizeIp", true);';
}
// Domain tracking type.
......@@ -235,11 +269,12 @@ function googleanalytics_page_alter(&$page) {
// Per RFC 2109, cookie domains must contain at least one dot other than the
// first. For hosts such as 'localhost' or IP Addresses we don't set a cookie domain.
if ($domain_mode == 1 && count(explode('.', $cookie_domain)) > 2 && !is_numeric(str_replace('.', '', $cookie_domain))) {
$script .= '_gaq.push(["_setDomainName", ' . drupal_json_encode($cookie_domain) . ']);';
$script .= 'ga("set", "cookieDomain", ' . drupal_json_encode($cookie_domain) . ');';
}
elseif ($domain_mode == 2) {
$script .= '_gaq.push(["_setDomainName", "none"]);';
$script .= '_gaq.push(["_setAllowLinker", true]);';
// @todo: unclear, cookieDomain = "none" is not documented.
$script .= 'ga("set", "cookieDomain", "none");';
$script .= 'ga("set", "allowLinker", true);';
}
if (!empty($custom_var)) {
......@@ -248,12 +283,11 @@ function googleanalytics_page_alter(&$page) {
if (!empty($codesnippet_before)) {
$script .= $codesnippet_before;
}
if (empty($url_custom)) {
$script .= '_gaq.push(["_trackPageview"]);';
}
else {
$script .= '_gaq.push(["_trackPageview", ' . $url_custom . ']);';
if (!empty($url_custom)) {
$script .= 'ga("set", "page", ' . $url_custom . ');';
}
$script .= 'ga("send", "pageview");';
if (!empty($message_events)) {
$script .= $message_events;
}
......@@ -261,44 +295,6 @@ function googleanalytics_page_alter(&$page) {
$script .= $codesnippet_after;
}
$script .= '(function() {';
$script .= 'var ga = document.createElement("script");';
$script .= 'ga.type = "text/javascript";';
$script .= 'ga.async = true;';
// Which version of the tracking library should be used?
if ($trackdoubleclick = variable_get('googleanalytics_trackdoubleclick', FALSE)) {
$library_tracker_url = 'stats.g.doubleclick.net/dc.js';
$library_cache_url = 'http://' . $library_tracker_url;
}
else {
$library_tracker_url = '.google-analytics.com/ga.js';
$library_cache_url = 'http://www' . $library_tracker_url;
}
// Should a local cached copy of ga.js be used?
if (variable_get('googleanalytics_cache', 0) && $url = _googleanalytics_cache($library_cache_url)) {
// 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
// URL changed.
$query_string = '?' . variable_get('css_js_query_string', '0');
$script .= 'ga.src = "' . $url . $query_string . '";';
}
else {
// Library paths do not follow the same naming convention.
if ($trackdoubleclick) {
$script .= 'ga.src = ("https:" == document.location.protocol ? "https://" : "http://") + "' . $library_tracker_url . '";';
}
else {
$script .= 'ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + "' . $library_tracker_url . '";';
}
}
$script .= 'var s = document.getElementsByTagName("script")[0];';
$script .= 's.parentNode.insertBefore(ga, s);';
$script .= '})();';
drupal_add_js($script, array('scope' => $scope, 'type' => 'inline'));
}
}
......@@ -384,13 +380,7 @@ function googleanalytics_user_presave(&$edit, $account, $category) {
function googleanalytics_cron() {
// Regenerate the tracking code file every day.
if (REQUEST_TIME - variable_get('googleanalytics_last_cache', 0) >= 86400 && variable_get('googleanalytics_cache', 0)) {
// Which version of the tracking library should be used?
if (variable_get('googleanalytics_trackdoubleclick', FALSE)) {
_googleanalytics_cache('http://stats.g.doubleclick.net/dc.js', TRUE);
}
else {
_googleanalytics_cache('http://www.google-analytics.com/ga.js', TRUE);
}
_googleanalytics_cache('http://www.google-analytics.com/analytics.js', TRUE);
variable_set('googleanalytics_last_cache', REQUEST_TIME);
}
}
......
......@@ -58,7 +58,7 @@ class GoogleAnalyticsBasicTest extends DrupalWebTestCase {
$this->assertNoRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is not displayed on admin page.');
$this->drupalGet('admin/config/system/googleanalytics');
// Checking for tracking code URI here, as $ua_code is displayed in the form.
$this->assertNoRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsPageVisibility]: Tracking code is not displayed on admin subpage.');
$this->assertNoRaw('google-analytics.com/analytics.js', '[testGoogleAnalyticsPageVisibility]: Tracking code is not displayed on admin subpage.');
// Test whether tracking code display is properly flipped.
variable_set('googleanalytics_visibility_pages', 1);
......@@ -66,7 +66,7 @@ class GoogleAnalyticsBasicTest extends DrupalWebTestCase {
$this->assertRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is displayed on admin page.');
$this->drupalGet('admin/config/system/googleanalytics');
// Checking for tracking code URI here, as $ua_code is displayed in the form.
$this->assertRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsPageVisibility]: Tracking code is displayed on admin subpage.');
$this->assertRaw('google-analytics.com/analytics.js', '[testGoogleAnalyticsPageVisibility]: Tracking code is displayed on admin subpage.');
$this->drupalGet('');
$this->assertNoRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is NOT displayed on front page.');
......@@ -93,18 +93,18 @@ class GoogleAnalyticsBasicTest extends DrupalWebTestCase {
variable_set('cache', 1);
// Test whether DNT headers will fail to disable embedding of tracking code.
$this->drupalGet('', array(), array('DNT: 1'));
$this->assertRaw('_gaq.push(["_trackPageview"]);', '[testGoogleAnalyticsDNTVisibility]: DNT header send from client, but page caching is enabled and tracker cannot removed.');
$this->assertRaw('ga("send", "pageview");', '[testGoogleAnalyticsDNTVisibility]: DNT header send from client, but page caching is enabled and tracker cannot removed.');
// DNT works only with caching of pages for anonymous users disabled.
variable_set('cache', 0);
$this->drupalGet('');
$this->assertRaw('_gaq.push(["_trackPageview"]);', '[testGoogleAnalyticsDNTVisibility]: Tracking is enabled without DNT header.');
$this->assertRaw('ga("send", "pageview");', '[testGoogleAnalyticsDNTVisibility]: Tracking is enabled without DNT header.');
// Test whether DNT header is able to remove the tracking code.
$this->drupalGet('', array(), array('DNT: 1'));
$this->assertNoRaw('_gaq.push(["_trackPageview"]);', '[testGoogleAnalyticsDNTVisibility]: DNT header received from client. Tracking has been disabled by browser.');
$this->assertNoRaw('ga("send", "pageview");', '[testGoogleAnalyticsDNTVisibility]: DNT header received from client. Tracking has been disabled by browser.');
// Disable DNT feature and see if tracker is still embedded.
variable_set('googleanalytics_privacy_donottrack', 0);
$this->drupalGet('', array(), array('DNT: 1'));
$this->assertRaw('_gaq.push(["_trackPageview"]);', '[testGoogleAnalyticsDNTVisibility]: DNT feature is disabled, DNT header from browser has been ignored.');
$this->assertRaw('ga("send", "pageview");', '[testGoogleAnalyticsDNTVisibility]: DNT feature is disabled, DNT header from browser has been ignored.');
}
function testGoogleAnalyticsTrackingCode() {
......@@ -118,40 +118,34 @@ class GoogleAnalyticsBasicTest extends DrupalWebTestCase {
/* Sample JS code as added to page:
<script type="text/javascript" src="/sites/all/modules/google_analytics/googleanalytics.js?w"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-123456-7']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
<script>
(function(q,u,i,c,k){window['GoogleAnalyticsObject']=q;
window[q]=window[q]||function(){(window[q].q=window[q].q||[]).push(arguments)},
window[q].l=1*new Date();c=i.createElement(u),k=i.getElementsByTagName(u)[0];
c.async=true;c.src='//www.google-analytics.com/analytics.js';
k.parentNode.insertBefore(c,k)})('ga','script',document);
ga('create', 'UA-123456-7');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
*/
// Test whether tracking code uses latest JS.
variable_set('googleanalytics_cache', 0);
$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_trackdoubleclick', 1);
$this->drupalGet('');
$this->assertRaw('stats.g.doubleclick.net/dc.js', '[testGoogleAnalyticsTrackingCode]: Doubleclick tracking code used.');
$this->assertRaw('google-analytics.com/analytics.js', '[testGoogleAnalyticsTrackingCode]: Latest 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.');
$this->assertNoRaw('ga("set", "anonymizeIp", true);', '[testGoogleAnalyticsTrackingCode]: Anonymize visitors IP address not found on frontpage.');
// Enable anonymizing of IP addresses.
variable_set('googleanalytics_tracker_anonymizeip', 1);
$this->drupalGet('');
$this->assertRaw('_gaq.push(["_gat._anonymizeIp"]);', '[testGoogleAnalyticsTrackingCode]: Anonymize visitors IP address found on frontpage.');
$this->assertRaw('ga("set", "anonymizeIp", true);', '[testGoogleAnalyticsTrackingCode]: Anonymize visitors IP address found on frontpage.');
// Test whether single domain tracking is active.
$this->drupalGet('');
$this->assertNoRaw('_gaq.push(["_setDomainName"', '[testGoogleAnalyticsTrackingCode]: Single domain tracking is active.');
$this->assertNoRaw('ga("set", "cookieDomain",', '[testGoogleAnalyticsTrackingCode]: Single domain tracking is active.');
// Enable "One domain with multiple subdomains".
variable_set('googleanalytics_domain_mode', 1);
......@@ -161,30 +155,32 @@ class GoogleAnalyticsBasicTest extends DrupalWebTestCase {
// TODO: Workaround to run tests successfully. This feature cannot tested reliable.
global $cookie_domain;
if (count(explode('.', $cookie_domain)) > 2 && !is_numeric(str_replace('.', '', $cookie_domain))) {
$this->assertRaw('_gaq.push(["_setDomainName",', '[testGoogleAnalyticsTrackingCode]: One domain with multiple subdomains is active on real host.');
$this->assertRaw('ga("set", "cookieDomain",', '[testGoogleAnalyticsTrackingCode]: One domain with multiple subdomains is active on real host.');
}
else {
// Special cases, Localhost and IP addresses don't show '_setDomainName'.
$this->assertNoRaw('_gaq.push(["_setDomainName",', '[testGoogleAnalyticsTrackingCode]: One domain with multiple subdomains may be active on localhost (test result is not reliable).');
$this->assertNoRaw('ga("set", "cookieDomain",', '[testGoogleAnalyticsTrackingCode]: One domain with multiple subdomains may be active on localhost (test result is not reliable).');
}
// Enable "Multiple top-level domains" tracking.
variable_set('googleanalytics_domain_mode', 2);
variable_set('googleanalytics_cross_domains', "www.example.com\nwww.example.net");
$this->drupalGet('');
$this->assertRaw('_gaq.push(["_setDomainName", "none"]);', '[testGoogleAnalyticsTrackingCode]: _setDomainName: "none" found. Cross domain tracking is active.');
$this->assertRaw('_gaq.push(["_setAllowLinker", true]);', '[testGoogleAnalyticsTrackingCode]: _setAllowLinker: true found. Cross domain tracking is active.');
$this->assertRaw('ga("set", "cookieDomain", "none");', '[testGoogleAnalyticsTrackingCode]: _setDomainName: "none" found. Cross domain tracking is active.');
$this->assertRaw('ga("set", "allowLinker", true);', '[testGoogleAnalyticsTrackingCode]: _setAllowLinker: true found. Cross domain tracking is active.');
$this->assertRaw('"trackCrossDomains":["www.example.com","www.example.net"]', '[testGoogleAnalyticsTrackingCode]: Cross domain tracking with www.example.com and www.example.net is active.');
// Test whether the BEFORE and AFTER code is added to the tracker.
variable_set('googleanalytics_codesnippet_before', '_setDetectFlash(false);');
variable_set('googleanalytics_codesnippet_after', '_gaq.push(["t2._setAccount", "UA-123456-3"]);_gaq.push(["t2._trackPageview"]);');
// @todo: review detectFlash once API docs are available.
variable_set('googleanalytics_codesnippet_before', 'ga("set", "detectFlash", false);');
variable_set('googleanalytics_codesnippet_after', 'ga("create", "UA-123456-3", {name: "newTracker"});ga("newTracker.send", "pageview");');
$this->drupalGet('');
$this->assertRaw('_setDetectFlash(false);', '[testGoogleAnalyticsTrackingCode]: Before codesnippet has been found with "Flash" detection disabled.');
$this->assertRaw('t2._setAccount', '[testGoogleAnalyticsTrackingCode]: After codesnippet with "t2" tracker has been found.');
$this->assertRaw('ga("set", "detectFlash", false);', '[testGoogleAnalyticsTrackingCode]: Before codesnippet has been found with "Flash" detection disabled.');
$this->assertRaw('ga("create", "UA-123456-3", {name: "newTracker"});', '[testGoogleAnalyticsTrackingCode]: After codesnippet with "newTracker" tracker has been found.');
}
}
/* @todo: upgrade to custom dimensions
class GoogleAnalyticsCustomVariablesTest extends DrupalWebTestCase {
public static function getInfo() {
......@@ -251,7 +247,7 @@ class GoogleAnalyticsCustomVariablesTest extends DrupalWebTestCase {
$this->drupalGet('');
foreach ($custom_vars['slots'] as $slot) {
$this->assertRaw("_gaq.push(['_setCustomVar', " . $slot['slot'] . ", \"" . $slot['name'] . "\", \"" . $slot['value'] . "\", " . $slot['scope'] . "]);", '[testGoogleAnalyticsCustomVariables]: _setCustomVar ' . $slot['slot'] . ' is shown.');
$this->assertRaw("ga('set', 'customvar', " . $slot['slot'] . ", \"" . $slot['name'] . "\", \"" . $slot['value'] . "\", " . $slot['scope'] . ");", '[testGoogleAnalyticsCustomVariables]: _setCustomVar ' . $slot['slot'] . ' is shown.');
}
// Test whether tokens are replaced in custom variable names.
......@@ -296,13 +292,13 @@ class GoogleAnalyticsCustomVariablesTest extends DrupalWebTestCase {
$this->verbose('<pre>' . print_r($custom_vars, TRUE) . '</pre>');
$this->drupalGet('');
$this->assertRaw("_gaq.push(['_setCustomVar', 1, \"Name: $site_slogan\", \"Value: $site_slogan\", 3]", '[testGoogleAnalyticsCustomVariables]: Tokens have been replaced in custom variable.');
$this->assertNoRaw("_gaq.push(['_setCustomVar', 2,", '[testGoogleAnalyticsCustomVariables]: Value with empty name is not shown.');
$this->assertNoRaw("_gaq.push(['_setCustomVar', 3,", '[testGoogleAnalyticsCustomVariables]: Name with empty value is not shown.');
$this->assertNoRaw("_gaq.push(['_setCustomVar', 4,", '[testGoogleAnalyticsCustomVariables]: Empty name and value is not shown.');
$this->assertNoRaw("_gaq.push(['_setCustomVar', 5,", '[testGoogleAnalyticsCustomVariables]: Empty name and value is not shown.');
$this->assertRaw("ga('set', 'customvar', 1, \"Name: $site_slogan\", \"Value: $site_slogan\", 3);", '[testGoogleAnalyticsCustomVariables]: Tokens have been replaced in custom variable.');
$this->assertNoRaw("ga('set', 'customvar', 2,", '[testGoogleAnalyticsCustomVariables]: Value with empty name is not shown.');
$this->assertNoRaw("ga('set', 'customvar', 3,", '[testGoogleAnalyticsCustomVariables]: Name with empty value is not shown.');
$this->assertNoRaw("ga('set', 'customvar', 4,", '[testGoogleAnalyticsCustomVariables]: Empty name and value is not shown.');
$this->assertNoRaw("ga('set', 'customvar', 5,", '[testGoogleAnalyticsCustomVariables]: Empty name and value is not shown.');
}
}
} */
class GoogleAnalyticsStatusMessagesTest extends DrupalWebTestCase {
......@@ -334,8 +330,8 @@ class GoogleAnalyticsStatusMessagesTest extends DrupalWebTestCase {
variable_set('googleanalytics_trackmessages', array('error' => 'error'));
$this->drupalPost('user/login', array(), 'Log in');
$this->assertRaw('_gaq.push(["_trackEvent", "Messages", "Error message", "Username field is required."]);', '[testGoogleAnalyticsStatusMessages]: _trackEvent "Username field is required." is shown.');
$this->assertRaw('_gaq.push(["_trackEvent", "Messages", "Error message", "Password field is required."]);', '[testGoogleAnalyticsStatusMessages]: _trackEvent "Password field is required." is shown.');
$this->assertRaw('ga("send", "event", "Messages", "Error message", "Username field is required.");', '[testGoogleAnalyticsStatusMessages]: Event message "Username field is required." is shown.');
$this->assertRaw('ga("send", "event", "Messages", "Error message", "Password field is required.");', '[testGoogleAnalyticsStatusMessages]: Event message "Password field is required." is shown.');
// @todo: investigate why drupal_set_message() fails.
//drupal_set_message('Example status message.', 'status');
......@@ -343,10 +339,10 @@ class GoogleAnalyticsStatusMessagesTest extends DrupalWebTestCase {
//drupal_set_message('Example error message.', 'error');
//drupal_set_message('Example error <em>message</em> with html tags and <a href="http://example.com/">link</a>.', 'error');
//$this->drupalGet('');
//$this->assertNoRaw('_gaq.push(["_trackEvent", "Messages", "Status message", "Example status message."]);', '[testGoogleAnalyticsStatusMessages]: Example status message is not enabled for tracking.');
//$this->assertNoRaw('_gaq.push(["_trackEvent", "Messages", "Warning message", "Example warning message."]);', '[testGoogleAnalyticsStatusMessages]: Example warning message is not enabled for tracking.');
//$this->assertRaw('_gaq.push(["_trackEvent", "Messages", "Error message", "Example error message."]);', '[testGoogleAnalyticsStatusMessages]: Example error message is shown.');
//$this->assertRaw('_gaq.push(["_trackEvent", "Messages", "Error message", "Example error message with html tags and link."]);', '[testGoogleAnalyticsStatusMessages]: HTML has been stripped successful from Example error message with html tags and link.');
//$this->assertNoRaw('ga("send", "event", "Messages", "Status message", "Example status message.");', '[testGoogleAnalyticsStatusMessages]: Example status message is not enabled for tracking.');
//$this->assertNoRaw('ga("send", "event", "Messages", "Warning message", "Example warning message.");', '[testGoogleAnalyticsStatusMessages]: Example warning message is not enabled for tracking.');
//$this->assertRaw('ga("send", "event", "Messages", "Error message", "Example error message.");', '[testGoogleAnalyticsStatusMessages]: Example error message is shown.');
//$this->assertRaw('ga("send", "event", "Messages", "Error message", "Example error message with html tags and link.");', '[testGoogleAnalyticsStatusMessages]: HTML has been stripped successful from Example error message with html tags and link.');
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment