JavaScript Async API#

Getting Started#

The PushEngage JavaScript Async 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 Async API, if you have added async installation code from the PushEngage dashboard on your website. Since it is an async API, it will only work on page load.

The PushEngage JavaScript API allows you to

  • Register Callback for New Subscription
  • Subscriber Hash for New Subscription
  • Geo Information 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#

window._peq.push(['init',{}.pe,function(res){
    if(res.statuscode==1)
    {
      // Code to return response
      console.log(res);
    }
}]);

Subscriber Hash for New Subscription#

window._peq.push(['init',{}.pe,function(res){
    if(res.statuscode==1)
    {
      //Get your subscriber hash in response 
      console.log(res.subscriber_hash);
    }
}]);

Geo Information for New Subscription#

window._peq.push(['init',{}.pe,function(res){
    if(res.statuscode==1)
    {
      // Code to return response
      console.log(res.data.geoInfo);
    }
}]);

Response


{statuscode:1,status:"SUBSCRIBED",message:"User subscribed sucessfully",subscriber_hash:"UserUniqueId",data:{geoInfo:{country: "Your Country", state: "Your State", city: "Your City"}}}
{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.

window._peq.push(['subscriber-status',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 the 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"

window._peq.push(['init','segmentName']);

OR,

window._peq.push(['init','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 page load of your site by passing parameter as a "segmentName"

window._peq.push(['add-to-segment','segmentName']);

OR,

window._peq.push(['add-to-segment','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 number of days, subscriber will be removed from passed segment name.

window._peq.push(['add-to-dynamic-segment','segmentName',days]);

OR,

window._peq.push(['add-to-dynamic-segment','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".

window._peq.push(['remove-to-segment','segmentName']);

OR,

window._peq.push(['remove-to-segment','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.

window._peq.push(['add-to-profile','profileId']);

OR,

window._peq.push(['add-to-profile','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.