0

I have a temporary data extension (will be updated daily) with subscribers need to be unsubscribed. It looks like this:

enter image description here

SubscriberKey, EmailAddress and Status were queried from All Subscribers list (ent.__Subscribers)

And a logging data extension to store logs.

enter image description here

In order to unsubscribe, I used a script activity:


<script runat="server" type="text/javascript">

Platform.Load("core", "1");
var unsubDE = DataExtension.Init('424ded4d-88cd-eb11-b824-48df37dc126b');
var data = unsubDE.Rows.Retrieve();
var logging = [];
if(data.length > 0) {
        for (var i = 0; i < data.length; i++)
        {
            var SubId = data[i].SubscriberKey;
            var subObj = Subscriber.Init(SubId);
            var status = subObj.Unsubscribe();
            if (status != "error") {      
             logging.push({SubscriberKey: SubId, EmailAddress: data[i].EmailAddress, Email_Status: data[i].Email_Status, Status: 'Unsubscribe Complete', Timestamp: Date.now()});              
            } else {
              logging.push({SubscriberKey: SubId, EmailAddress: data[i].EmailAddress, Email_Status: data[i].Email_Status, Status: 'Error Processing Optout', Timestamp: Date.now()});
                };                
            
        };
        var loggingDE = DataExtension.Init('f222668a-c0cd-eb11-b824-48df37dc126b');
        loggingDE.Rows.Add(logging);
    };

</script>

I get the ID 424ded4d-88cd-eb11-b824-48df37dc126b of the temporary data extension and the remaining one by this Way
Then run once for the script activity. However, it hasn't been added into the logging data extension after that and the subscriber hasn't been unsubscribed (checked in all subscribers list).
I'm not sure how to debug and fix it. Really appreciate if anyone can take a look at it.
P/s: the idea came from an Answer on salesforce stackexchanger.

2

Toggling the status doesn't generate tracking data for unsubscribe events. You'll have to log an actual unsubscribe event:

Here's a WSProxy function to do that:

  function logUnsubEvent(mid, SubscriberKey, EmailAddress, JobID, ListID, BatchID, Reason) {

    var prox = new Script.Util.WSProxy();
    prox.setClientId({"ID": mid});

    var props = [];

    props.push({ Name: "SubscriberKey", Value: SubscriberKey });
    props.push({ Name: "EmailAddress", Value: EmailAddress });

    if (JobID && JobID != '') {
      props.push({ Name: "JobID", Value: JobID });
    }

    /*
    if (ListID && ListID != '') {
      props.push({ Name: "ListID", Value: ListID });
    }

    if (BatchID && BatchID != '') {
      props.push({ Name: "BatchID", Value: BatchID });
    }
    */

    props.push({ Name: "Reason", Value: Reason});

    try {

          if (SubscriberKey && SubscriberKey != "") {

            var response = prox.execute(props, "LogUnsubEvent");

            if (debug) {
               Write("<br><br>props: " + Stringify(props));
               Write("<br>response: " + Stringify(response));
               Write("<br>response.Results[0].StatusCode: " + response.Results[0].StatusCode);
               Write("<br>response.Results[0].ErrorCode: " + response.Results[0].ErrorCode);
               Write("<br>response.Results[0].StatusMessage: " + response.Results[0].StatusMessage);
            }

            var result = ((response.Results[0].ErrorCode == "0" && response.Results[0].StatusMessage == "Event posted") || (response.Results[0].ErrorCode == "12012" && response.Results[0].StatusMessage == "OnMasterUnsubscribeList") || (response.Results[0].ErrorCode == "401" && response.Results[0].StatusMessage == "Subscriber is already unsubscribed")) ? ["OK"] : ["Error"];

            return result[0];
          }

    } catch (e) {

      writeLog("Exception", Stringify(e));

    }


  }

Also, unsubscribe events are not instantaneous, so you may have to wait a minute for them to register.

4
  • Thanks @Adam. It would be nice to have tracking data for subscribe event. To be honest, I'm not familiar with the parameters in the function. Which/Where values should I put in JobID, ListID, BatchID, Reason? I know it's not a good recommend but in case I still want to stick with my initial code. Do you have any idea where the mistakes are being made? Any helps would be appreciated.
    – duyduc27
    Jun 17 at 0:16
  • 1
    If you don't specify JobID, ListID, BatchID, it'll be a global unsub. Reason is whatever you need/want it to be. Jun 17 at 2:30
  • 1
    The _Subscribers data view is not an accurate source of status. I would check it in All Subscribers in the UI in the business unit. The ent._Subscribers data view would be the global status. Jun 17 at 2:31
  • thank you so much. I can be able to unsubscribe them now! How do I query the data from the unsubscribe event which I used in the function like mid, SubscriberKey, EmailAddress, JobID, Reason? I took a look at _Unsubscribe and it doesn't have those column names
    – duyduc27
    Jun 17 at 3:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.