Subscribers can be categorized into different segments. This allows you to categorize and target users who have similar behavior on your website. For example :- for all users who get subscribed to push notifications from the homepage, you may want to put them into a segment 'homepage'. Later on, you can use the REST API/our dashboard to send notifications to subscribers of that particular segment only.

Notes:

  1. A subscriber can belong to multiple segments.
  2. This operation will only work reliably if it's placed inside the APIReady callback. (Example given below)
  3. This operation will only put the subscriber in the segment once the API gets ready(on page load). If a new user subscribes, you can use the same operation inside the subscriptionSuccessCallback (Example given below)

Parameters:

  1. addSubscriberToSegment: Command to add the subscriber to the specified segment
  2. segmentName: This is a string used to identify the segment. Maximum length of segment name can be 30 chars and it can only contain alphanumeric characters, underscore and dash.
  3. callbackFunctionName: (Optional)
    Name of the function to be executed when the subscriber gets added to the segment successfully. This is optional.

Example usage:

_pcq.push(['addSubscriberToSegment', 'homepage', callbackFunction]);

window._pcq = window._pcq || [];
_pcq.push(['APIReady', callbackOnAPIReady]); //will execute callback function when VWO Engage API is ready

function callbackOnAPIReady() {
    //now api is ready
    _pcq.push(['addSubscriberToSegment', 'homepage', callbackForAddToSegment]);
}

function callbackForAddToSegment(response) {
    if(response === -1) {
        console.log('User is not a subscriber or has blocked notifications');
    }
  
    if(response === false) {
        console.log('Segment name provided is not valid. Maximum length of segment name can be 30 chars and it can only contain alphanumeric characters, underscore and dash.');
    }
  
    if(response === true) {
        console.log('User got added to the segment successfully. Now you may run any code you wish to execute after user gets added to segment successfully');
    }
}

If the user needs to be put in a segment as soon as he/she subscribes, here is some example code:

window._pcq = window._pcq || [];
_pcq.push(['APIReady', callbackOnAPIReady]); //will execute callback function when VWO Engage API is ready

_pcq.push(['subscriptionSuccessCallback',callbackOnSuccessfulSubscription]); //registers callback function to be called when user gets successfully subscribed

function callbackOnAPIReady() {
    //now api is ready
    _pcq.push(['addSubscriberToSegment', 'homepage', callbackForAddToSegment]);
}

function callbackOnSuccessfulSubscription(subscriberId, values) {
    //user just got subscribed
    _pcq.push(['addSubscriberToSegment', 'homepage', callbackForAddToSegment]);
}

function callbackForAddToSegment(response) {
    if(response === -1) {
        console.log('User is not a subscriber or has blocked notifications');
    }
  
    if(response === false) {
        console.log('Segment name provided is not valid. Maximum length of segment name can be 30 chars and it can only contain alphanumeric characters, underscore and dash.');
    }
  
    if(response === true) {
        console.log('User got added to the segment successfully. Now you may run any code you wish to execute after user gets added to segment successfully');
    }
}

If you want to add subscribers into segment depending upon the url(specific keyword in the url) they visit, here is some sample code:

<script type="text/javascript">

window._pcq = window._pcq || [];
_pcq.push(['APIReady', callbackOnAPIReady]); //will execute callback function when VWO Engage API is ready

_pcq.push(['subscriptionSuccessCallback',callbackOnSuccessfulSubscription]); //registers callback function to be called when user gets successfully subscribed

function addToAppropriateSegment() {
    if(window.location.href.indexOf("category1") > -1) {
        _pcq.push(['addSubscriberToSegment', 'category1']);
    }
    else if(window.location.href.indexOf("category2") > -1) {
        _pcq.push(['addSubscriberToSegment', 'category2']);
    }
    else if(window.location.href.indexOf("category3") > -1) {
        _pcq.push(['addSubscriberToSegment', 'category3']);
    }
}

function callbackOnAPIReady() {
   addToAppropriateSegment();
}

function callbackOnSuccessfulSubscription(subscriberId, values) {
   addToAppropriateSegment();
}