Last active
August 29, 2015 14:13
-
-
Save samuelsolis/5cc733a98aa843d783c1 to your computer and use it in GitHub Desktop.
Add param to drupal mail links for tracking with Google Analytics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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