This is a lower-level API. Lower-level APIs are not recommended for most projects, and may not be available on all plans. See our page on recommended deployment patterns for more information.

Public Methods

This article details public methods the client can use to interact with the Events API.

Public Methods example

<html>

    <head>
    </head>

    <body>

    <button id="onButton">Add event handler</button>
    <button id="publishButton">Publish events</button>
    <button id="resetButton">Reset Events API</button>

    <script src="//events.learnosity.com"></script>
    <script>
description Initialises the Events API.
arguments initOptions (Object), eventOptions (Object)
returns Object, containing Events API Public Methods - all other public methods are called against this object. (See example).

init example

    var initOptions = {
        /*  See the Initialisation Options
            section for guidance on the initialisation object.
        */
    };

    var eventOptions = {
        readyListener: function () {
            console.log("Learnosity Events API is ready");
        },
        errorListener: function (e) {
            console.log('error', e);
        }
    };

    var eventsApp = LearnosityEvents.init(initOptions, eventOptions);
description

Register additional users, after initialisation, to subscribe or publish to.

arguments userHashes (Object). Object similar to the config.users initialisation option.

returns -

addUsers example

    document.getElementById('addUsers').onclick = function () {

        var user_hashes = {
          '5fb6d972-157b-4e25-b449-85e58089e714': '9bcffb8898d59fc3f3e425faa1041bf6b8f8950997cbb9cc37d576368be5d341'
        };

        eventsApp.addUsers(user_hashes);
    };
description

Set event handler callback for certain events

callback is called with an array of events.

arguments

events (Array). An array of objects describing the events to subscribe to. Required properties for each event object are user_id and kind. Optionally, activity_id can be passed to filter events by activity ID.

callback (Function)

returns -

on example

    document.getElementById('onButton').onclick = function () {

        var eventHandler = function (events) {
            console.log(events);

            /* Example:
            [
                {
                    "id": "ac478bc1-2e0e-44c1-a19c-6cb065f06bad",
                    "timestamp": "2014-10-08T00:51:28.808Z",
                    "actor": {
                        "account": {
                            "homePage": "yis0TYCu7U9V4o7M",
                            "name": "jessepinkman"
                        },
                        "objectType": "Agent"
                    },
                    "verb": {
                        "id": "http://adlnet.gov/expapi/verbs/progressed",
                        "display": {
                            "en-US": "progressed"
                        }
                    },
                    "object": {
                        "id": "https://xapi.learnosity.com/activities/org/1/pool/null/activity/demoactivity",
                        "definition": {
                            "extensions": {
                                "data": 1
                            }
                        },
                        "objectType": "Activity"
                    }
                }
            ]
            */
        };

        eventsApp.on(events, eventHandler);
    };
description

Publish raw or valid XAPI compliant events through Events App.

arguments

object (Object). Parameters are xapi - whether the objects are XAPI-compliant, and events, an array of event objects to publish. Every event must have a kind property.

returns

-

publish example

    document.getElementById('publishButton').onclick = function () {

            eventsApp.publish({
                "xapi": false,  // whether it's a full(or raw) xAPI events array, true | false (default)
                "events": [{
                    "kind": "assess_logging",
                    "actor": "jessepinkman",  // string, user_id
                    "verb": "progressed", // string, verb
                    "object": {  // string (unique "id") or object (full object)
                        "id": "https://xapi.learnosity.com/activities/org/1/pool/null/activity/demoactivity",
                        "definition": {
                            "extensions": {
                                "data": 1
                            }
                        }
                    }
                }]
            })

    };
description

Reset the Events App - stop subscribing to events, return any unpublished events.

arguments

-

returns

events (Array). An array of events that had been passed to publish() but not yet actually published, due to batching or errors requiring retry.

reset example

    document.getElementById('resetButton').onclick = function () {
            eventsApp.reset();
    };
    </script>
    </body>
</html>