Created
August 29, 2020 07:40
-
-
Save rubysoho07/691dd891c03bb498b74f5fbd720567cb to your computer and use it in GitHub Desktop.
Example of creating and sending Caliper Analytics data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pprint | |
from datetime import datetime | |
import caliper | |
from caliper import events, entities | |
from caliper.constants import CALIPER_ACTIONS | |
sensor_config = caliper.HttpOptions( | |
host='(host)', | |
auth_scheme='Bearer', | |
api_key='(api_key provided)' | |
) | |
sensor = caliper.build_sensor_from_config( | |
sensor_id='https://caliper.imsglobal.org/testserver', | |
config_options=sensor_config | |
) | |
event = events.SessionEvent( | |
actor=entities.Person(id='https://example.com/user/123'), | |
action=CALIPER_ACTIONS['LOGGED_IN'], | |
object=entities.SoftwareApplication(id='https://example.com/application/1'), | |
eventTime=datetime.utcnow().isoformat(timespec='milliseconds') + 'Z' | |
) | |
actor = entities.Person( | |
# Required Property | |
id='https://example.com/user/123', | |
# Optional Property | |
name='Yungon Park' | |
) | |
video_object = entities.VideoObject( | |
# Required Property | |
id='https://example.com/video/1234', | |
# Optional Property | |
name='Introduction of Caliper Analytics', | |
duration='PT10M15S' # ISO 8601 duration format | |
) | |
media_event = events.MediaEvent( | |
actor=actor, | |
action=CALIPER_ACTIONS['STARTED'], | |
object=video_object, | |
eventTime=datetime.utcnow().isoformat(timespec='milliseconds') + 'Z' | |
) | |
pprint.pprint(media_event.as_dict(thin_context=True, thin_props=True)) | |
sensor.send(event) | |
sensor.send(media_event) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment