Sitecore Custom EXM Unsubscribe Page

If you ever got a chance to implement the email campaign functionality using the default email template in EXM, then you may have observed that each email send to the users contains two links that are Unsubscribe and Unsubscribe from all from future emails campaigns.

Requirement: When the user clicks on the Unsubscribe link in the email, instead of redirecting it to Unsubscribe.aspx page which is the default, the user should land on the custom page developed into the Sitecore. On the custom page, the user can choose the options from which list the user wants to unsubscribe.

Solution: When the user clicks on the unsubscribe link, Sitecore default redirects to the RedirectUrlPage.aspx with decrypted query string parameter named ec_eq. The ec_eq query string parameter is the encrypted query string, and it is handled by the RedirectUrlPage.aspx process which handles the decryption of those parameters and returns Contact ID, Message-Id and others. On the custom page, we will fetch the other details from xDB using Contact Id.

I have created one page named "Unsubscribe" into the Sitecore using Controller Rendering to achieve the same. You need to write one patch file to update two sections in "Sitecore.EmailExperience.ContentDelivery.config".

  • Creates the URL to redirect the request
  • Registers custom page events

You can find the above config file at this location: {Sitecore Root Instance}\App_Config\Sitecore\EmailExperience. I have written the patch file as mentioned below:

<processor type="Sitecore.EmailCampaign.Cd.Pipelines.RedirectUrl.SetRedirectToUrl, Sitecore.EmailCampaign.Cd" resolve="true" patch:instead="*[@resolve='true' and @type='Sitecore.EmailCampaign.Cd.Pipelines.RedirectUrl.SetRedirectToUrl, Sitecore.EmailCampaign.Cd']">

              <internalCarryoverFields hint="list:AddInternalCarryoverField">

                <carryoverField type="Sitecore.EmailCampaign.Cd.Pipelines.RedirectUrl.CarryoverField, Sitecore.EmailCampaign.Cd">

                  <param desc="fieldKey" ref="settings/setting[@name='QueryStringKey.MessageId']/@value" />

                  <param desc="urlPattern">/news/unsubscribe|SubscriptionPreferences.ashx|.*ConfirmSubscription.aspx|.*Unsubscribe.aspx|UnsubscribeFromAll.aspx.*|.*sc_pd_view=1.*</param>

                </carryoverField>

                <carryoverField type="Sitecore.EmailCampaign.Cd.Pipelines.RedirectUrl.CarryoverField, Sitecore.EmailCampaign.Cd">

                  <param desc="fieldKey" ref="settings/setting[@name='QueryStringKey.AnalyticsContactId']/@value" />

                  <param desc="urlPattern">/news/unsubscribe|SubscriptionPreferences.ashx|.*ConfirmSubscription.aspx|.*Unsubscribe.aspx|UnsubscribeFromAll.aspx.*|.*sc_pd_view=1.*</param>

                </carryoverField>

                <carryoverField type="Sitecore.EmailCampaign.Cd.Pipelines.RedirectUrl.CarryoverField, Sitecore.EmailCampaign.Cd">

                  <param desc="fieldKey" ref="settings/setting[@name='QueryStringKey.ContactIdentifierSource']/@value" />

                  <param desc="urlPattern">/news/unsubscribe|SubscriptionPreferences.ashx|.*ConfirmSubscription.aspx|.*Unsubscribe.aspx|UnsubscribeFromAll.aspx.*|.*sc_pd_view=1.*</param>

                </carryoverField>

                <carryoverField type="Sitecore.EmailCampaign.Cd.Pipelines.RedirectUrl.CarryoverField, Sitecore.EmailCampaign.Cd">

                  <param desc="fieldKey" ref="settings/setting[@name='QueryStringKey.ContactIdentifierIdentifier']/@value" />

                  <param desc="urlPattern">/news/unsubscribe|SubscriptionPreferences.ashx|.*ConfirmSubscription.aspx|.*Unsubscribe.aspx|UnsubscribeFromAll.aspx.*|.*sc_pd_view=1.*</param>

                </carryoverField>

                <carryoverField type="Sitecore.EmailCampaign.Cd.Pipelines.RedirectUrl.CarryoverField, Sitecore.EmailCampaign.Cd">

                  <param desc="fieldKey" ref="settings/setting[@name='QueryStringKey.TargetLanguage']/@value" />

                  <param desc="urlPattern">/news/unsubscribe|SubscriptionPreferences.ashx|.*ConfirmSubscription.aspx|.*Unsubscribe.aspx|UnsubscribeFromAll.aspx.*|.*sc_pd_view=1.*</param>

                </carryoverField>

                <carryoverField type="Sitecore.EmailCampaign.Cd.Pipelines.RedirectUrl.CarryoverField, Sitecore.EmailCampaign.Cd">

                  <param desc="fieldKey" ref="settings/setting[@name='QueryStringKey.TestValueIndex']/@value" />

                  <param desc="urlPattern">/news/unsubscribe|SubscriptionPreferences.ashx|.*ConfirmSubscription.aspx|.*Unsubscribe.aspx|UnsubscribeFromAll.aspx.*|.*sc_pd_view=1.*</param>

                </carryoverField>

                <carryoverField type="Sitecore.EmailCampaign.Cd.Pipelines.RedirectUrl.CarryoverField, Sitecore.EmailCampaign.Cd">

                  <param desc="fieldKey" ref="settings/setting[@name='QueryStringKey.EmailHistoryEntryId']/@value" />

                  <param desc="urlPattern">/news/unsubscribe|SubscriptionPreferences.ashx|.*ConfirmSubscription.aspx|.*Unsubscribe.aspx|UnsubscribeFromAll.aspx.*|.*sc_pd_view=1.*</param>

                </carryoverField>

              </internalCarryoverFields>

            </processor>

            <processor type="Sitecore.EmailCampaign.Cd.Pipelines.RedirectUrl.RegisterPageEvents, Sitecore.EmailCampaign.Cd" resolve="true">

              <IgnoredUrlPattern>/news/unsubscribe|SubscriptionPreferences.ashx|.*ConfirmSubscription.aspx|.*Unsubscribe.aspx|UnsubscribeFromAll.aspx.*|.*sc_pd_view=1.*</IgnoredUrlPattern>

            </processor>

            <processor type="Sitecore.EmailCampaign.Cd.Pipelines.RedirectUrl.TriggerCampaign, Sitecore.EmailCampaign.Cd" resolve="true">

              <IgnoredUrlPattern>/news/unsubscribe|SubscriptionPreferences.ashx|.*ConfirmSubscription.aspx|.*Unsubscribe.aspx|UnsubscribeFromAll.aspx.*|.*sc_pd_view=1.*</IgnoredUrlPattern>

            </processor>

In the above patch file, I have added my custom page in the URL pattern parameter. Here my custom page location in the Sitecore content tree is "/sitecore/content/{Project Name}/Home/news/ unsubscribe", which is why I have added it as "news/unsubscribe" in the patch file.

In "fieldKey" parameter you can find the defined querystringkey fields like MessageId, AnalyticsContactId, etc.

sc-custom-exm-unsubscribe-page

Once the user will redirect to the custom page, you can get the message id and contact identifier form query string using the below code:

 Contact contact = null;
if (Sitecore.Modules.EmailCampaign.Core.ExmContext.IsEcmIdVerified)
  {
  	contact = Sitecore.Modules.EmailCampaign.Core.ExmContext.Contact;
var ContactId = Convert.ToString(contact.Id)
  }

Now you have a contact Id, from XDB you can fetch other facts of the contact and perform the operation as per your requirement using Sitecore APIs.

Reference: https://doc.sitecore.com/en/developers/exm/101/email-experience-manager/the-unsubscribe-options.html

Happy Sitecoreing ðŸ˜Š

Comments

Popular posts from this blog

Sitecore Installation Error: Failed to Start Service 'Sitecore Marketing Automation Engine'

Import CSV Data in Sitecore Using PowerShell: Part-3

Sitecore : The database principal owns a schema in the database, and cannot be dropped message