Here's a patch which allows 3rd party modules to modify links in mimemail messages. This makes it possible add tracking code (for example, Google Analytics) to every link in an outgoing email, or any other future uses people might come up with to tweak the links in email (redirect to a different domain, language, etc. )
This patch does 2 things:
- Adds hook_mimemail_url_alter when parsing links in a message, so that other modules can edit these links
- Provides a mechanism to store and retrieve the mailkey, in case this is needed by functions which will later alter the mail (mailkey is passed through initially through 1 or 2 layers of functions, but not available to hooks or other functions which might come into play and which want to alter their behavior depending on the type of email).
And here's some sample code implementing this new hook. This function adds Google Analytics tracking code to all links in the email, using the mailkey as the campaign:
/**
* Implementation of hook_mimemail_url_alter
* @param string $url url to alter
* @return string altered url
*/
function MYMODULE_mimemail_url_alter($url) {
$campaign = mimemail_key();
if (!$campaign) $campaign = t("generic");
$url .= "?utm_source=users&utm_medium=email&utm_campaign=$campaign";
return $url;
}
Comment | File | Size | Author |
---|---|---|---|
#5 | mimemail-1308630-4.patch | 1.41 KB | jducro |
#3 | mimemail-1308630-3.patch | 1.37 KB | sgabe |
mimemail.inc_.tracker.patch | 1.61 KB | gmania | |
Comments
Comment #1
gmania CreditAttribution: gmania commentedAny chance of this getting committed? This seems like the cleanest way to attach tracking code to URLs in outgoing messages, and has been working great for us on a Production site for the past 3 months.
Alternatively, anyone have a workaround? There doesn't seem to be any way to alter links within Mimemail, which is why I proposed adding an alter hook to make this possible.
Thanks!
Comment #2
dineshcooper CreditAttribution: dineshcooper commentedLooking for this too
Comment #3
sgabe CreditAttribution: sgabe commentedI would rather see this at the end of _mimemail_url when the URL processing is done.
Since this is just a helper function and the include file will be loaded anyway, lets move this to mimemail.inc.
I am not sure about the idea of handling this by simply appending something to the end of the URL. I think it would be better to pass the URL's additional options to the alter function where you can modify the query element.
See the attached patch.
Comment #4
sgabe CreditAttribution: sgabe commentedChanging status.
Comment #5
jducro CreditAttribution: jducro commentedAs the $path variable is a string, adding the index '__drupal_alter_by_ref' causes all link first letter to be replaced by A (Array).
I suggest the following patch to deal with this specific problem.
Comment #6
TR CreditAttribution: TR commentedThis is a really old issue and the patch no longer works for any supported version of Mime Mail. New features should be added to the most recent version first then backported if necessary.