SparkPost – Control engagement tracking with ActionMailer

In a recent post, I’ve shown how to configure action mailer to send emails through SparkPost. Today, I have faced a situation where it was needed to enable/disable tracking on a specific email. It’s to be noted that, you can globally enable or disable engagement tracking in SMTP. However, if you want to enable tracking (when globally disabled) or disabled tracking (when globally enabled) for a specific email, keep reading.

 

I assume, you’ve created separate mailer for each type of emails (or for at least the mails you want different tracking behavior for). Follow official doc if you’re not sure what that means or how to do it. Now, let’s configure a mailer to enable engagement tracking. For that I’ve created a HelloMailer.

 

In the above snippet, we’ve configured welcome email of HelloMailer, hello_mailer.rb, to enable engagement tracking. So links in your template will be automatically converted to trackable links.

Notes:

  • If email is HTML (text/html), only links will be converted to a tracking URL. It’ll not convert any URLs (because you can use it for, like, anchor text and it won’t make sense to convert)
  • If email is text (text/plain), all URLs in the email will be converted to a tracking URL.
    (See the template, hello.html.erb, to see which URLs will be converted)

If you want to disable engagement tracking in an email, just set the values to false.

headers 'X-MSYS-API': { "options": { "open_tracking": false, "click_tracking": false } }.to_json

It’s that simple :).