[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/google_analytics/ -> googleanalytics.install (source)

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Installation file for Google Analytics module.
   6   */
   7  
   8  function googleanalytics_uninstall() {
   9    variable_del('googleanalytics_account');
  10    variable_del('googleanalytics_custom_var');
  11    variable_del('googleanalytics_codesnippet_before');
  12    variable_del('googleanalytics_codesnippet_after');
  13    variable_del('googleanalytics_domain_mode');
  14    variable_del('googleanalytics_segmentation');
  15    variable_del('googleanalytics_trackoutgoing');
  16    variable_del('googleanalytics_trackoutboundaspageview');
  17    variable_del('googleanalytics_trackmailto');
  18    variable_del('googleanalytics_trackfiles');
  19    variable_del('googleanalytics_trackfiles_extensions');
  20    variable_del('googleanalytics_cache');
  21    variable_del('googleanalytics_last_cache');
  22    variable_del('googleanalytics_site_search');
  23    variable_del('googleanalytics_trackadsense');
  24    variable_del('googleanalytics_js_scope');
  25    variable_del('googleanalytics_custom');
  26    variable_del('googleanalytics_roles');
  27    variable_del('googleanalytics_visibility');
  28    variable_del('googleanalytics_visibility_roles');
  29    variable_del('googleanalytics_pages');
  30    variable_del('googleanalytics_tracker_anonymizeip');
  31    variable_del('googleanalytics_privacy_donottrack');
  32    variable_del('googleanalytics_translation_set');
  33  
  34    // Remove backup variables if exits. Remove this code in D8.
  35    variable_del('googleanalytics_codesnippet_before_backup_6300');
  36    variable_del('googleanalytics_codesnippet_after_backup_6300');
  37  }
  38  
  39  /**
  40   * Remove cache directory if module is disabled (or uninstalled).
  41   */
  42  function googleanalytics_disable() {
  43    googleanalytics_clear_js_cache();
  44  }
  45  
  46  /**
  47   * Implementation of hook_requirements().
  48   */
  49  function googleanalytics_requirements($phase) {
  50    $requirements = array();
  51  
  52    if ($phase == 'runtime') {
  53      // Raise warning if Google user account has not been set yet.
  54      if (!preg_match('/^UA-\d{4,}-\d+$/', variable_get('googleanalytics_account', 'UA-'))) {
  55        $requirements['googleanalytics'] = array(
  56          'title' => t('Google Analytics module'),
  57          'description' => t('Google Analytics module has not been configured yet. Please configure its settings from the <a href="@url">Google Analytics settings page</a>.', array('@url' => url('admin/settings/googleanalytics'))),
  58          'severity' => REQUIREMENT_ERROR,
  59          'value' => t('Not configured'),
  60        );
  61      }
  62    }
  63  
  64    return $requirements;
  65  }
  66  
  67  
  68  function googleanalytics_update_1() {
  69    $ret = array();
  70  
  71    $result = db_query("SELECT * FROM {role}");
  72    while ($role = db_fetch_object($result)) {
  73      // can't use empty spaces in varname
  74      $role_varname = str_replace(' ', '_', $role->name);
  75      variable_set('googleanalytics_track_'. $role->rid, !variable_get("googleanalytics_track_{$role_varname}", FALSE));
  76      variable_del("googleanalytics_track_{$role_varname}");
  77    }
  78    variable_set('googleanalytics_track__user1', FALSE);
  79  
  80    return $ret;
  81  }
  82  
  83  /**
  84   * Upgrade old extension variable to new and use old name as enabled/disabled flag.
  85   */
  86  function googleanalytics_update_6000() {
  87    $ret = array();
  88  
  89    variable_set('googleanalytics_trackfiles_extensions', variable_get('googleanalytics_trackfiles', '7z|aac|avi|csv|doc|exe|flv|gif|gz|jpe?g|js|mp(3|4|e?g)|mov|pdf|phps|png|ppt|rar|sit|tar|torrent|txt|wma|wmv|xls|xml|zip'));
  90    $trackfiles = variable_get('googleanalytics_trackfiles', '7z|aac|avi|csv|doc|exe|flv|gif|gz|jpe?g|js|mp(3|4|e?g)|mov|pdf|phps|png|ppt|rar|sit|tar|torrent|txt|wma|wmv|xls|xml|zip') ? TRUE : FALSE;
  91    variable_set('googleanalytics_trackfiles', $trackfiles);
  92    $ret[] = array('success' => TRUE, 'query' => 'Updated download tracking settings.');
  93  
  94    return $ret;
  95  }
  96  
  97  function googleanalytics_update_6001() {
  98    $ret = array();
  99  
 100    variable_set('googleanalytics_visibility', 0);
 101  
 102    // Remove tracking from all administrative pages, see http://drupal.org/node/34970.
 103    $pages = array(
 104      'admin*',
 105      'user*',
 106      'node/add*',
 107      'node/*/*',
 108    );
 109    variable_set('googleanalytics_pages', implode("\n", $pages));
 110    $ret[] = array('success' => TRUE, 'query' => 'Added page tracking to every page except the listed pages: '. implode(', ', $pages));
 111  
 112    return $ret;
 113  }
 114  
 115  /**
 116   * Upgrade role settings and per user tracking settings
 117   * of "User 1" and remove outdated tracking variables.
 118   */
 119  function googleanalytics_update_6002() {
 120    $ret = array();
 121  
 122    // Upgrade enabled/disabled roles to new logic (correct for upgrades from 5.x-1.4 and 6.x-1.0).
 123    $roles = array();
 124    foreach (user_roles() as $rid => $name) {
 125      if (variable_get('googleanalytics_track_'. $rid, FALSE)) {
 126        // Role ID is activated for user tracking.
 127        $roles[$rid] = $rid;
 128        $ret[] = array('success' => TRUE, 'query' => 'Enabled page tracking for role: '. $name);
 129      }
 130      else {
 131        $ret[] = array('success' => TRUE, 'query' => 'Disabled page tracking for role: '. $name);
 132      }
 133    }
 134    variable_set('googleanalytics_roles', $roles);
 135  
 136    // Upgrade disabled tracking of "user 1" to new logic.
 137    if (!$track_user1 = variable_get('googleanalytics_track__user1', 1)) {
 138      variable_set('googleanalytics_custom', 1);
 139  
 140      // Load user 1 object, set appropiate value and save new user settings back.
 141      $account = user_load(array('uid' => 1));
 142      $account = user_save($account, array('googleanalytics' => array('custom' => 0)), 'account');
 143      $ret[] = array('success' => TRUE, 'query' => 'Disabled user specific page tracking for website administrator.');
 144    }
 145  
 146    // Delete outdated tracking settings.
 147    $ret[] = update_sql("DELETE FROM {variable} WHERE name LIKE 'googleanalytics_track_%'");
 148  
 149    return $ret;
 150  }
 151  
 152  /**
 153   * #262468: Clear menu cache to solve stale menu data in 5.x-1.5 and 6.x-1.1
 154   */
 155  function googleanalytics_update_6003() {
 156    $ret = array();
 157    menu_rebuild();
 158    return $ret;
 159  }
 160  
 161  /**
 162   * Change visibility setting for path "user/*".
 163   */
 164  function googleanalytics_update_6004() {
 165    $ret = array();
 166  
 167    // Orginal pages setting.
 168    $pages = array(
 169      'admin*',
 170      'user*',
 171      'node/add*',
 172      'node/*/*',
 173    );
 174  
 175    $diff = array_diff($pages, preg_split('/(\r\n?|\n)/', variable_get('googleanalytics_pages', implode("\n", $pages))));
 176    if (empty($diff)) {
 177      // No diff to original settings found. Update with new settings.
 178      $pages = array(
 179        'admin*',
 180        'user/*/*',
 181        'node/add*',
 182        'node/*/*',
 183      );
 184      variable_set('googleanalytics_pages', implode("\n", $pages));
 185      $ret[] = array('success' => TRUE, 'query' => 'Path visibility filter setting changed from "user*" to "user/*/*".');
 186    }
 187    else {
 188      $ret[] = array('success' => TRUE, 'query' => 'Custom path visibility filter setting found. Update skipped!');
 189    }
 190  
 191    return $ret;
 192  }
 193  
 194  /**
 195   * Change visibility setting for path "admin*".
 196   */
 197  function googleanalytics_update_6005() {
 198    $ret = array();
 199  
 200    // Orginal pages setting.
 201    $pages = array(
 202      'admin*',
 203      'user/*/*',
 204      'node/add*',
 205      'node/*/*',
 206    );
 207  
 208    $diff = array_diff($pages, preg_split('/(\r\n?|\n)/', variable_get('googleanalytics_pages', implode("\n", $pages))));
 209    if (empty($diff)) {
 210      // No diff to original settings found. Update with new settings.
 211      $pages = array(
 212        'admin',
 213        'admin/*',
 214        'user/*/*',
 215        'node/add*',
 216        'node/*/*',
 217      );
 218      variable_set('googleanalytics_pages', implode("\n", $pages));
 219      $ret[] = array('success' => TRUE, 'query' => 'Path visibility filter setting changed from "admin*" to "admin" and "admin/*".');
 220    }
 221    else {
 222      $ret[] = array('success' => TRUE, 'query' => 'Custom path visibility filter setting found. Update skipped!');
 223    }
 224  
 225    return $ret;
 226  }
 227  
 228  /**
 229   * Upgrade custom javascript settings.
 230   */
 231  function googleanalytics_update_6006() {
 232    $ret = array();
 233  
 234    variable_set('googleanalytics_codesnippet_before', variable_get('googleanalytics_codesnippet', ''));
 235    variable_del('googleanalytics_codesnippet');
 236    $ret[] = array('success' => TRUE, 'query' => 'Upgraded custom javascript codesnippet setting.');
 237  
 238    return $ret;
 239  }
 240  
 241  /**
 242   * Remove "User identifier" and "User name" from segmentation fields.
 243   *
 244   * This is a data protection and privacy law change. For more information see Google Analytics
 245   * terms of use section 8.1 (http://www.google.com/analytics/en-GB/tos.html).
 246   */
 247  function googleanalytics_update_6007() {
 248    $ret = array();
 249  
 250    $profile_fields = variable_get('googleanalytics_segmentation', array());
 251    unset($profile_fields['uid']);
 252    unset($profile_fields['name']);
 253    variable_set('googleanalytics_segmentation', $profile_fields);
 254    $ret[] = array('success' => TRUE, 'query' => 'Removed "User identifier" and "User name" from segmentation fields.');
 255  
 256    return $ret;
 257  }
 258  
 259  /**
 260   * Remove outdated legacy support variables and files.
 261   */
 262  function googleanalytics_update_6200() {
 263    $ret = array();
 264  
 265    $path = file_directory_path() .'/googleanalytics';
 266    if (file_exists($path)) {
 267      file_delete($path .'/urchin.js');
 268    }
 269    variable_del('googleanalytics_legacy_version');
 270  
 271    $ret[] = array('success' => TRUE, 'query' => 'Removed legacy support.');
 272  
 273    return $ret;
 274  }
 275  
 276  /**
 277   * Update list of default file extensions.
 278   */
 279  function googleanalytics_update_6201() {
 280    $ret = array();
 281  
 282    if (variable_get('googleanalytics_trackfiles_extensions', '') == '7z|aac|avi|csv|doc|exe|flv|gif|gz|jpe?g|js|mp(3|4|e?g)|mov|pdf|phps|png|ppt|rar|sit|tar|torrent|txt|wma|wmv|xls|xml|zip') {
 283      variable_set('googleanalytics_trackfiles_extensions', '7z|aac|arc|arj|asf|asx|avi|bin|csv|doc|exe|flv|gif|gz|gzip|hqx|jar|jpe?g|js|mp(2|3|4|e?g)|mov(ie)?|msi|msp|pdf|phps|png|ppt|qtm?|ra(m|r)?|sea|sit|tar|tgz|torrent|txt|wav|wma|wmv|wpd|xls|xml|z|zip');
 284    }
 285    $ret[] = array('success' => TRUE, 'query' => 'The default extensions for download tracking have been updated.');
 286  
 287    return $ret;
 288  }
 289  
 290  /**
 291   * Try to update Google Analytics custom code snippet to async version.
 292   */
 293  function googleanalytics_update_6300() {
 294    $ret = array();
 295  
 296    // TODO: Backup synchronous code snippets. Remove variables in D8.
 297    variable_set('googleanalytics_codesnippet_before_backup_6300', variable_get('googleanalytics_codesnippet_before', ''));
 298    variable_set('googleanalytics_codesnippet_after_backup_6300', variable_get('googleanalytics_codesnippet_after', ''));
 299  
 300    // Upgrade of BEFORE code snippet.
 301    $code_before = variable_get('googleanalytics_codesnippet_before', '');
 302    if (!empty($code_before)) {
 303      // No value, e.g. _setLocalRemoteServerMode()
 304      $code_before = preg_replace('/(.*)pageTracker\.(\w+)\(\);(.*)/i', '$1_gaq.push(["$2"]);$3', $code_before);
 305      // One value, e.g. _setCookiePath()
 306      $code_before = preg_replace('/(.*)pageTracker\.(\w+)\(("|\'?)(\w+)("|\'?)\);(.*)/i', '$1_gaq.push(["$2", $3$4$5]);$6', $code_before);
 307      // Multiple values e.g. _trackEvent()
 308      $code_before = preg_replace('/(.*)pageTracker\.(\w+)\((.*)\);(.*)/i', '$1_gaq.push(["$2", $3]);$4', $code_before);
 309  
 310      variable_set('googleanalytics_codesnippet_before', $code_before);
 311  
 312      $ret[] = array('success' => TRUE, 'query' => 'Upgraded custom "before" code snippet.');
 313      drupal_set_message(db_prefix_tables("<strong>Attempted</strong> to upgrade Google Analytics custom 'before' code snippet. Backup of previous code snippet has been saved in database table '{variable}' as 'googleanalytics_codesnippet_before_backup_6300'. Please consult Google's <a href='http://code.google.com/intl/de-DE/apis/analytics/docs/tracking/asyncUsageGuide.html'>Asynchronous Tracking Usage Guide</a> if the upgrade was successfully."), 'warning');
 314    }
 315  
 316    // Upgrade of AFTER code snippet.
 317    // We cannot update this code snippet automatically. Show message that the upgrade has been skipped.
 318    $code_after = variable_get('googleanalytics_codesnippet_after', '');
 319    if (!empty($code_after)) {
 320      $ret[] = array('success' => TRUE, 'query' => 'Skipped custom "after" code snippet.');
 321      drupal_set_message(db_prefix_tables("Automatic upgrade of Google Analytics custom 'after' code snippet has been skipped. Backup of previous code snippet has been saved in database table '{variable}' as 'googleanalytics_codesnippet_after_backup_6300'. You need to manually upgrade the custom 'after' code snippet."), 'error');
 322    }
 323  
 324    return $ret;
 325  }
 326  
 327  /**
 328   * Upgrade "User roles" tracking to custom variables.
 329   */
 330  function googleanalytics_update_6301() {
 331    $ret = array();
 332  
 333    // Read previous segmentation settings.
 334    $segmentation = variable_get('googleanalytics_segmentation', array());
 335  
 336    if (module_exists('token') && in_array('roles', $segmentation)) {
 337      // Upgrade previous segmentation settings to new custom variables settings.
 338      $googleanalytics_custom_vars = variable_get('googleanalytics_custom_var', array());
 339  
 340      $googleanalytics_custom_vars['slots'][1]['slot'] = 1;
 341      $googleanalytics_custom_vars['slots'][1]['name'] = 'User roles';
 342      $googleanalytics_custom_vars['slots'][1]['value'] = '[user-role-names]';
 343      $googleanalytics_custom_vars['slots'][1]['scope'] = 1; // Sets the scope to visitor-level.
 344  
 345      variable_set('googleanalytics_custom_var', $googleanalytics_custom_vars);
 346      $ret[] = array('success' => TRUE, 'query' => t('The deprecated profile segmentation setting for "User roles" has been added to custom variables. You need to deselect all selected profile fields in <a href="@admin">Google Analytics settings</a> and upgrade other profile fields manually or you may loose tracking data in future! See Google Analytics <a href="@customvar">Custom Variables</a> for more information.', array('@customvar' => 'http://code.google.com/intl/en/apis/analytics/docs/tracking/gaTrackingCustomVariables.html', '@admin' => url('admin/settings/googleanalytics'))));
 347    }
 348    else {
 349      $ret[] = array('success' => TRUE, 'query' => t('You need to deselect all selected profile fields in <a href="@admin">Google Analytics settings</a> and upgrade other profile fields manually or you may loose tracking data in future! See Google Analytics <a href="@customvar">Custom Variables</a> for more information.', array('@customvar' => 'http://code.google.com/intl/en/apis/analytics/docs/tracking/gaTrackingCustomVariables.html', '@admin' => url('admin/settings/googleanalytics'))));
 350      $ret[] = array('success' => TRUE, 'query' => t('<a href="@token">Token</a> module is recommended for custom variables.', array('@token' => 'http://drupal.org/project/token')));
 351    }
 352  
 353    return $ret;
 354  }
 355  
 356  /**
 357   * Path visibility filter setting should hide "batch" path.
 358   */
 359  function googleanalytics_update_6302() {
 360    $ret = array();
 361  
 362    // Current pages setting.
 363    $pages = array(
 364      'admin',
 365      'admin/*',
 366      'user/*/*',
 367      'node/add*',
 368      'node/*/*',
 369    );
 370  
 371    $diff = array_diff($pages, preg_split('/(\r\n?|\n)/', variable_get('googleanalytics_pages', implode("\n", $pages))));
 372    if (empty($diff)) {
 373      // No diff to previous settings found. Update with new settings.
 374      $pages = array(
 375        'admin',
 376        'admin/*',
 377        'batch',
 378        'node/add*',
 379        'node/*/*',
 380        'user/*/*',
 381      );
 382      variable_set('googleanalytics_pages', implode("\n", $pages));
 383      $ret[] = array('success' => TRUE, 'query' => 'Added "batch" to path visibility filter setting.');
 384    }
 385    else {
 386      $ret[] = array('success' => TRUE, 'query' => 'Custom path visibility filter setting found. Update skipped!');
 387    }
 388  
 389    return $ret;
 390  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7