Skip to content

Instantly share code, notes, and snippets.

@samuelsolis
Last active August 29, 2015 14:13
Show Gist options
  • Save samuelsolis/5cc733a98aa843d783c1 to your computer and use it in GitHub Desktop.
Save samuelsolis/5cc733a98aa843d783c1 to your computer and use it in GitHub Desktop.
Add param to drupal mail links for tracking with Google Analytics
<?php
function mail_customization_mail_alter(&$message){
$preg = '/((https?:\/\/)([\da-z\.-]+)\.?([a-z\.]{2,6})?([\/\w \.-]*)*\/?(\?([A-Za-z0-9=&])*)?)/';
foreach($message['body'] as $key=>$body){
preg_match($preg , $body, $matches);
if (!empty($matches[0])){
$url = $matches[0];
//If we havent any parameter in the URL
if (empty($matches[5])){
$url .= '?';
}else{
$url .= '&';
}
$url .= 'source=mail';
$body = preg_replace($preg, $url, $body);
$message['body'][$key] = $body;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment