2

Klipfolio Datasource : Zendesk API Pagination

Klipfolio Datasource : Zendesk Pagination

Disclaimer:Zendesk API response can have large number of pages, which can create large number of datasource instances, it can cause performance issues on your dashboards. you can limit the number of datasource instances by limiting the number of results the query brings back. Zendesk limits the number of results per query to 100. If you have more than 100 results you need to use their pagination mechanism to get the next page. You can use the Zendesk incremental ticket export. Explained here. You can also limit the data by filtering it using date parameters and getting the data for certain period of time.

They have an optional parameter called “page” which you can specify in the query. More details are given in the documentation here.

For example to pull the number of users on second page you can use following query.

https://subdomain.zendesk.com/api/v2/users.json?page=2

Klipfolio has a concept called dynamic data sources which allows you to make the page number as a dynamic property instead of static value and using the formulas you can pull all the available data from multiple pages.

Each value of the dynamic property will create a dynamic datasource instance in the background. You can use instructions about troubleshooting dynamic data sources and remove stale/bad/unwanted instances.

For example, say you create called pageNum in the Klipfolio Klip editor, assign a default value =1 to the same. Instructions to create variable are linked here.

You can use the same variable in the above query as shown below.

https://subdomain.zendesk.com/api/v2/users.json?page={props.pageNum}

Now , you need to configure your formulas to loop through the number of available pages and get all of the records.

Following query gives me the list of tickets solved in the last month. Please note that I am using {props.pageNum} as dynamic property. Also {date.addMonths(-1).format()} as dynamic date. You can use also date parameters in data source queries.

https://<your_subdomain>.zendesk.com/api/v2/search.json?page={props.page}&query=type:ticket+solved>{date.addMonths(-1).format()}

To select all the ticket ids returned by the above datasoure, you can use MAP function as shown below.

New Information : Now instead of going all this trouble of joining all the results together and then making an array you can use MAPFLAT function, thanks to Klipfolio. So formula is much simpler now. 

MAPFLAT(CUMULATIVE ( REPEAT ( 1, ( CEILING ( count/100)))) ,“pagenum”, (id))))

--------

Let’s break it down to understand.  In this formula CUMULATIVE ( REPEAT ( 1, ( CEILING ( count/100)))) will generate an array of (1,2,3..)

Here count is number of total items in the result of the query.

For example if count = 234 the CUMULATIVE function will return 1,2,3 which are the number of pages we have for the result.

MAP function will act as a loop, where first parameter is a list of values to loop though; in this case it is array of offset values.  In this loop, for each iteration, one of the values from this array will be assigned to a variable called “pagenum” specified as a second parameter of the MAP function. You will manually enter the name of the variable as a string there.

Remember while creating the datasource you have used it as page=: {props.pagenum} ? Every time the pagenum variable value is changed a new instance of this datasource will be created.

MAP (CUMULATIVE ( REPEAT ( 1, ( CEILING ( count/100)))) ,“pagenum”, JOIN(id) )

Third part here JOIN (id) will give you joined list of ticket ids separated by comma for a single instance of the dynamic datasource. So for single page here you will get a comma separated ticket ids from that page.

You can use JOIN to join the Ids from one instance in a single value separated by comma, as MAP will return one value for each input value of the variable

Now in this example you will have 3 sets of joined values for each datasource instance, you can JOIN all of them together and convert them to an array to get the list of 234 ids in this example.

ARRAY( JOIN(MAP(CUMULATIVE ( REPEAT ( 1, ( CEILING ( count/100)))) ,“pagenum”, JOIN(id))))

Happy Dashboarding!

 

6 comments

  • Avatar
    Janice Janczyn Official comment

    Hi Chris, 

    Since Zendesk returns a maximum of 100 records/page, to retrieve 7,000 records would take 70 Zendesk API queries. Each query takes time (varies), which must fit within the 40-second formula timeout window. Whenever possible, we recommend that you limit the data returned by setting timeframe parameters in your datasource queries.

    That said, Zendesk has a Side-Loading endpoint which may be useful in your scenario and is worth investigating: https://developer.zendesk.com/rest_api/docs/core/side_loading.

    Thanks, 

             Janice

  • 0
    Avatar
    Chris Marquer

    Hi,

    Say I have approx 7,000 tickets in zendesk and I need to pull information from that pool. Will it significantly slow down the klip or dashboard? At what point will you start to see performance declining?

    Thanks!

  • 0
    Avatar
    Alex Clausen

    I trying to communicate with my zendesk using MAPFLAT and "https://subdomain.zendesk.com/api/v2/users.json?page={props.pageNum}"

    My json call to zendesk has 1000+ results. But it seems that instead of getting records from
    "https://subdomain.zendesk.com/api/v2/users.json?page=1"
    "https://subdomain.zendesk.com/api/v2/users.json?page=2"
    "https://subdomain.zendesk.com/api/v2/users.json?page=3"
    "https://subdomain.zendesk.com/api/v2/users.json?page=4"
    and so on....

    I just get the same 100 results from the first page "https://subdomain.zendesk.com/api/v2/users.json?page=1" again and again.

    It's like my variable isn't increased by +1 for each sequential call


    Any idea why this is?

     

  • 2
    Avatar
    Janice Janczyn

    Hi Alex,

    The first parameter in your MAPFLAT is your list of page numbers. Calculate the number of pages needed using the count value returned by your first query and the per_page parameter in your query and REPEAT the value 1 for the number of pages, then wrap the CUMULATIVE function to get the cumulative count.

    For example, if count = 234:

       ROUND( count/100 ) + 1 returns 3
       REPEAT( 1, ROUND( count/100 ) + 1 ) returns 1, 1, 1
      CUMULATIVE( REPEAT( 1, ROUND( count/100 ) + 1) ) returns 1, 2, 3

    For details, please refer to Datasources with Multiple Pages.

    Happy Dashboarding!

                              Janice

  • 0
    Avatar
    Alexander Mette

    Hi Janice, I have the same problem as Alex, so how can one get the 500 records in 5 pages (100 for each)?

  • 0
    Avatar
    Janice Janczyn

    Hi Alexander, 

    If you want to retrieve only 5 pages, the first parameter in your MAPFLAT would be an ARRAY of 1 - 5, so either of the following formulas would work:

       ARRAY(1,2,3,4,5)   
      CUMULATIVE ( REPEAT ( 1, 5 ) )

    If you want to calculate the number of pages to return based on the number of records and can get a count of the records from the data, for example, if there is a 'count' field, you can calculate the number of pages as follows and use this as the first parameter in your MAPFLAT.

       CUMULATIVE( REPEAT( 1, CEILING( count / 100 ) + 1) ) 

     

    Thanks,
            Janice

Please sign in to leave a comment.