Here's the updated documentation to include the section about how to send emails with the necessary fields for parsing, as well as the Reply
schema for storing replies:
This documentation provides an overview of the new features and updates for the campaign reply tracking and analytics system. These include the integration with SendGrid's inbound parsing service, updates to the campaign and reply schemas, the introduction of an analytics schema for tracking email campaign performance, and real-time notifications for campaign owners.
campaignId
: A unique identifier (campaignId
) is added to the campaign schema to associate emails with specific campaigns.- Unique
reply-to
address: Each campaign now has a uniquereply-to
address that ensures replies are correctly routed to the respective campaign.
-
Reply Schema Enhancements:
For each email sent:- A unique
messageId
is generated and stored in the reply schema along with thecampaignId
andrecipientId
. - This
messageId
is appended to outgoing emails to allow for precise matching of incoming replies with their respective emails.
- A unique
-
Webhook Route Setup:
A new route (/api/analytics/replies
) is introduced to accept incoming webhook requests from SendGrid's inbound email parsing service. This service is responsible for processing the parsed email data.
- A route is implemented to parse incoming replies:
- It identifies the campaign associated with the incoming reply using the
messageId
andIn-Reply-To
headers. - The reply’s timestamp and other details are logged in the database for future analytics.
- It identifies the campaign associated with the incoming reply using the
- Campaign analytics have been updated to include new metrics such as:
- Total
replyCount
: The number of replies received for a campaign. responsePercentage
: The percentage of replies relative to the number of emails sent, giving insight into the engagement rate.
- Total
- Integrated Socket.IO to notify campaign owners in real-time when a reply is received.
- Notifications are sent to the campaign owner's dashboard or notification center as soon as a reply is logged in the system.
- New Field: The
campaignId
field is added to the campaign schema to track the source of each email. - Unique
reply-to
Address: Each campaign has a uniquereply-to
email address that is used for receiving replies, ensuring that all replies are routed to the correct campaign.
-
Schema: A new reply schema stores a unique
messageId
for each email, along with thecampaignId
andrecipientId
. This structure allows for matching incoming replies to their original emails. -
Database Structure:
const replySchema = new mongoose.Schema({ campaignId: { type: mongoose.Schema.Types.ObjectId, ref: 'Campaign' // Reference to the Campaign model }, messageId: { type: String, required: true, unique: true // Ensure the messageId is unique for each reply }, recipientId: { type: mongoose.Schema.Types.ObjectId, ref: 'Recipient', // Reference to the Recipient model required: true }, timestamp: { type: Date, default: Date.now }, replyContent: { type: String, required: true }, inReplyTo: { type: String, required: true } });
-
Setup:
- SendGrid’s Inbound Parsing service is configured to forward incoming emails to a specific webhook URL. The webhook parses the incoming email and provides a structured payload containing key details, such as the sender's email address, message body,
messageId
,In-Reply-To
header, and more. - The SendGrid webhook is set up to forward emails to a dedicated route (
/api/analytics/replies
).
- SendGrid’s Inbound Parsing service is configured to forward incoming emails to a specific webhook URL. The webhook parses the incoming email and provides a structured payload containing key details, such as the sender's email address, message body,
-
Webhook Route:
APOST
route is created to handle incoming parsed data from SendGrid.Route:
/api/analytics/replies
Description: This route accepts webhook requests from SendGrid, extracts the necessary data (e.g.,messageId
,In-Reply-To
, reply content), and processes the reply for storage in the database.Request Payload:
{ "to": ["[email protected]"], "from": "[email protected]", "text": "Reply content here", "html": "<p>Reply content here</p>", "message_id": "<[email protected]>", "headers": { "In-Reply-To": "<[email protected]>" } }
To ensure replies are matched to the correct campaign and email, the email should include the following fields:
- Unique
Message-ID
: A unique identifier for the email, typically generated by the email service provider. This field is essential for identifying and matching replies. In-Reply-To
Header: This header must contain theMessage-ID
of the original email, linking the reply to the correct email.- Unique
Reply-To
Address: Each campaign should have a uniquereply-to
address (e.g.,[email protected]
). This ensures that replies are directed to the correct campaign.
Example Email Headers:
From: "Campaign Team" <[email protected]>
To: [email protected]
Subject: "Special Offer Just for You!"
Message-ID: <[email protected]>
In-Reply-To: <[email protected]>
Reply-To: [email protected]
Date: Wed, 26 Dec 2024 12:00:00 -0500
When a reply is received via SendGrid's webhook, the messageId
and In-Reply-To
headers are used to identify the original email. The reply is then stored in the Reply
schema in the database.
-
Analytics Schema:
A new analytics schema is introduced to track the performance of each campaign. This schema stores key metrics such as:emailsSent
: Total number of emails sent in a campaign.opens
: Number of times the emails have been opened.openPercentage
: Percentage of emails opened out of the total emails sent.replyCount
: Total number of replies received.responsePercentage
: Percentage of replies received out of the total emails sent.
-
Schema Example:
const analyticsSchema = new mongoose.Schema({ campaignId: { type: mongoose.Schema.Types.ObjectId, ref: 'Campaign' }, emailsSent: { type: Number, default: 0 }, opens: { type: Number, default: 0 }, openPercentage: { type: Number, default: 0 }, replyCount: { type: Number, default: 0 }, responsePercentage: { type: Number, default: 0 } });
Here are the routes involved in the campaign reply tracking system. Replace with actual routes as necessary:
- Method:
POST
- Path:
/api/analytics/replies
- Description: Accepts webhook requests from SendGrid's inbound email parsing service to process incoming email replies and log them in the database.
- Request Body:
{ "to": ["[email protected]"], "from": "[email protected]", "text": "Reply content here", "html": "<p>Reply content here</p>", "message_id": "<[email protected]>", "headers": { "In-Reply-To": "<[email protected]>" } }
Analytics Route**
- Method:
GET
- Path:
/api/analytics/:campaignId
- Description: Fetches the analytics data for a specific campaign, including metrics like replies, opens, and response percentage.
This documentation will help guide your implementation of the email reply tracking and analytics system. You can edit the routes and other specifics as necessary based on your project's actual implementation.