H5P Guides

Working with xAPI statements

An H5P library uses xAPI to tell the world what the user is doing in real time. As a library developer you might be interested in listening in on what the user does in child libraries, or generate your own xAPI statements.

Listen for xAPI statements

When listening for xAPI statements you have three options. If you know which library you want to listen to you add a listener on that library like this:

libraryInstance.on('xAPI', function(event) {
  // Handle the event here, the statement is within event.data.statement
});

 The event will be of the type XAPIEvent.

The XAPIEvents bubble up the parent tree by default so it might pass through the library you're working on and you may replace libraryInstance with this to listen for it.

Also XAPIEvent goes to the H5P.externalDispatcher by default so you may listen for them there by replacing libraryInstance with H5P.externalDispatcher like this:

H5P.externalDispatcher.on('xAPI', function(event) {
  // Handle the event here, the statement is within event.data.statement
});

Generating statements

For your library to tell the world what the user does you need to generate xAPI statements. If you have inherited the Event Dispatcher you have also inherited the functions you need to send xAPI statements. Learn about inheriting and using the EventDispatcher in the EventDispatcher guide.

This is all you need to tell the world that a user answered a question and got 3 out of 5 points:

this.triggerXAPIScored(3, 4, 'completed');

The above code will generate a complete xAPI statement where information about the object, the actor, the context and more is automatically filled in.

Other xAPI releated functions provided by the EventDispatcher is documented in the API specification.