10.0: Track email opens with mail templates, draft tokens, and webhook automations #29
jstanden
started this conversation in
Guides and Tutorials
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Introduction
A common metric in email marketing campaigns is tracking which messages actually get opened by the recipient rather than deleted. This data can be used during A/B testing to develop subject lines that consistently perform well.
While Cerb doesn't provide email tracking as a pre-built feature (nor do we endorse the practice), it's simple to implement this functionality yourself using Cerb's tools.
When implementing tracking functionality, it's very important to consider the privacy implications. Email open tracking works by sending a request to a remote server when a message is viewed. This is usually accomplished by using an image (e.g. logo, transparent pixel) and only works with "HTML" messages (e.g. images, colors, fonts, layouts) rather than plain text.
Every time a reader opens an email message that is logged. You will know how often, when, and approximately where (if not obfuscated by a VPN) your messages are read.
In general, this kind of information is already recorded by most web servers: timestamp, URL, IP, and OS/browser versions. But it's not difficult to imagine a case where a stalker could abuse even imprecise location details, or information that a target read their last message multiple times, including in the middle of the night.
There are many ways email applications prevent tracking:
We highly recommend that you respect these preferences when a recipient blocks your tracking.
This guide won't attempt to circumvent such measures, and our provided example is obvious about what it's doing with
/open/
in the tracking URL (a pattern that is frequently blocked).If email tracking is still an important requirement for your use case, this guide demonstrates how you would accomplish it.
NOTE: This guide requires Cerb 10.0.1. Make sure you are fully updated before continuing.
Import the package
Log into Cerb as an administrator.
Navigate to Setup -> Packages -> Import.
Paste the following package:
Click the Import button.
How it works
The provided package creates a new mail HTML template named Email Tracking. You'll find it in Search -> Email Templates. You should customize this for your needs (layout/style).
This email template can be selected manually for outgoing messages, or it can be the default for an entire group and/or bucket. You would configure the latter from the HTML template: setting in Search -> Groups or Search -> Buckets.
The email template includes a 1x1 pixel transparent image like:
The
{{message_token}}
placeholder is unique and randomized for every draft. When a draft is successfully sent and delivered, the same token is set on the eventual message record.You can use the token to refer to a message before it has been created. You can also safely disclose a token to customers in public URLs. They don't betray private information in the same way sequential numeric IDs might; and they can't reasonably be guessed to target a specific message.
Email templates are useful for this workflow because they are only applied to the outgoing message. The results of a mail template aren't saved on the copy of the message within Cerb, so workers viewing the history later won't trigger open tracking.
When the recipient reads the message -- and external images are enabled and not blocked -- their computer requests the image from Cerb.
The target in Cerb is a webhook portal named Email Tracking that you'll find in Search -> Portals.
If you're familiar with portals, you may recall that you can access them by name from
/portal/
URLs without being logged in. In real-world use, however, you should put a web proxy in front of a portal so you can use your own domain name and SSL certificate, without disclosing the location of your Cerb instance.Webhook portals receive an arbitrary web request and use an automation to respond based on its properties: client IP, host, path, method, query arguments, payload, etc.
The webhook portal is configured to simply run an automation by name for every request:
The wgm.example.webhook.email_tracking automation has this logic:
If it's a
GET
request for the/open/<token>
path, the automation uses the record.search: command to locate the message bytoken:
filter in therecord_query:
option. It also usesrecord_query_params:
to sanitize untrusted user input so it can't modify the query.The
record_query:
also specifies thelimit:1
filter, to return a single message rather than a list. With this, themessage_record
output is a dictionary.When a matching message record is found, the record.update: command sets two custom fields on that message:
trackingOpenedAt
(the current date/time) andtrackingOpenedIp
(the IP of the reader). This could also look up the location by IP, decode theUser-Agent:
HTTP header for OS/browser details, etc.Afterwards, a 1x1 pixel transparent image is returned with the
Content-Type: image/png
and the image bytes encoded in Base-64 as thebody@base64:
option. You could replace this image with your logo instead and use it at the top of the mail template.For any other request, the automation returns a
404
(Not found) error.After that has taken place, you can filter messages by when and/or by whom they were opened.
In Search -> Messages you can use a query like the following to find messages that were opened in the past day:
This field can also be used for reporting.
Testing email tracking
You can test email tracking by navigating to any ticket profile from Search -> Tickets. Ideally this would be a test ticket.
Click the Reply button.
Set your personal email address to the only
To:
orCc:
recipient.Make sure Formatting is enabled.
Select the Email Tracking option in HTML Template. This doesn't require a group or bucket to be configured with the template as the default.
Click the Send message button to deliver the message.
Check your personal email. If external images aren't disabled or blocked, it will ping your Cerb instance and record the fact the message was read.
Reload the ticket profile.
When viewing the message in the timeline, you'll see the tracking custom fields:
Conclusion
The first thing you should do before deploying this workflow in the real-world is to set up a site for the webhook portal on your own hostname using SSL. Then edit the mail template and replace the
<img>
URL with your new hostname.You can add additional custom fields for tracking more metrics, or add more custom fieldsets for tracking different campaigns.
Beta Was this translation helpful? Give feedback.
All reactions