JS API

All you need to know about PushAlert JS API.

Welcome to the JS API. Below, you’ll find a full listing of all the available functions. As we add more functionality, they will be automatically documented here.

If you have any issue or need any help using the JS API, please send us a mail at api@pushalert.co.

For more information about a particular function, click on its name under the Resource header. You’ll be taken to the function’s documentation along with an example.

Resource Description
onSuccess/onFailure To add callback on success and failure.
disableAutoInit To disable automatic subscription box.
subscribeToSegment Initiate subscriber box with the given segment.
addToSegment Add subscriber to a segment.
removeFromSegment Remove subscriber from a segment.
unsubscribe Unsubscribe the user.

onSuccess/onFailure

You can add your callback function when user successfully subscribes or fails to subscribe.

Example

<script>
    (pushalertbyiw = window.pushalertbyiw || []).push(['onSuccess', callbackOnSuccess]);

    function callbackOnSuccess(result) {
        console.log(result.subscriber_id); //will output the user's subscriberId
        console.log(result.alreadySubscribed); // False means user just Subscribed

        //YOUR CODE
    }
</script>
<script>
    (pushalertbyiw = window.pushalertbyiw || []).push(['onFailure', callbackOnFailure]);

    function callbackOnFailure(result) {
        console.log(result.status); //-1 - blocked, 0 - canceled or 1 - unsubscribed

        //YOUR CODE
    }
</script>

disableAutoInit

In case, if you don't want requests for subscription to be sent automatically, use the following code

Example

<script>
    (pushalertbyiw = window.pushalertbyiw || []).push(['disableAutoInit', false]);
</script>
<script>
PushAlertCo.init() //You can call this method to request subscription box manually
</script>

subscribeToSegment

If you provide segment ID to this function, when user subscribes, they will also be automatically added to the mentioned segment.

Example

<script>
    (pushalertbyiw = window.pushalertbyiw || []).push(['subscribeToSegment', 3383]); //You can get segment ID from dashboard or via REST API
</script>

addToSegment

To add a user to a particular segment. This function will do nothing if the user is not subscribed, else they will be automatically added to mentioned segment. Additionally you can mention a callback function.

Example

<script>
    (pushalertbyiw = window.pushalertbyiw || []).push(['addToSegment', 993, callbackFunction]); //You can get segment ID from dashboard or via REST API

    function callbackFunction(result){
        console.log(result.success) // True or False
        //Your Code
    }
</script>

removeFromSegment

To remove a user from a particular segment. This function will do nothing if the user is not subscribed, else they will be automatically removed from the mentioned segment. Additionally you can mention a callback function.

Example

<script>
    (pushalertbyiw = window.pushalertbyiw || []).push(['removeFromSegment', 993, callbackFunction]); //You can get segment ID from dashboard or via REST API

    function callbackFunction(result){
        console.log(result.success) // True or False
        //Your Code
    }
</script>

unsubscribe

For opting-out of Push Notifications. User won't receive any notification if succesfully unsubscribed.

Example

<script>

    PushAlertCo.unsubscribe();

</script>