4

In Salesforce Marketing Cloud (SFMC), I want to unsubscribe a contact from a single publication list by using a form on a landing page.

I also want to create an Unsub event in SFMC when they submit the form.

There is some sample code from SFMC Help documents here that seems to do that for external sites. Will it work in a SFMC hosted landing page? Do I need to edit the code much?

2 Answers 2

9

The code in the documentation should work fine, you just need to make sure the required variables all get set properly before executing the API calls.

This piece of code is using the RequestParameter function to set @sid, @jid, @listid and @batchid from a form. You would want to create a form that POSTs to a page that contains this AMPscript.

SET @sid = IIF(Empty(RequestParameter("email_address")),RequestParameter("current_email_address"),RequestParameter("email_address"))
SET @jid = RequestParameter("jobid")
SET @listid = RequestParameter("listid")
SET @batchid = RequestParameter("batchid")

One thing about the code sample on their docs is that it sets a variable called @sid, which is used to actually set the SubscriberKey, this is sort of misleading because not all accounts use the email address as Subscriber Key.

How I do this

Just to make things easier for you I will post the full code that I use on my Preference Center.

You are going to need a way to pass the required values from the email to the landing page. One good way to send the values behind the scenes to the microsite is to use the Microsite_URL function when linking to your preference center.

In your email:

<a href="%%=MicrositeURL(123456)=%%">Unsubscribe</a>

Note: replace 123456 with the Id if your landing page

On the landing page:

    VAR @sid, @jid, @listid, @batchid, @reason, @lue, @lue_prop, @lue_statusCode, @overallStatus, @requestId, @lue_Response, @lue_Status, @lue_Error
    SET @sid = SubscriberID
    SET @jid = JobID
    SET @listid = ListID
    SET @batchid = _JobSubscriberBatchID
    SET @reason = "Landing Page Unsubscribe"

    SET @lue = CreateObject("ExecuteRequest")
    SetObjectProperty(@lue,"Name","LogUnsubEvent")

    SET @lue_prop = CreateObject("APIProperty")                 
    SetObjectProperty(@lue_prop, "Name", "SubscriberID")
    SetObjectProperty(@lue_prop, "Value", @sid)
    AddObjectArrayItem(@lue, "Parameters", @lue_prop)

    SET @lue_prop = CreateObject("APIProperty")
    SetObjectProperty(@lue_prop, "Name", "JobID")
    SetObjectProperty(@lue_prop, "Value", @jid)
    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)

    SET @lue_statusCode = InvokeExecute(@lue, @overallStatus, @requestId)

    SET @lue_Response = Row(@lue_statusCode, 1)
    SET @lue_Status = Field(@lue_Response,"StatusMessage")
    SET @lue_Error = Field(@lue_Response,"ErrorCode")

The URL that is generated by Microsite_URL does not expose any information in the URL, but allows you to access all the subscriber information on the landing page. One you have the values you can use them however you want, usually in a form or logging the unsub event automatically.

The above code sample will create a one click unsubscribe. Most companies prefer to let their user confirm once they are on the landing page. For this you will need to create a form that POSTs these values or some other page in between the email click and the final unsub processing.

0
0

As an addendum to the great answer above.

On an external page, there are plausible scenarios where JobId cannot be determined. E.g. the recipient is not coming to the page from an email, but for example logs in to a customer account in another system and gets access to a custom subscription center there, connected via APIs.

If the external page POSTs to an API handler that logs unsub events, setting jobid to "0" in there will perform the unsubscription, but will NOT create a record in _Unsubscribe Data View, as jobId "0" acts like a test sendout.

A workaround / solution to this is documented here, as JobId, ListId and BatchID can complement each other:

https://developer.salesforce.com/docs/atlas.en-us.mc-apis.meta/mc-apis/unsubscribing_and_logging_an_unsubevent_with_a_logunsubevent_execute_call.htm

You don't need to supply all three values [jobID,listID,BatchID]. The system looks up any missing values using the following rules: If the JobID is supplied, we can lookup a missing ListID and/or BatchID.

If the ListID is supplied, we can lookup a missing JobID and/or BatchID. If the JobID is missing, we use the most recent JobID that the subscriber was sent to.This may not be the Job that the Subscriber is acting upon.

If only the BatchID is supplied, we cannot lookup the remaining information and the job context is not defined.

So: If you have no means of getting to a JobId but want an _Unsubscribe Event logged, then you can fallback to a logUnsubEvent with only listID, using the listId of the List the user was sent against, and the last jobId to the recipient will be determined and logged as an event in _Unsubscribe.

In case you use Publication Lists, the ListId is different for each subscription. You'd have to map each subscription the user manages to the ID of the respective Publication List.

In case you don't, the listId can be set to the listId of the All Subscribers List, but you will log a "Master unsubscribe" following the rules determined in the Business Unit settings (= BU-wide or global) and you would have to work from there.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .