JavaScript API#

Getting Started#

The PushEngage Javascript API is used to control the subscribers. The subscriber is a user that subscribes to your website and get notification.
The PushEngage javaScript API allows you to

  • Getting Subscriber Hash after Subscription
  • Add a Segment to a Subscriber
  • Remove a Subscriber from a Segment
  • Add Profile Id to a Subscriber

Getting Subscriber Hash after Subscription#

This API call returns unique id of the subscriber in the form of string. This API valid for HTTPS sites only.

    _pe.subscribe({}.pe,function(data){
        console.log(data.subscriber_hash)});

Add a Segment to a Subscriber#

Add a segment to a subscribe and notification will be sent to only those subscribers which are subscribed with this segment. Segment name is a user defined name that used to target subscribers based on location or category. Create a segment name from PushEngage dashboard under subscribers → Segments.

Case a: Add a segment to a subscriber at the time of subscription with pass parameter as a "segmentname"

    _pe.subscribe('segmentname');

OR,

This API valid for HTTPS sites only.

    _pe.subscribe('segmentname',function(data){
        console.log(data.subscriber_hash)});

Case b: Add a segment to a subscriber on any event on your site with pass parameter as a "segmentname"

    _pe.addSubscriberToSegment('segmentname');

Note

A subscriber can belong to multiple segments.

Remove a Subscriber from a Segment#

Remove a subscriber from a particular segment with pass parameter as a "segmentname".

    _pe.removeSubscriberFromSegment('segmentname');

Find If a User is Already Subscribed#

This API call returns if a user is subscribed or not. This API valid for HTTPS sites only.

    _pe.isSubscribed(
    function(isSubscribed){
    console.log(isSubscribed);
    });

If user is subscribed, the return value is true, otherwise it is false.

OR,

    _pe.isSubscribed(
    function(isSubscribed){
    console.log(isSubscribed);
    },true);

If the user passes true as the second parameter, it will return subscriber hash. Subscriber hash is a unique hash generated to identify the subscriber if subscriber hash is present means the user is subscribed.

Add Profile Id to a Subscribers#

Add profile id to subscribers with pass parameter as a "profileid" and notifications will be sent to only those devices which are having this profile id. Profile id is a user defined name that used to target particular subscribers with unique IDs.

    _pe.addProfileId("profileid");

Note

An API to send notification to a specific profile id.

Callback for Allow, Deny and Closed action in subscription dialogbox#

This API call returns "CLOSED", "DENIED" and "SUBSCRIBED". This API valid for HTTPS sites only.

    _pe.subscribe({}.pe,function(data){        
             console.log(data.status);
             //  =="CLOSED" (This will be the status when the subscriber clicks on X of the native popup)
             //  =="DENIED" (This will be the status when the subscriber clicks on deny button of the native popup)
             //  =="SUBSCRIBED" (This will be the status when the subscriber successfully allows the permission to notifications)        
             if(data.status=="SUBSCRIBED")
             console.log(data.subscriber_hash);//(This will be filled when the user subscribes to the notifications)
    });