'Google Analytics tags', 'description' => 'Add google Analytics tags to newsletter links.', 'type' => MENU_NORMAL_ITEM, 'page callback' => 'drupal_get_form', 'page arguments' => array('simplenews_analytics_admin'), 'access arguments' => array('administer simplenews settings'), 'weight' => -7.9, ); return $items; } /** * Implemetation of hook_mail_alter(). * * Add Google Analysics codes to email links. * * @param $message * Mail message array. */ function simplenews_analytics_mail_alter(&$message) { if (strpos($message['id'], 'simplenews') !== FALSE) { // Parse message body array. if (isset($message['body'])) { _simplenews_analytics_get_context($message['params']['context']); _simplenews_analytics_parse_body($message['body']); } } } /** * 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 !newsletter_name 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) { switch ($path) { case 'admin/help#simplenews_analytics': $help = '

'. t('Simplenews Google Analytics adds tags to links in in the email. This helps you to track clicks in your email newsletters with Google Analytics.') ."
\n"; $help .= t("Permission 'administer simplenews settings' is used. No additional permissions are required.") ."

\n"; return $help; case 'admin/settings/simplenews/google_analytics': $help = '

'. t('See Google Analytics URL builder for tag information and examples.', array('!ga_url' => 'http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer=55578')) ."

\n"; return $help; } } /** * 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')); $context = _simplenews_analytics_get_context(); $tokens = simplenews_mail_tokens($context['account'], $context, $language); $campain = strtr(variable_get('simplenews_analytics_utm_campaign', '!newsletter_name'), $tokens); $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; }