cancel
Showing results for 
Search instead for 
Did you mean: 
jonathan_puno
Community Member

URL structure for Pageviews using start_time & end_time

Jump to solution

Hi everyone,

I am trying to retrieve the pageviews for a student.  I can only retrieve 300 lines via the frontend and was advised that outside of that, I need to do API calls.  I consulted the PageViews API documentation here.  I am able to make a successful call using {{schoolURLbase}}/api/v1/users/{{userID}}/page_views and {{schoolURLbase}}/api/v1/users/{{userID}}/page_views?per_page=xxxxx.  With these calls, I am only to get a maximum of 10 lines and seemingly only from the previous day. 

I need help using the start_time and end_time parameters and possibly with paging as well.  Can someone help me what the structure of the URL should be to get lines/pageviews from a specific date range, please?

I know there are more than 10 pageviews because I can retrieve 300 lines from the Canvas front end.

Jonathan

1 Solution

Accepted Solutions
James
Navigator

 @jonathan_puno ,

The 10 items is almost always due to pagination within the API. You can add per_page=100 to the URL, but that's typically as high as you can go and that won't be enough in all cases. In the case of page views, you'll have to follow the Link headers because it contains a bookmark to the next page of views.

The List user page views endpoint is what you need. You knew that, but I'm documenting in case someone else comes along later. The start_time and end_time are DateTime types and the Welcome to the Canvas LMS API Documentation page explains that the dates need to be in ISO 8601 format using the UTC time zone. 

That format looks like: YYYY-MM-DDTHH:MM:SSZ where the T and Z are literals.

If you want 0:00 (12 am) on 7 August 2019 and are UTC+10, then you would need 2019-08-07T10:00:00Z for the timestamp.

These get combined into the query parameter, so you would end up with something like

/api/v1/users/123/page_views?per_page=100&start_time=2019-08-01T10:00:00Z&end_time=2019-08-07T10:00:00Z

Then you'll need to look at the link header for the one with rel="next" and call that URL to get any additional pages. The link header on this particular API call is broken in a way that doesn't really break anything but makes the URL unnecessarily long. The next link header includes the start_time and end_time duplicated. It still works, but when I was downloading these, I cleaned up the URL ... just because I'm that kind of person. When there ceases to be a next link header, then you know that you've received all the information.

View solution in original post

2 Replies
James
Navigator

 @jonathan_puno ,

The 10 items is almost always due to pagination within the API. You can add per_page=100 to the URL, but that's typically as high as you can go and that won't be enough in all cases. In the case of page views, you'll have to follow the Link headers because it contains a bookmark to the next page of views.

The List user page views endpoint is what you need. You knew that, but I'm documenting in case someone else comes along later. The start_time and end_time are DateTime types and the Welcome to the Canvas LMS API Documentation page explains that the dates need to be in ISO 8601 format using the UTC time zone. 

That format looks like: YYYY-MM-DDTHH:MM:SSZ where the T and Z are literals.

If you want 0:00 (12 am) on 7 August 2019 and are UTC+10, then you would need 2019-08-07T10:00:00Z for the timestamp.

These get combined into the query parameter, so you would end up with something like

/api/v1/users/123/page_views?per_page=100&start_time=2019-08-01T10:00:00Z&end_time=2019-08-07T10:00:00Z

Then you'll need to look at the link header for the one with rel="next" and call that URL to get any additional pages. The link header on this particular API call is broken in a way that doesn't really break anything but makes the URL unnecessarily long. The next link header includes the start_time and end_time duplicated. It still works, but when I was downloading these, I cleaned up the URL ... just because I'm that kind of person. When there ceases to be a next link header, then you know that you've received all the information.

View solution in original post

jonathan_puno
Community Member

Thank you,  @James .  Works perfectly.