All Products
Search
Document Center

Direct Mail:Track Email Open and Click Operation Based on Tags

Last Updated:Mar 13, 2024

For more information, please refer to SMTP - Java

Premise

To enable the email tracking feature, you must meet the following prerequisite:

1. The domain name needs to complete "CNAME verification". For more information about CNAME verification, please check Setup Domain Names.

Add dependencies

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
     <version>2.8.6</version>
</dependency>   
<dependency>        
    <groupId>commons-codec</groupId>        
    <artifactId>commons-codec</artifactId>        
    <version>1.2</version>   
</dependency>

Sample code

For more information, please refer to How to Enable the Data Tracking Feature

// To enable the email tracking service, use the following code to configure the tracking link. For information about the related prerequisites and limits, see "How can I enable the data tracking feature?"
String tagName = "Test";
HashMap<String, String> trace = new HashMap<>();
// Here is the string "1".
trace.put("OpenTrace", "1"); // Enable email tracking.
trace.put("LinkTrace", "1"); // Click the URL trace in the email.
trace.put("TagName", tagName); // The tagname that is created in the console.
String jsonTrace = new GsonBuilder().setPrettyPrinting().create().toJson(trace);
//System.out.println(jsonTrace);
String base64Trace = new String(Base64.getEncoder().encode(jsonTrace.getBytes()));
//Configure the SMTP header for tracking.
message.addHeader("X-AliDM-Trace", base64Trace);
// Example value in the original eml: X-AliDM-Trace: eyJUYWdOYW1 lIjoiVGVzdCIsIk 9 wZW5UcmFjZSI6IjEiLCJMaW5rVHJhY2UiOiIxIn0=

The statistics feature will batch the Open and Click behavior data during the hour and output it to the Console. The open and click data of emails sent in the current hour can be queried in the Console or API in the next hour. 

For more information about the API operation, please refer to Delivery Overview.