Search code examples
mean-stacksendgridsendgrid-api-v3

How to Send Abandon Cart Email with Sendgrid / MEAN Stack


I've implemented a MEAN stack application and added Sendgrid. I am looking to send emails/abandon cart emails based on the state of the user.

Example:

If a user is going through a flow and abandons on page 2, send them a reminder email to complete the application;

I've also looked into sending an event with the Events Webhook and also creating a Unique Argument, but I am not sure if either of those options is correct.


Solution

  • I was able to achieve this by using Sengrid Contacts API

    Prerequisites:

    1. Create a custom field in Sendgrid Sengrid Custom Fields for Email Marketing

    2. My application is using this Sendgrid Client npm package, Sengrid V3 API, and /v3/contactdb/recipients endpoint

    My custom field tracks which step the user is in during the application progress, so my custom field name is "progress_application_step"

    If the user is on step 2 of the application, update their contact info with the current step

    const client = require('@sendgrid/client');
    client.setApiKey(SENDGRID_API_KEY);
    const request = {
      method: 'PATCH',
      url: '/v3/contactdb/recipients'
    };
    
    request.data = 
    [
     {
       "email": "emailforuser@mail.xxx",
       "progress_application_step": "2"
     }
    ];
    
    client.request(request)
    .then(([response, body]) => {
      console.log(response.statusCode);
      console.log(body);
    })
    

    This is still a work in progress, but I was able to achieve what I was looking to do with sending behavior emails. You can read more about Contacts API Recipients here. If anyone has found a better solution for sending user-behavior emails, please let me know!