JavaScript API#

Getting Started#

The PushEngage JavaScript API is used to gather information about your web push subscribers. The subscriber is a user, who clicks “allow” on your notification permission box, and subscribes to your website.

Note

Use JavaScript API, if you have added Legacy installation code from the PushEngage dashboard on your website.

The PushEngage JavaScript API allows you to

  • Register Callback for New Subscription
  • Subscriber Hash for New Subscription
  • Get User Subscription Status
  • Add a Segment to a Subscriber
  • Remove a Subscriber from a Segment
  • Add Profile Id to a Subscriber

Register Callback for New Subscription#

This API is valid for HTTPS sites with quick install off.

_pe.subscribe({}.pe,function(res){
    if(res.statuscode==1)
    {
      // Code to return response 
      console.log(res);
    }
});

Subscriber Hash for New Subscription#

This API is valid for HTTPS sites with quick install off.

_pe.subscribe({}.pe,function(res){
    if(res.statuscode==1)
    {
      //Get your subscriber hash in response 
      console.log(res.subscriber_hash);
    }
});

Response


{statuscode:1,status:"SUBSCRIBED",message:"User subscribed sucessfully",subscriber_hash:"UserUniqueId"}
{statuscode:2,status:"DENIED",message:"User denied push notification"}
{statuscode:3,status:"CLOSED",message:"User closed subscription Opt-in"}
{statuscode:0,message:"Something went wrong"}
{statuscode:20,message:"Your website url is not allowed, please configure in PushEngage correctly"}
{statuscode:21,message:"Your browser does not support PushEngage web push notification"}

User Subscription Status#

This API call returns user subscription status in the form of object.

_pe.subscriberStatus(function(res){
    if(res.statuscode==1)
    {
      // Code to return response
      console.log(res);
    }
});

Response


{statuscode:1,status:"SUBSCRIBED",message:"User subscribed sucessfully",data:{subscriber_hash:"UserUniqueId"}}
{statuscode:2,status:"DENIED",message:"User denied push notification"}
{statuscode:3,status:"CLOSED",message:"User closed subscription Opt-in"}
{statuscode:20,message:"Your website url is not allowed, please configure in PushEngage correctly"}
{statuscode:21,message:"Your browser does not support PushEngage web push notification"}
{statuscode:22,message:"User not subscribed"}

Add a Segment to a Subscriber#

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

Case a: Add a segment to a subscriber at the time of subscription by passing parameter as a "segmentName"

_pe.subscribe('segmentname');

OR,

This API is valid for HTTPS sites with quick install off.

_pe.subscribe('segmentName',function(res){
    if(res.statuscode==1)
    {
      // Code to return response 
      console.log(res);
    }
});

Response


{statuscode:1,status:"SUBSCRIBED",message:"User subscribed sucessfully",subscriber_hash:"UserUniqueId"}
{statuscode:2,status:"DENIED",message:"User denied push notification"}
{statuscode:3,status:"CLOSED",message:"User closed subscription Opt-in"}
{statuscode:0,message:"Something went wrong"}
{statuscode:20,message:"Your website url is not allowed, please configure in PushEngage correctly"}
{statuscode:21,message:"Your browser does not support PushEngage web push notification"}

Case b: Add a segment to a subscriber on any event on your site by passing parameter as a "segmentName"

_pe.addSubscriberToSegment('segmentName');

OR,

_pe.addSubscriberToSegment('segmentName',function(res){
    if(res.statuscode==1)
    {
      // Code to return response
      console.log(res); 
    }
});

Response


{statuscode:1,message:"Segment added sucessfully"}
{statuscode:2,message:"Segment already added"}
{statuscode:3,message:"Segment not found"}
{statuscode:0,message:"Something went wrong"}
{statuscode:20,message:"Your website url is not allowed, please configure in PushEngage correctly"}
{statuscode:21,message:"Your browser does not support PushEngage web push notification"}
{statuscode:22,message:"User not subscribed"}
{statuscode:23,message:"Subscriber is not valid for request api"}
{statuscode:24,message:"Your given segment is not valid"}

Add a Dynamic Segment to a Subscriber#

Case c: Add a dynamic segment to a subscriber on page load of your site by passing parameter as a "segmentName" and days, where after the given days subscriber will be removed from passed segment name.

_pe.addSubscriberToDynamicSegment('segmentName',days);

OR,

_pe.addSubscriberToDynamicSegment('segmentName',days,function(res){
    if(res.statuscode==1)
    {
      // Code to return response 
      console.log(res);
    }
});

Response


{statuscode:1,message:"Segment added sucessfully"}
{statuscode:2,message:"Segment already added"}
{statuscode:3,message:"Segment not found"}
{statuscode:0,message:"Something went wrong"}
{statuscode:20,message:"Your website url is not allowed, please configure in PushEngage correctly"}
{statuscode:21,message:"Your browser does not support PushEngage web push notification"}
{statuscode:22,message:"User not subscribed"}
{statuscode:23,message:"Subscriber is not valid for request api"}
{statuscode:24,message:"Your given segment or days is not valid"}

Note

A subscriber can belong to multiple segments.

Remove a Subscriber from a Segment#

Remove a subscriber from a particular segment by passing parameter as a "segmentName".

_pe.removeSubscriberFromSegment('segmentName');

OR,

_pe.removeSubscriberFromSegment('segmentName',function(res){    
    if(res.statuscode==1)
    {
      // Code to return response
      console.log(res); 
    }
});

Response


{statuscode:1,message:"Segment removed sucessfully"}
{statuscode:2,message:"Segment already removed"}
{statuscode:0,message:"Something went wrong"}
{statuscode:20,message:"Your website url is not allowed, please configure in PushEngage correctly"}
{statuscode:21,message:"Your browser does not support PushEngage web push notification"}
{statuscode:22,message:"User not subscribed"}
{statuscode:23,message:"Subscriber is not valid for request api"}
{statuscode:24,message:"Your given segment is not valid"}

Add Profile Id to a Subscribers#

Add profile id to a subscriber by passing 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 is used to target particular subscribers with unique IDs.

_pe.addProfileId('profileId');

OR,

_pe.addProfileId('profileId',function(res){
    if(res.statuscode==1)
    {
      // Code to return response 
      console.log(res);
    }
});

Response


{statuscode:1,message:"Profile added sucessfully"}
{statuscode:2,message:"Profile already exists"}
{statuscode:0,message:"Something went wrong"}
{statuscode:20,message:"Your website url is not allowed, please configure in PushEngage correctly"}
{statuscode:21,message:"Your browser does not support PushEngage web push notification"}
{statuscode:22,message:"User not subscribed"}
{statuscode:23,message:"Subscriber is not valid for request api"}

Note

Using this Profile Id, you can send notification to specific subscribers.

Find If a User is Already Subscribed deprecated#

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.

Unsubscribe a User#

This API call unsubscribes the user.

_pe.unsubscribe();

OR,

_pe.unsubscribe(function(res){    
    if(res.statuscode==1)
    {
      // Code to return response
      console.log(res); 
    }
});

Response


{statuscode:1,message:"User is unsubscribed successfully"}
{statuscode:0,message:"Something went wrong"}
{statuscode:20,message:"Your website url is not allowed, please configure in PushEngage correctly"}
{statuscode:21,message:"Your browser does not support PushEngage web push notification"}
{statuscode:22,message:"User not subscribed"}
{statuscode:23,message:"Subscriber is not valid for request api"}