Log an Unsubscribe Event with Ampscript in Salesforce Marketing Cloud

Author: Florin Valean

Use case

A common scenario in which you would need to log an unsubscribe event is when you are required to create a custom landing page for a one-click unsubscribe. In this case you'll need to create a cloud page first which will be your landing page and then include an unsubscribe link in your emails. The unsubscribe link will redirect to your cloud page. Create this link using CloudPagesURL ampscript function as shown in the sample below:

/* replace YOUR_CLOUD_PAGE_NUMERIC_ID with your cloud page ID that can be found on the cloud page details page */ <a href="%%=CloudPagesURL('YOUR_CLOUD_PAGE_NUMERIC_ID', 'qs', _subscriberkey)=%%">Unsubscribe here</a>

CloudPagesURL function will create the link in this format:
http(s)://YOUR_DOMAIN_NAME/YOUR_CLOUD_PAGE_NAME?qs=b466fb7030b7cff385dca3d3e925ac04ac72d8764dde678d62480d89e454bce5a13xyz33c779ffdf8a055948f1e5754be8764efc353e75afdd64d6b20d781c82
where qs is the query string parameter (its value is a 128 characters long string) that encodes all personalization strings available for the email at the moment of send such as:

*) These personalization strings will be used in the unsubscribe cloud page

The unsubscribe cloud page

When the unsubscribe link in the email is clicked it will redirect to the unsubscribe cloud page where the unsubscribe will be performed. The correct way to perform the unsubscribe is by executing a LogUnsubEvent request as shown in the code below:

%%[ /* declare variables */ var @sid, @jid, @listid, @batchid, @reason, @lue, @lue_prop, @lue_statusCode, @overallStatus, @requestId, @Response, @Status, @Error /* import the personalization string values from the query string into the variables */ set @skey = _subscriberkey set @jobid = jobid set @listid = listid
set @batchid = _JobSubscriberBatchID
/* set the reason for the unsubscribe */ set @reason = "Custom Unsubscribe" /* initiate the LogUnsubEvent request */ set @lue = CreateObject("ExecuteRequest")
SetObjectProperty(@lue, "Name", "LogUnsubEvent")
/* configure the properties of the API object */ set @lue_prop = CreateObject("APIProperty") SetObjectProperty(@lue_prop, "Name", "SubscriberKey") SetObjectProperty(@lue_prop, "Value", @skey) AddObjectArrayItem(@lue, "Parameters", @lue_prop) set @lue_prop = CreateObject("APIProperty") SetObjectProperty(@lue_prop, "Name", "JobID") SetObjectProperty(@lue_prop, "Value", @jobid) AddObjectArrayItem(@lue, "Parameters", @lue_prop) set @lue_prop = CreateObject("APIProperty") SetObjectProperty(@lue_prop, "Name", "ListID") SetObjectProperty(@lue_prop, "Value", @listid) AddObjectArrayItem(@lue, "Parameters", @lue_prop) set @lue_prop = CreateObject("APIProperty") SetObjectProperty(@lue_prop, "Name", "BatchID") SetObjectProperty(@lue_prop, "Value", @batchid) AddObjectArrayItem(@lue, "Parameters", @lue_prop) set @lue_prop = CreateObject("APIProperty") SetObjectProperty(@lue_prop, "Name", "Reason") SetObjectProperty(@lue_prop, "Value", @reason) AddObjectArrayItem(@lue, "Parameters", @lue_prop) /* you have to set the ClientID only when working with On Your Behalf accounts var @mid, @lue_oyb set @mid = memberid set @lue_oyb = CreateObject('ClientID') SetObjectProperty(@lue_oyb, 'ID', @mid) SetObjectProperty(@lue, 'Client', @lue_oyb) */ /* this is where the unsubscribe is performed */ set @lue_statusCode = InvokeExecute(@lue, @overallStatus, @requestId) /* check status/errors set @Response = Row(@lue_statusCode, 1) set @Status = Field(@Response, "StatusMessage") set @Error = Field(@Response, "ErrorCode") */ ]%%


View the unsubscribe in the list

As a result the Status attribute will be set to 'Unsubscribed' in the list and the Unsubscribe Reason will be set to 'Custom Unsubscribe'. Unsubscribe Date will also be updated with the current datetime.
The main reason for executing the LogUnsubEvent request rather than just updating the Status attribute for the subscriber is to enable reporting on the status. If we would simply update the status to 'Unsubscribed' without logging the unsub event, the unsubscribe would not appear in the reports.

This is how the custom unsubscribe appears in my All Subscribers list:

Custom unsubscribed in the All Subscribers list
Share this page
Stay in touch

Subscribe to the newsletter

p1 p2 p3