We’ve migrated our documentation to a new site, which means some URLs have changed.
Subscriptions

How to automatically add users with a specific Identity Management custom field value to an ESP mailing list?

If you have a Custom field with a checkbox type in a form shown in your Piano ID register page template and you want to automatically add users that have checked this field to an ESP mailing list, you can use the loginSuccess callback to perform this action. Please follow the steps below:

  1. Add the following code to your Piano load script:

    tp.push(["addHandler", "loginSuccess", function (data) {
    console.log('data');    
    console.log(data);
                if (data.registration) {
                    var CFields = [];
                    var MAILING_LISTS = [];
    
    
                    tp.pianoId.loadExtendedUser({
                        extendedUserLoaded: function (data) {
    
                            var email = tp.pianoId.getUser() ? tp.pianoId.getUser().email : data.user.email;
                            var API_URL = 'https://sandbox-api-esp.piano.io';
                            var SITE_ID = <ESP_Site_ID>;
    
                            CFields = data.custom_field_values;
                            console.log('fields');
                                console.log(CFields);
    
                            for (var i in CFields) {
                                var fieldName = CFields[i].field_name;
                                var fieldValue = CFields[i].value;
                                //         console.log(fieldName, fieldValue);
                                if (fieldName == 'Field_1') {
                                    if (fieldValue == 'true') {
                                        MAILING_LISTS = [<ML_ID>]
                                    }
                                }
                            };
    
    
                            console.log(MAILING_LISTS);
    
    
                            var body = { email: email, sqids: MAILING_LISTS };
                            var xhr = new XMLHttpRequest();
                            xhr.open('POST', API_URL + '/tracker/lucid/sub/' + SITE_ID, true);
                            xhr.setRequestHeader('Content-type', 'application/json');
                            xhr.send(JSON.stringify(body))
    
                        },
                        formName: 'RegistrationFields'
                    });
                };
    
            }]);
    
  2. Replace the values for the following variables with IDs and names from your Piano / ESP dashboard:

    formName - custom field form name that contains the custom field checkbox, in the example "RegistrationFields" is used
    fieldName - the custom field ID of the checkbox, in the example "Field_1" is used
    API_URL - ESP API URL based on the ESP environment
    SITE_ID - ESP Site ID
    MAILING_LISTS - The ID of the ESP Maling List

Last updated: