Mailgun is the most popular developer friendly Transactional Email service provider. It can track every event of the emails and can send the data to you through its Events API. Because of its developer-friendly nature, you can easily access the data from its various API.
Today, we are going to understand the usage of Events in the process to track the emails sent by you. Email Tracking becomes so easy because of the Mailgun Events API, you can track open, click, delivered, Spam, Failures, etc by this.
Mailgun provides three ways to interact with Its Data
Let's start with tracking the Emails Events
1. Tracking Open/View
To Enable the Open tracking functionality to add the "o:tracking-opens" or "o:tracking" parameters while sending an email from your code. You can also do it by enabling the tracking setting from your Mailgun account. You will need to add CNAME record to your DNS as Specified in the DNS and Domain Verification settings. Below is the sample response of the Open Event
{ "event": "opened", "id": "-laxIqj9QWubsjY_3pTq_g", "timestamp": 1377047343.042277, "log-level": "info", "recipient": "[email protected]", "geolocation": { "country": "US", "region": "Texas", "city": "Austin" }, "tags": [], "campaigns": [], "user-variables": {}, "ip": "111.111.111.111", "client-info": { "client-type": "mobile browser", "client-os": "iOS", "device-type": "mobile", "client-name": "Mobile Safari", "user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 6_1 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Mobile/10B143" }, "message": { "headers": { "message-id": "[email protected]" } }, }
You can also specify your webhook URL so that, when a user open an email, your webhook will be called and you will get the response instantly.
2. Tracking Clicks
Similar to the open tracking you can also track the click on anything in the mail. You can track the click on the link, image or any element of the message body. You would need to add the "o:tracking-clicks" or "o:tracking" parameters while sending an email from your code. In this case, the links will be overwritten and pointed to Mailgun servers so that they can track click events. You can also mention that you only want links rewritten in the HTML part of a message with the parameter o:tracking-clicks and passing htmlonly. Below is the sample response of the Click Event
{ "event": "clicked", "id": "G5zMz2ysS6OxZ2C8xb2Tqg", "timestamp": 1377075564.094891, "log-level": "info", "recipient": "[email protected]", "geolocation": { "country": "US", "region": "TX", "city": "Austin" }, "tags": [], "url": "http://example.com/signup", "ip": "123.123.123.321", "campaigns": [], "user-variables": {}, "client-info": { "client-type": "browser", "client-os": "Linux", "device-type": "desktop", "client-name": "Chromium", "user-agent": "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/28.0.1500.71 Chrome/28.0.1500.71 Safari/537.36" }, "message": { "headers": { "message-id": "[email protected]" } }, }