Finding out whether or not the gift card email you sent has been read by your recipients is a great way to gather insight about your email.
It can also indicate (although see important caveats below) whether or not your emails are reaching users’ inboxes or are getting caught in a spam filter, as very few people open emails in their spam folder.
At Giftbit, email deliverability of gifts is core to our business, and open tracking is one of many statistics we collect and analyze to improve our product. It is also a metric we make available to our customers as part of their gift card orders.
Email open tracking is a simple concept and implementation:
All code samples are written in Groovy/Grails, but can easily be applied to any web framework / language.
Your controller has two jobs:
Accept tracking information and do something with it. This example shows an image tracking controller that accepts a URL that looks like an image (http://yoursite.com/opens/somestat/TrackingInformation123.gif), rather than a standard dynamic page (http://yoursite.com/opens/somestat?track=TrackingInformation123). The reason for doing it this way is to avoid it being identified as a web bug and stripped out by email clients and email scanning programs.
Return the actual binary data of the image and set the appropriate content type of the response.
In this example, we are tracking opens of gift emails. To track additional emails, you could try adding additional methods (which in grails are exposed as URL paths). This avoids a long single controller method with conditionals for different tracking types.
class OpenController {
def openTrackingService
def giftinstance() {
String giftcode = params.id?.replaceAll('.gif', '')
if (giftcode) {
openTrackingService.trackGiftInstanceEmailOpen(giftcode)
}
sendTrackingImageToResponse()
}
//add additional tracking as needed.
//def someOtherEmail() {}
public void sendTrackingImageToResponse() {
response.contentType = 'image/gif'
response.outputStream << openTrackingService.get1pixelTrackingImageAsBytes()
response.outputStream.flush()
}
Here is the source for the service. While very simple, resist the urge to put your database interaction into your controller. Good encapsulation is fundamental to code health and maintainability.
Of note is the definition of the 1×1 pixel image data. Given that it is such a tiny amount of data, simply store the byte array from a 1×1 pixel transparent gif for performance and simplicity, rather than go and actually load the image data from a file/URL/Classpath every time it is needed.
class OpenTrackingService {
private final byte[] singlePixelTransparentShimBytes = [71, 73, 70, 56, 57, 97, 1, 0, 1, 0, -128, 0, 0, -1, -1, -1, 0, 0, 0, 33, -7, 4, 1, 0, 0, 0, 0, 44, 0, 0, 0, 0, 1, 0, 1, 0, 0, 2, 2, 68, 1, 0, 59]
public byte[] get1pixelTrackingImageAsBytes() {
return singlePixelTransparentShimBytes
}
public void trackGiftInstanceEmailOpen(String giftCode) {
//update your tracking database/datastore accordingly.
GiftInstance giftInstance = giftCode ? GiftInstance.findByGiftcode(giftCode) : null;
if (giftInstance) {
giftInstance.lastOpened = new Date()
giftInstance.save()
}
}
//public void trackSomeOtheStuff(def data) {}
}
For any email you want to track, simply add an tag to call your controller as part of the email source. Usually this is done as part of the footer.
<img src="http://yoursite.com/open/giftinstance/GIFTCODE123.gif" alt="">
This would result in the giftcode ‘GIFTCODE123′ being tracked every time the email is opened.
If your email is actionable, click throughs (the act of a user following a link from the email) are a great statistic to measure rather than, or in addition to, opens.
They work with plaintext emails and are easy to measure by adding a parameter to your destination in the links in the email (http://yoursite.com/destination?fromemail=emailidentifier) or handling all your email links through a central tracking controller that does statistics recording and then sends the user to the real destination (http://yoursite.com/fromemail?emailid=something&destination=).
The advantage of the latter approach is that you don’t need your tracking business logic scattered throughout your application if there are many destinations.
Click throughs are often considered a ‘better’ metric because they show actual engagement and aren’t skewed by potential technical or user behaviour limitations that prevent images in emails from being rendered.