Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHANNELS-1156] remove email unsub link when unsubcribe tag not present #2198

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,89 @@ describe.each([
expect(sendGridRequest.isDone()).toEqual(true)
})

it('Do not add list-unsubscribe headers when the link is empty in the email profile', async () => {
const bodyHtml =
'<html><head></head><body><p>Hi First Name, welcome to Segment</p> <a href="http://unsubscribe_link">Unsubscribe</a></body></html>'
const replacedHtmlWithLink =
'<html><head></head><body><p>Hi First Name, welcome to Segment</p> <a href="http://unsubscribe_link">Unsubscribe</a></body></html>'
const expectedSendGridRequest = {
personalizations: [
{
to: [
{
email: userData.email
}
],
bcc: [
{
email: 'test@test.com'
}
],
custom_args: {
source_id: 'sourceId',
space_id: 'spaceId',
user_id: userData.userId,
__segment_internal_external_id_key__: 'email',
__segment_internal_external_id_value__: userData.email
}
}
],
from: {
email: 'from@example.com',
name: 'From Name'
},
reply_to: {
email: 'replyto@example.com',
name: 'Test user'
},
subject: `Hello ${userData.lastName} ${userData.firstName}.`,
content: [
{
type: 'text/html',
value: replacedHtmlWithLink
}
],
tracking_settings: {
subscription_tracking: {
enable: true,
substitution_tag: '[unsubscribe]'
}
}
}

const sendGridRequest = nock('https://api.sendgrid.com')
.post('/v3/mail/send', expectedSendGridRequest)
.reply(200, {})

const responses = await sendgrid.testAction('sendEmail', {
event: createMessagingTestEvent({
timestamp,
event: 'Audience Entered',
userId: userData.userId,
external_ids: [
{
collection: 'users',
encoding: 'none',
id: userData.email,
isSubscribed: true,
unsubscribeLink: 'http://unsubscribe_link',
preferencesLink: '',
type: 'email'
}
]
}),
settings,
mapping: getDefaultMapping({
body: undefined,
bodyHtml: bodyHtml,
bodyType: 'html'
})
})

expect(responses.length).toBeGreaterThan(0)
expect(sendGridRequest.isDone()).toEqual(true)
})

it('inserts unsubscribe link in all the places in the html body', async () => {
const bodyHtml =
'<p>Hi First Name, welcome to Segment. Here is an <a href="[upa_unsubscribe_link]">Unsubscribe</a> link.</p> <a href="[upa_unsubscribe_link]">Unsubscribe</a> | <a href="[upa_preferences_link]">Manage Preferences</a>'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,19 @@ export class SendEmailPerformer extends MessageSendPerformer<Settings, Payload>
return $.html()
}

// Remove unsubscribe link from email profile if no substution tag is found this will keep subscription tracking on
// so Sendgrid can keep track of unsubscriptions from the email header. This allows customers that use their own unsubscribe
// links to be able to set up a webhook to track unsubscribe events and sync it with their own subscription management system.
if ($(unsubscribeLinkRef).length === 0) {
_this.logger.info(`Unsubscribe tag is missing`)
emailProfile.unsubscribeLink = ''
}

if ($(preferencesLinkRef).length === 0) {
_this.logger.info(`Preferences tag missing`)
emailProfile.preferencesLink = ''
}

if (groupId) {
const group = emailProfile.groups?.find((grp) => grp.id === groupId)
const groupUnsubscribeLink = group?.groupUnsubscribeLink
Expand Down
Loading