Index: simplenews_analytics.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/simplenews_analytics/simplenews_analytics.module,v
retrieving revision 1.2.2.2
diff -u -p -r1.2.2.2 simplenews_analytics.module
--- simplenews_analytics.module	6 Dec 2010 08:57:03 -0000	1.2.2.2
+++ simplenews_analytics.module	6 Dec 2010 09:08:25 -0000
@@ -17,6 +17,7 @@ function simplenews_analytics_menu() {
     'page callback' => 'drupal_get_form',
     'page arguments' => array('simplenews_analytics_admin'),
     'access arguments' => array('administer simplenews settings'),
+    'file' => 'simplenews_analytics.inc',
     'weight' => -7.9,
   );
 
@@ -32,6 +33,8 @@ function simplenews_analytics_menu() {
  *  Mail message array.
  */
 function simplenews_analytics_mail_alter(&$message) {
+  module_load_include('inc', 'simplenews_analytics', 'simplenews_analytics');
+
   if (strpos($message['id'], 'simplenews') !== FALSE) {
     // Parse message body array.
     if (isset($message['body'])) {
@@ -42,45 +45,6 @@ function simplenews_analytics_mail_alter
 }
 
 /**
- * Simplenews Google Analytics settings
- */
-function simplenews_analytics_admin(&$form_state) {
-  $form['simplenews_analytics_utm_source'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Campaign source'),
-    '#required' => TRUE,
-    '#size' => 60,
-    '#maxlength' => 128,
-    '#default_value' => variable_get('simplenews_analytics_utm_source', 'newsletter'),
-  );
-  $form['simplenews_analytics_utm_medium'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Campaign medium'),
-    '#required' => TRUE,
-    '#size' => 60,
-    '#maxlength' => 128,
-    '#default_value' => variable_get('simplenews_analytics_utm_medium', 'email'),
-  );
-  $form['simplenews_analytics_utm_campaign'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Campaign name'),
-    '#required' => TRUE,
-    '#size' => 60,
-    '#maxlength' => 128,
-    '#default_value' => variable_get('simplenews_analytics_utm_campaign', '!newsletter_name'),
-    '#description' => t('Newsletter name, product, promo code, or slogan. Use <em>!newsletter_name</em> to use the name of the newsletter series.'),
-  );
-  $form['simplenews_analytics_current_domain'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Limit to current domain'),
-    '#default_value' => variable_get('simplenews_analytics_current_domain', TRUE),
-    '#description' => t('When checked, the Google Analytics tags will only be added to links to the current domain (%domain).', array('%domain' => _simplenews_analytics_current_domain())),
-  );
-  
-  return system_settings_form($form);
-}
-
-/**
  * Implementation of hook_help().
  */
 function simplenews_analytics_help($path, $arg) {
@@ -95,93 +59,3 @@ function simplenews_analytics_help($path
   }
 }
 
-/**
- * Recursively parse email message body array.
- *
- * @param $text
- *  Mail message body text (array or string).
- */
-function _simplenews_analytics_parse_body(&$text) {
-  if (is_array($text)) {
-    foreach ($text as $key => $element) {
-      _simplenews_analytics_parse_body($text[$key]);
-    }
-  }
-  else {
-    if (!empty($text)) {
-      $text = preg_replace_callback("`(https?://[a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+*~#&?=/;-])`i", '_simplenews_analytics_parse_full_url', $text);
-    }
-  }
-}
-
-/**
- * preg callback: Add Google Analytics query string to url.
- *
- * @param $match
- *  Preg match array.
- *  
- * @return parsed url
- */
-function _simplenews_analytics_parse_full_url($match) {
-  // Don't add query string if link is outside of current domain.
-  if (variable_get('simplenews_analytics_current_domain', TRUE) && strpos($match[1], _simplenews_analytics_current_domain()) === FALSE) {
-    return $match[1];
-  }
-  
-  // Use ? or & prefix depending on existing query string.
-  if ($query = _simplenews_analytics_build_query()) {
-    $query = strpos($match[1],'?') ? '&'. $query : '?'. $query;
-  }
-
-  // Add query string to url.
-  return $match[1] . $query;
-}
-
-/**
- * Build query from Google Analytics settings
- *
- * @return query string containing Google Analytics query.
- */
-function _simplenews_analytics_build_query() {
-  global $language;
-  $query[] = 'utm_source='.  drupal_urlencode(variable_get('simplenews_analytics_utm_source', 'newsletter'));
-  $query[] = 'utm_medium='.  drupal_urlencode(variable_get('simplenews_analytics_utm_medium', 'email'));
-
-  // Build campain data with token replacements.
-  $context = _simplenews_analytics_get_context();
-  if (function_exists('simplenews_mail_tokens')){
-    // For simplenews 6.x-1.x, without token support.
-    $tokens = simplenews_mail_tokens($context['account'], $context, $language);
-    $campain = strtr(variable_get('simplenews_analytics_utm_campaign', '!newsletter_name'), $tokens);
-  }
-  else {
-    // For simplenews 6.x-2.x, because this version is using tokens.
-    $campain = token_replace(variable_get('simplenews_analytics_utm_campaign', '[simplenews-newsletters-name]'), 'simplenews', $context);
-  }  
-  $query[] = 'utm_campaign='.  drupal_urlencode($campain);
-
-  return implode('&', $query);
-}
-
-/**
- * Return the base URL without the protocol.
- */
-function _simplenews_analytics_current_domain() {
-  global $base_url;
-  return preg_replace('@https?://(.*)@i', '\1', $base_url);
-}
-
-/**
- * Store and reproduce context variables.
- *
- * @param $context
- *  Array of data to store. If NULL
- */
-function _simplenews_analytics_get_context($context = NULL) {
-  static $data = array();
-  
-  if (isset($context)) {
-    $data = array_merge($data, $context);
-  }
-  return $data;
-}
\ No newline at end of file
