Skip to content

Commit

Permalink
[CHANNELS-1156] remove email unsub link when unsubcribe tag not prese…
Browse files Browse the repository at this point in the history
…nt (#2198)

* fix: remove email unsub link when unsubcribe tag not present

* chore: add unit test and comment
  • Loading branch information
CesarAyuso authored and marinhero committed Aug 2, 2024
1 parent 3e34134 commit e80c367
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
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

0 comments on commit e80c367

Please sign in to comment.