Search code examples
.net-corec#-4.0sendgridsendgrid-api-v3sendgrid-templates

How to get event of sent email & open, click counts in sendgrid using c#?


I used the sendgrid's filters to filter messages and get count of open & clicks. But is there any other way to get that counts & events of sent email?

var queryParams ="(last_event_time BETWEEN TIMESTAMP " + "'" + lastEmailSent + "'" + " AND TIMESTAMP " + "'" + date2 + "'" + ") AND from_email='" + From_Email + "' AND subject='" + myMessage.Subject + "'";

var client1 = new RestClient("https://api.sendgrid.com/v3/messages?query="+ queryParams +"&limit=1");  
                var request = new RestRequest(Method.GET);  
                client1.Timeout = -1;

                // var request1 = new RestRequestExecute();
                request.AddHeader("X-Query-Id", "{{X-Query-Id}}");
                request.AddHeader("X-Cursor", "{{X-Cursor}}");
                request.AddHeader("Authorization", "Bearer API_KEY");
                
                IRestResponse response = client1.Execute(request);
                Console.WriteLine(response.Content);

Does anyone know how this could be done. Thanks.


Solution

  • Twilio SendGrid developer evangelist here.

    You can receive events like opens and clicks live using the Event Webhook. There are 2 types of events - delivery and engagement events. Delivery events indicate the status of email delivery to the recipient. Engagement events indicate how the recipient is interacting with the email, such as opening or clicking on the email. Events are batched and then sent as an HTTP request to your designated endpoint and you can parse out the events that are important to you.