You can use the Events API in Zendesk apps to retrieve events associated with users. The Events API accepts a Zendesk user id in GET requests to identify the user:

GET /api/v2/users/{user_id}/events

In the Zendesk Apps framework, you can get a Zendesk user id from the User object. The User object is available in all app locations in Support. The User object can also be a property of other objects in the framework. For example, the ticket.requester property of the Ticket object is a User object.

This article provides a sample function for a Zendesk app that gets events associated with the ticket requester. The function uses event filters to limit the events to only those that occurred in the 7 days before the ticket was created.

Disclaimer: Zendesk provides this article for instructional purposes only. Zendesk does not support or guarantee the code. Zendesk also can't provide support for third-party technologies such as JavaScript.

Example

Details

  • App location in Support: Ticket sidebar

  • Zendesk user id: The ticket requester

  • Filter events by start time: 7 days before the ticket was created

  • Filter events by end time: When the ticket was created

JavaScript function for app

(function(){
  let user_id;  let start_time;  let end_time;
  // create ZAF client  const client = ZAFClient.init();
  // get the Zendesk user id of the ticket's requester  client.get('ticket.requester.id')  .then(function (data) {    user_id = data['ticket.requester.id'];    return;  })
  // set event filters based on when the ticket was created  .then(client.get('ticket.createdAt')    .then(function (data) {      end_time = new Date(data['ticket.createdAt']);      start_time = new Date(end_time);      start_time.setDate(end_time.getDate() - 7);      start_time = start_time.toISOString();      end_time = end_time.toISOString();      return;    })  )
  // make the API request  .then(function () {    const settings = {      type: 'GET',      url: '/api/v2/users/' + user_id + '/events',      data: 'filter[start_time]=' + start_time + '&filter[end_time]=' + end_time    };    client.request(settings)    .then(function (data) {      for (let event of data['events']) {        console.log(event);      }    })  })
  .catch(function () {    console.error('Error');  });
})();

Response

The function displays the events in the browser console:

How it works

Because framework requests are asynchronous and some requests must follow others, the function consists of a promise chain.

Preliminaries

The function starts by declaring some variables at the function level so that they're available in nested functions.

let user_id;  let start_time;  let end_time;

It then creates a ZAF client to work with the framework APIs:

const client = ZAFClient.init();

Get the Zendesk user id

Next, the function gets the Zendesk user id of the person requesting support in the ticket:

client.get('ticket.requester.id')  .then(function (data) {    user_id = data['ticket.requester.id'];    return;  })

The function uses the client.get(path) framework method to call the framework API. See Working with framework APIs.

Set up the filters

Next, the function gets the ticket's createdAt date to define the values of the start_time and end_time variables. The variables will be used in the Events API request to limit the events to only those that occured in the 7 days before the ticket was created.

.then(client.get('ticket.createdAt')    .then(function (data) {      end_time = new Date(data['ticket.createdAt']);      start_time = new Date(end_time);      start_time.setDate(end_time.getDate() - 7);      start_time = start_time.toISOString();      end_time = end_time.toISOString();      return;    })  )

Get the events

Finally, the function makes a request to the Events API and displays the events in the browser console. The function uses the client.request() framework method to call the API. See Making API requests from a Zendesk app.

.then(function () {    const settings = {      type: 'GET',      url: '/api/v2/users/' + user_id + '/events',      data: 'filter[start_time]=' + start_time + '&filter[end_time]=' + end_time    };    client.request(settings)    .then(function (data) {      for (let event of data['events']) {        console.log(event);      }    })  })

The request includes the following path parameters to filter the events:

'filter[start_time]=' + start_time + '&filter[end_time]=' + end_time

See Filtering events.