setUserAttributes can now update counts and occurrences for Events and States

Our API method setUserAttributes now supports incrementing Event and State occurrences for that user, as well as setting the time of the first and last occurrence. Before, you could only set the total lifetime occurrences.

New parameters

To make this possible, we've added several new parameters in the API request:

Parameter Type Description
events object A map of event data to update for the current user.
events.eventName object A map of properties to update for eventName.
events.eventName.count integer New lifetime count of eventName to update for the current user.
events.eventName.countIncrement integer Amount to increment the lifetime count of eventName.
events.eventName.value float New lifetime value of eventName to update for the current user.
events.eventName.valueIncrement integer Amount to increment the lifetime value of eventName.
events.eventName.firstTime integer Time that the event first occurred, in Unix time
events.eventName.lastTime integer Time that the event last occurred, in Unix time.
states object A map of state data to update for the current user.
states.stateName object A map of properties to update for stateName.
states.stateName.count integer New lifetime count of stateName to update for the current user.
states.stateName.countIncrement integer Amount to increment the lifetime count of stateName.
states.stateName.firstTime integer Time that the state first occurred, in Unix time.
states.stateName.lastTime integer Time that the state last occurred, in Unix time.

Example

You can increment the occurrences for an event by sent a POST request.

https://www.leanplum.com/api?apiVersion={{api_version}}&appId={{app_id}}&clientKey={{prod}}&userId={{user_id}}&action=setUserAttributes
{
   "events": {
      "yourEventName": {
         "countIncrement": 1
       }
   }
}

If it is successful, it will return:

{
  "response": [
    {
      "success": true
    }
  ]
}

Verifying the changes

You can test whether or not the changes were made as expected by calling exportUser to see the current values for each of the Events and States first, then calling it again after making the setUserAttributes call. For example, this response shows the count for yourEventName is now 8, after being incremented 1 in the call above from 7.

https://www.leanplum.com/api?appId={{app_id}}&clientKey={{data_export_key}}&apiVersion=1.0.6&action=exportUser&userId={{user_id}}
{
  "response": [
    {
      "totalSessions": 11,
      "created": 1486405831.293,
      "events": {
        "yourEventName": {
          "firstTime": 1486417362.171,
          "count": 8,
          "lastTime": 1488214158.118
        },
        "anotherEventName": {
          "firstTime": 1486674763.178,
          "count": 1,
          "lastTime": 1486674763.178
        }
      },
      "lastActive": 1488216153.901,
      "states": {
        "aCoolStateName": {
          "firstTime": 1486674534.904,
          "count": 5,
          "lastTime": 1486676492.79
        }
      },
      "success": true,
      "userAttributes": {
        "lastName": "Smith",
        "email": "bob.smith@example.com",
        "firstName": "Bob"
      }
    }
  ]
}

 


Was this article helpful?
Have more questions? Submit a request