Skip to content

Attributes

Introduction

Attributes is a user defined key-value pair that is used for sending personalized notification.

  • Targeting notifications to specific users or groups using attributes.
  • Targeting notifications for sending personalized notification.

Constraint:

  • Attribute key must be alphanumeric and also can have hyphen (-), underscore (_).
  • Attributes key is case sensitive .
  • Attributes key cannot start with hyphen (-), underscore (_).
  • Attribute key can contain up to 255 characters.
  • Attribute value should be number, string, boolean, ISO8601DateString.
  • Attribute value can contain up to 256 characters.

Example :

{
    "name": "Jon smith", 
    "score": 500,
    "dob": "2019-09-19T05:41:53.150Z",
    "isActive": true
}

Working with Attributes

Once you've integrated pushengage in your website, implementing Attribute is very straightforward - simply use the setAttribute/addAttribute/getAttribute/removeAttribute javascript api to work with attribute. See below for detailed description.

Set Attributes

This api is used to set attributes to a subscriber. This will overwrite any other attributes previously associated with current subscriber. Use this api whenever you need to completely reset the attribute to a new values.

var newAttributes = {
  'name': 'Jon smith',
  'isAdmin': true,
  'lastActive': "2019-09-19T05:41:53.150Z",
};

window._peq.push(['set-attributes', newAttributes, function(res){
  if(res.statuscode == 1)
  {
    // handle response
    console.log(res);
  }
}]);

Response

{statuscode: 1, message: "Attribute replaced successfully", data: {…}}
{statuscode: xxxxx, message: "Error message", data: {…}}

Add Attributes

This api is used to add new attributes to the existing attribute of a subscriber. If there is already existing attribute with the given attribute key name then the value will be replaced with new value.

var attribute = {
  'gender': 'female',
  'postCode': '560024',
}
window._peq.push(['add-attributes', attribute, function(res){
 if(res.statuscode == 1)
 {
    // handle response
  console.log(res);
 }
}]);

Response

{statuscode: 1, message: "Attribute added successfully", data: {…}}
{statuscode: xxxxx, message: "Error message", data: {…}}

Get Attributes

Use this api to get the list of attributes associated with current subscriber.

window._peq.push(['get-attributes', function(res){
 if(res.statuscode == 1)
 {
    // handle response
  console.log(res);
 }
}]);

Response

{statuscode: 1, message: "", data: {'name': 'Jon smith','isAdmin': true,'lastActive': "2019-09-19T05:41:53.150Z",'gender': 'female','postCode': '560024'}}
{statuscode: xxxxx, message: "Error message", data: {…}}

Remove Attributes

Use this api to remove one or more attribute associated with current subscriber. The api accept the array of attribute name to remove from current subscriber. If you pass empty array then the api will remove all the attributes associated with subscriber.

// remove all attributes 
window._peq.push(['remove-attributes', [], function(res){
  if(res.statuscode == 1)
 {
    // handle response
  console.log(res);
 }
}]);

// remove name and age associated with current subscriber
window._peq.push(['remove-attributes', ['name', 'age'], function(res){
  if(res.statuscode == 1)
 {
    // handle response
  console.log(res);
 }
}]);

Response

{statuscode: 1, message: "Attribute removed successfully", data: {…}}
{statuscode: xxxxx, message: "Error message", data: {…}}

Using Attributes

Attributes can be used to send targeted notification to a group of subscribers matching the attribute filter rule.

Attribute Filter Rules

Attribute filter rule is used to specify the criteria on attributes to filter subscribers. Attribute filter rule is composed of attribute rule which is an array of attribute rule items joined by AND operators. Attribute rule item is an object having property key , op and value.

//filter subscribers having age greater than 20 and role equal to editor
 [[  
      {"key": "age", "op":  "gt", "value": 20 }, 
      {"key": "role", "op":  "eq", "value": "editor"}
 ]]

Allowed Operators

Operator Name Operator Description Example
Equal 'eq' Matches values that are equal to a specified value. {"key": "name", "op": "eq", "value": "Jon Smith"}
Not Equal 'ne' Matches all values that are not equal to a specified value. {"key": "name", "op": "ne", "value": "smith jones"}
Greater Than 'gt' Matches values that are greater than a specified value. {"key": "age", "op": "gt", "value": 20}
Greater Than Equal 'gte' Matches values that are greater than or equal to a specified value. {"key": "Age", "op": "gte", "value": 20}
Less Than 'lt' Matches values that are less than a specified value. {"key": "dob", "op": "lt", "value": "2019-09-19T05:41:53.150Z"},
Less Than Equal 'lte' Matches values that are less than or equal to a specified value. {"key": "quantity", "op": "lte", "value": 500}
Exists 'exists' Matches all the subscriber that have the specified key. {"key": "name", "op": "exists"}
Not Exists 'not_exists' Matches documents that have not the specified field. {"key": "role", "op": "not_exists"}

Sending Notification using Attribute Filter Rules

Push notification with Attribute filter rule will create the notification which will be sent to only those subscribers who satisfied the rules. For this push notification the required parameters are -

  • notification_title
  • notification_message
  • notification_url
  • attributes

Request

curl -X POST -H "api_key: <your_pushengage_api_key>" -H "Content-Type: application/x-www-form-urlencoded" -d 'notification_title=this is title of notification&notification_message=this is message of notification&notification_url=http://www.example.com&notification_type=now&attributes=[[{"key": "age", "op": "gt", "value": 20}]]' "https://api.pushengage.com/apiv1/notifications"
<?php

$attribute_filter_rule=array(
array((object)array(
"key" => "age",
        "op" => "gt", 
        "value"=> 20
))
  );

  $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://api.pushengage.com/apiv1/notifications",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => "notification_title=this is title of notification&notification_message=this is message of notification&notification_url=http://www.example.com&notification_type=now&attributes=".json_encode($attribute_filter_rule),
      CURLOPT_HTTPHEADER => array(
        "api_key: <your_pushengage_api_key>",
        "cache-control: no-cache",
        "content-type: application/x-www-form-urlencoded"
      ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response;
    }

?>  

Response

{
  "success": true,
  "notification_id": 12345
}
{
  "success": false,
  "message": "API KEY Invalid"
}
{
  "message": "Error Message",
  "success": false
}
{
  "success": false,
  "message": "sending notification failed"
}