Search code examples
javahtmlhttpspring-mvcoutlook

JAVA email tracking pixel keep track of reading time


I am working on email tracking pixel project, and I want to make sure that users spend at least 30 seconds reading an email before considering it "read"

I am using Java Springboot for backend server and HTML to create email templates

in my template I put this pixel for tracking:

<img id="tr_pixel" src="https://localhost:8080/api/v1/images/1"/>

once the image is loaded it will request my server:

@GetMapping("/images/{emailID}")
public ResponseEntity<byte[]> getImagePixel (@Pathvariable String emailID) {
    try{
      // wait 30seconds before saving the event
      Thread.sleep(30000);
      // If the connection with the client is lost, throw an exception and ignore the next line
      //save tracking only if user spend > 30s 
      service.saveTracking(emailID);

      return ok().contentType(IMAGE_PNG).body(pixel);

    } catch (ConnectionLostException){
       // catch connection lost error if the client close the email before 30s or if did not receive the response
    }
    
}

is there any way to check if the connection with the client is lost, or if the client receives the http response?


Solution

  • Perhaps a 302 redirect will help you. It will be enough for you to define not one, but two entry points.

    1. The first point @GetMapping("/wait30second/{emailID}"), having received the request, sleeps for 30 seconds and sends a 302 redirect to the second point ...your-site.../images/{emailID}, for example like this: https://www.baeldung.com/spring-redirect-and-forward
    2. The second point @GetMapping("/images/{emailID}") just fixes repeated access to a pixel in service.saveTracking(emailID) without a pause of 30 seconds. 302 redirect is performed by the client. Therefore, if there is no repeated appeal, it means that the letter was not read.

    However, keep in mind that with the massive use of such monitoring, there is a high risk of your emails being flagged as spam.