Track email openings in Salesforce

Federico Kleinman
4 min readMay 8, 2021

--

Couple of days ago someone asked me to spike on the functionality about tracking email openings in Salesforce… This might be really useful in some cases.

Maybe you are a Sales rep and you want to know every time that the user opens the email so that you can see if the email was important for him or not. Maybe doing some A/B Testing and getting to pick the one that got the grater number of openings. And of course there are million cases in which knowing if the email recipient opened the email or not (Can you think of another use case?)

So, first thing i did, as usual was to Google it…

I found out that Salesforce already has a built in functionality. I thought great! I wont have to do any custom development for this to work, i was so happy…

But nothing is as good as it seems.

I started testing the functionality and discovered that it is not mend to work with Email-to-Case functionality (the exact one that the customer is using haha).

So i had to do a bigger study of the topic and as i couldn't find this kind of solution in Google, i decided to write it down here in case someone needs it.

Whats Email-to-Case?

I will try to explain this with my own words and you can refer to Salesforce documentation to know more. Email-to-Case is a functionality in which Salesforce allows users to automatically create or link emails to a Case.

Lets say you are working on a Case and need to send an Email. If you have Email-to-Case properly configured in your org, then Salesforce will automatically add an unique id to the subject or the body (or both) of the email.

This way, it can then track emails and associate them all to the same case with out your interaction.

But remember, when sending emails related to a Case, you wont be able to track the opens, at least not that i am aware of. Documentation says:

Email tracking in both Lightning Experience and Salesforce Classic doesn’t apply to emails sent from Cases or emails that use Email-to-Case.

How does the email tracking work?

It works with a “pixel”.

Its easier than it sounds… When you activate the Email Tracking in Salesforce, actually what happens is that Salesforce adds an image to every email created within the Org.

An htmlBody Email from Salesforce looks like this:
<!DOCTYPE html><html><style>p{margin-top:0px; margin-bottom:0px;}</style><body bgcolor=”#FFFFFF” marginwidth=”2" marginheight=”2" style=”font-family:Arial;font-size:10pt;color:000000"><html><body>This is a BODY</body></html><br><br><img src=”https://YOUR-ORG-INSTANCE.salesforce.com/servlet/servlet.ImageServer?oid=ORGID&esid=EMAILSTATUSID&from=int"></body></html>

When a recipient opens the email on his inbox, the browser try to load all the data, so it performs a GET Request to the src of the image (to retrieve it and show it), but the thing is that that url is not an image, it is a service from Salesforce that refers to EmailStatus Object (related to the EmailMessage) and every time the service receives a request, it adds 1 to the opened times and also modifies the LastOpenedDate from the EmailMessage.

You cant query an EmailStatus, this also means that you want be able to create or use triggers on that Object.

With this said and the limitations that Salesforce has, it is imposible to track openings on this type of Emails.

Solution 1

Instead of relating the Emails to the Case, if you relate it to a Contact, Lead or Task, tracking will be done.

Personally I relate the emails to Opportunity, just to test it and it got the first opening but then never updating again. Dont know with the other objects because because of my business reality it was not possible.

So, this solution did not work for me.

Solution 2

I thought about doing and endpoint, exposing it to be public and programming the entire functionality myself.

EmailMessage Object
I wanted to track the amount of times that the email was opened, so I added a new custom field to EmailMessage object called OpenedTimes__c, of course this field should be a number.

The endpoint
It is just a class with the appropriate annotations to handle get request from anywhere. The idea of the endpoint is:

  • Add 1 to the OpenedTimes__c field that i created on the EmailMessage object.
  • Update LastOpenedEmail field on the EmailMessage object.

Let me know if you need the code, I can share it with you.

Exposing the endpoint
For this purpose i used Sites. Go to the Setup and search for “Site”.
The idea of this is to open a door on your org to everyone, it does not require any login.
Be careful with this, you dont want to give more access than the needed one to everyone.

Adding the pixel on every new email
For this you will need to add a bit of code on the EmailMessage trigger. Before insert, you should append the htmlbody field with the url where you exposed the endpoint.

Should look something like this:
email.htmlbody = email.htmlbody + ‘<img src=’https://ORG_INSTANCE/services/apexrest/NAME_OF_YOUR_ENDPOINT_CLASS?caseId=500540000089yFIAAX’>’

Now you are all set, when a customer receives a new email and opens it, the browser will make a GET Request to get your “image” that will actually be the endpoint.

Once the endpoint is reached, it will run the code that searches for the Email related with the Case that come as a parameter and the magic is done.

Let me know if you have any questions! Glad to help.

--

--

Federico Kleinman
Federico Kleinman

Written by Federico Kleinman

Hey there! Salesforce Developer and soccer lover.

Responses (1)