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;
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

gmania’s picture

Any 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!

dineshcooper’s picture

Looking for this too

sgabe’s picture

Status: Needs review » Needs work
FileSize
1.37 KB
+++ mimemail.inc Locally Modified (Based On LOCAL)
@@ -297,6 +297,7 @@
+  drupal_alter('mimemail_url', $matches[2]);

I would rather see this at the end of _mimemail_url when the URL processing is done.

+++ mimemail.module Locally Modified (Based On LOCAL)
@@ -449,3 +450,17 @@
+function mimemail_key($key=null) {

Since this is just a helper function and the include file will be loaded anyway, lets move this to mimemail.inc.

$url .= "?utm_source=users&utm_medium=email&utm_campaign=$campaign";

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.

sgabe’s picture

Status: Needs work » Needs review

Changing status.

jducro’s picture

As 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.

TR’s picture

Version: 6.x-1.x-dev » 8.x-1.x-dev
Issue summary: View changes
Status: Needs review » Needs work

This 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.