Canvas Python API wrapper list user page views in useable format

Jump to solution
WillGHampton
Community Participant

Greetings!

I am working with the Canvas API python wrapper trying to check a particular student's access at a particular point in time to determine the IP used to access a test.  I can create the connection, create the correct object and pull a list of page views. I can iterate the list of page views, which returns an object of type canvasapi.course.PageView.

My code, minus key, instance declaration and user_id value, is below:

>>> from datetime import date, time, datetime

>>> from canvasapi import Canvas

>>> canvas = Canvas(API_URL, API_KEY)

>>> user = Canvas.get_user(user_id)

>>> startdate = datetime(year=2024, month=4, day=22, hour=19, minute=0, second=0)
>>> enddate = datetime(year=2024, month=4, day=22, hour=22, minute=0, second=0)

\\thank the documentation for the below, would not have figured the below out in a million years

>>> pageviews = user.get_page_views(user={'start_time':startdate,'end_time':enddate})

>>> for pageview in pageviews:
... print(pageview)

>>> for pageview in pageviews:
... print(pageview)
...
None (b3740b44-0fde-4367-b03e-7f0a543ef6e8)
None (b06f47e4-514c-45fd-8318-7234e32a413b)

..etc,etc,etc

 

Iterating over the list of pageview object and printing does not give useful information. I believe I need the methods for the object to print the values in the object. How can I print out the values of pageview objects?

Searching the canvasapi documentation for pageview does not give more information on this object. Visiting the courses page does not yield information on this object type. Where can the method information be gained? How would I know to look there?

I feel like there is something that may seem obvious to others that I am completely missing that is preventing me from making progress. Can someone help shed light on what I am missing?

 

In summary, what are the methods of the object canvasapi.course.pageview that will allow me to print timestamps, IP and URL for that object in a paginated list? Once I can print the values to screen, I can get them to file in whatever format I need.

Thank you

 

0 Likes
1 Solution
stimme
Community Coach
Community Coach

@WillGHampton We also use Python and canvasapi to retrieve detailed pageview data when needed. Within the for loop, you could try this to get the values you're after plus a couple that may be worth having.

print(
pageview.id,
pageview.url,
pageview.created_at,
pageview.remote_ip,
pageview.links["real_user"]
)

 To print even more of the PageView data:

print(
pageview.id,
pageview.app_name,
pageview.url,
pageview.context_type,
pageview.asset_type,
pageview.controller,
pageview.action,
pageview.interaction_seconds,
pageview.created_at,
pageview.user_request,
pageview.render_time,
pageview.user_agent,
pageview.participated,
pageview.http_method,
pageview.remote_ip,
pageview.links["user"],
pageview.links["context"],
pageview.links["asset"],
pageview.links["real_user"],
pageview.links["account"]
)

 

View solution in original post