You are here

function googleanalytics_update_6300 in Google Analytics 7

Same name and namespace in other branches
  1. 6.4 googleanalytics.install \googleanalytics_update_6300()
  2. 6.3 googleanalytics.install \googleanalytics_update_6300()
  3. 7.2 googleanalytics.install \googleanalytics_update_6300()

Try to update Google Analytics custom code snippet to async version.

File

./googleanalytics.install, line 273
Installation file for Google Analytics module.

Code

function googleanalytics_update_6300() {
  $messages = array();

  // TODO: Backup synchronous code snippets. Remove variables in D8.
  variable_set('googleanalytics_codesnippet_before_backup_6300', variable_get('googleanalytics_codesnippet_before', ''));
  variable_set('googleanalytics_codesnippet_after_backup_6300', variable_get('googleanalytics_codesnippet_after', ''));

  // Upgrade of BEFORE code snippet.
  $code_before = variable_get('googleanalytics_codesnippet_before', '');
  if (!empty($code_before)) {

    // No value, e.g. _setLocalRemoteServerMode()
    $code_before = preg_replace('/(.*)pageTracker\\.(\\w+)\\(\\);(.*)/i', '$1_gaq.push(["$2"]);$3', $code_before);

    // One value, e.g. _setCookiePath()
    $code_before = preg_replace('/(.*)pageTracker\\.(\\w+)\\(("|\'?)(\\w+)("|\'?)\\);(.*)/i', '$1_gaq.push(["$2", $3$4$5]);$6', $code_before);

    // Multiple values e.g. _trackEvent()
    $code_before = preg_replace('/(.*)pageTracker\\.(\\w+)\\((.*)\\);(.*)/i', '$1_gaq.push(["$2", $3]);$4', $code_before);
    variable_set('googleanalytics_codesnippet_before', $code_before);
    drupal_set_message(Database::getConnection()
      ->prefixTables("<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='https://developers.google.com/analytics/devguides/collection/gajs/'>Asynchronous Tracking Usage Guide</a> if the upgrade was successfully."), 'warning');
    $messages[] = t('Upgraded custom "before" code snippet.');
  }

  // Upgrade of AFTER code snippet.
  // We cannot update this code snippet automatically. Show message that the upgrade has been skipped.
  $code_after = variable_get('googleanalytics_codesnippet_after', '');
  if (!empty($code_after)) {
    drupal_set_message(Database::getConnection()
      ->prefixTables("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');
    $messages[] = t('Skipped custom "after" code snippet.');
  }
  return empty($messages) ? t('No custom code snipped found. Nothing to do.') : implode(' ', $messages);
}