xAPIWrapper
Wrapper to simplify communication to an LRS. Read more about the Experience API Spec here.
Check out the Reference Documentation Here
xapiwrapper.js
Javascript Experience API wrapper.
This javascript file can be included to web based xAPI clients to
simplify the process of connecting and communicating to an LRS. It
is enclosed in an ADL object like the
ADL xAPI Verbs project, allowing
a single object to contain both the ADL verbs and the ADL xapiwrapper.
This wrapper has two version identifiers within the code. One, xapiVersion
is the version of the Experience API Specification for which it was built,
and can be used to determine if the wrapper is compatible with an LRS implementing a specific
xAPI Specification version. The second is the build date in the header of the minified file,
which can be used to tell if you're using the latest version.
Dependencies
The wrapper relies on external dependencies to perform some actions. Make sure you include
our compilation of the necessary CryptoJS components
in your pages if you're not using xapiwrapper.min.js.
<script type="text/javascript" src="./lib/cryptojs_v3.1.2.js"></script>
In the past we used the below libraries for the same purpose. You may continue to use them
for current systems, but the CryptoJS compilation is recommended.
For IE/Edge support you
will need to include a TextEncoder and TextDecoder shim if you're not using xapiwrapper.min.js
<script type="text/javascript" src="./lib/utf8-text-encoding.js"></script>
Installing
Using this wrapper could either be done by downloading the latest release or cloning the project.
Downloading the latest release version
The minified wrapper is self-contained. It includes all required dependencies
in addition to the ADL Verbs and the XAPIStatement module. For production sites,
this version of the wrapper is recommended.
Download the latest release
Follow the instructions for including the wrapper in your source files.
Cloning and building the project
You can optionally clone and use the dist/xapiwrapper.min.js:
git clone https://github.com/adlnet/xAPIWrapper/
Building the project
Compiling the minified version is easy. Install Node.js and NPM if you don't already have them
(download them here) or
$ sudo apt-get install nodejs
$ sudo apt-get install npm
$ sudo ln -s /usr/bin/nodejs /usr/bin/node
Then install the build system, Grunt. This
may require root/admin privileges on your system.
$ cd xAPIWrapper
$ sudo npm install -g grunt
Install the xAPIWrapper dependencies:
$ sudo npm install
Then execute the build script:
$ grunt
This will overwrite dist/xapiwrapper.min.js with the minifed versions of the wrapper and all its
dependencies.
Including in your Software.
Include the wrapper file, and optionally the dependencies.
<script type="text/javascript" src="./lib/cryptojs_v3.1.2.js"></script>
<script type="text/javascript" src="./lib/utf8-text-encoding.js"></script>
<script type="text/javascript" src="./src/activitytypes.js"></script>
<script type="text/javascript" src="./src/verbs.js"></script>
<script type="text/javascript" src="./src/xapiwrapper.js"></script>
<script type="text/javascript" src="./src/xapistatement.js"></script>
<script type="text/javascript" src="./src/xapi-util.js"></script>
<script type="text/javascript" src="./src/xapi-launch.js"></script>
Alternatively, use the minified version:
<script type="text/javascript" src="./dist/xapiwrapper.min.js"></script>
Configuration
The wrapper at a minimum needs to know the url of the LRS, though
most cases will also require the authorization information as well.
This wrapper provides two options for configuration. You may:
- Edit the configuration object (
Config) in the xapiwrapper.js file
var Config = function()
{
var conf = {};
conf['endpoint'] = "http://localhost:8000/xapi/";
try
{
conf['auth'] = "Basic " + toBase64('tom:1234');
}
catch (e)
{
log("Exception in Config trying to encode auth: " + e);
}
// Statement defaults [optional configuration]
// conf["actor"] = {"mbox":"default@example.com"};
// conf["registration"] = ruuid();
// conf["grouping"] = {"id":"ctxact:default/grouping"};
// conf["activity_platform"] = "default platform";
// Behavior defaults
// conf["strictCallbacks"] = false; // Strict error-first callbacks
return conf
}();
- Create your own configuration object and pass it to the xapiwrapper object
var conf = {
"endpoint" : "https://lrs.adlnet.gov/xapi/",
"auth" : "Basic " + toBase64('tom:1234'),
};
ADL.XAPIWrapper.changeConfig(conf);
Optionally, auth credentials can be updated by user and password properties on the
configuration object:
var conf = {
"endpoint" : "https://lrs.adlnet.gov/xapi/",
"user" : "tom",
"password" : "1234",
};
ADL.XAPIWrapper.changeConfig(conf);
or
var creds = {
"user" : "tom",
"password" : "1234",
};
ADL.XAPIWrapper.updateAuth(ADL.XAPIWrapper.lrs, creds.user, creds.password);
The script automatically runs, creating or adding to an ADL object an
instantiated xAPI Wrapper object. The object is created using the
configuration object inside the xapiwrapper.js file. If you modified this
object with your configuration, then xAPI Wrapper object is ready to use.
ADL.XAPIWrapper.testConfig();
>> true
Launch Parameters
The configuration will also look for url query parameters and use those
name - value pairs in the XAPIWrapper's internal configuration. That means
that http://localhost:8000/content/example.html?actor={"mbox":"mailto:tom@example.com"}
(not url encoded for illustrative purposes) would be parsed for an actor,
which would automatically be included in the wrapper configuration.
NOTE: endpoint, auth, actor, registration, activity_id, grouping, and activity_platform
are keywords that if found are used in send statement requests. See below for
usage examples.
Logging
The wrapper comes with a logging function (ADL.XAPIWrapper.log(message))
which attempts to write a message to console.log. This can be configured
to not write messages by setting log.debug = false;.
xAPI Launch support
The xAPI Wrapper supports ADL's xAPI Launch.
This allows configuration - agent info, lrs endpoint info - to be sent to the wrapper,
instead of using hard-coded configurations. See Using the xAPI-Launch library for
more details.
If you are using the src files, include xapi-launch.js.
<script type="text/javascript" src="./lib/cryptojs_v3.1.2.js"></script>
<script type="text/javascript" src="./lib/utf8-text-encoding.js"></script>
<script type="text/javascript" src="./src/activitytypes.js"></script>
<script type="text/javascript" src="./src/verbs.js"></script>
<script type="text/javascript" src="./src/xapiwrapper.js"></script>
<script type="text/javascript" src="./src/xapistatement.js"></script>
<script type="text/javascript" src="./src/xapi-util.js"></script>
<script type="text/javascript" src="./src/xapi-launch.js"></script>
Alternatively, use the minified xapiwrapper version, which includes xapi-launch:
<script type="text/javascript" src="./dist/xapiwrapper.min.js"></script>
To use, construct and ADL.launch object passing in a callback.
var wrapper;
ADL.launch(function(err, launchdata, xAPIWrapper) {
if (!err) {
wrapper = xAPIWrapper;
console.log("--- content launched via xAPI Launch ---\n", wrapper.lrs, "\n", launchdata);
} else {
wrapper = ADL.XAPIWrapper;
wrapper.changeConfig({
endpoint: "https://lrs.adlnet.gov/xapi/",
user: 'tom',
password: '1234'
});
console.log("--- content statically configured ---\n", wrapper.lrs);
}
$('#endpoint').text(wrapper.lrs.endpoint);
}, true);
Use
Statements
Statement Object (xapistatement.js)
new ADL.XAPIStatement(actor, verb, object)
new ADL.XAPIStatement.Agent(identifier, name)
new ADL.XAPIStatement.Group(identifier, members, name)
new ADL.XAPIStatement.Verb(id, description)
new ADL.XAPIStatement.Activity(id, name, description)
new ADL.XAPIStatement.StatementRef(uuid)
new ADL.XAPIStatement.SubStatement(actor, verb, object)
This sub-API makes it easier to author valid xAPI statements
by adding constructors and encouraging best practices. All objects in this
API are fully JSON-compatible, so anything expecting an xAPI statement can
take an improved statement and vice versa.
In addition to the above forms, each constructor can instead take as an argument
another instance of the object or the equivalent plain object. So you can convert
a plain xAPI statement to an improved one by calling new XAPIStatement(plain_obj).
Building a Simple "I Did This"
Passing in strings produces a default form: Agent Verb Activity.
var stmt = new ADL.XAPIStatement(
'mailto:steve.vergenz.ctr@adlnet.gov',
'http://adlnet.gov/expapi/verbs/launched',
'http://vwf.adlnet.gov/xapi/virtual_world_sandbox'
);
>> {
"actor": {
"objectType": "Agent",
"mbox": "mailto:steve.vergenz.ctr@adlnet.gov" },
"verb": {
"id": "http://adlnet.gov/expapi/verbs/launched" },
"object": {
"objectType": "Activity",
"id": "http://vwf.adlnet.gov/xapi/virtual_world_sandbox" }}
Adding Descriptors
var stmt = new ADL.XAPIStatement(
new ADL.XAPIStatement.Agent(ADL.XAPIWrapper.hash('mailto:steve.vergenz.ctr@adlnet.gov'), 'Steven Vergenz'),
new ADL.XAPIStatement.Verb('http://adlnet.gov/expapi/verbs/launched', 'launched'),
new ADL.XAPIStatement.Activity('http://vwf.adlnet.gov/xapi/virtual_world_sandbox', 'the Virtual World Sandbox')
);
>> {
"actor": {
"objectType": "Agent",
"name": "Steven Vergenz",
"mbox_sha1sum": "f9fa1a084b2b0825cabb802fcc1bb6024141eee2" },
"verb": {
"id": "http://adlnet.gov/expapi/verbs/launched",
"display": {
"en-US": "launched" }},
"object": {
"objectType": "Activity",
"id": "http://vwf.adlnet.gov/xapi/virtual_world_sandbox",
"definition": {
"name": {
"en-US": "the Virtual World Sandbox" }}}}
Adding Additional Fields
You can mix generated and manual fields without any conflicts.
var stmt = new ADL.XAPIStatement(
'mailto:steve.vergenz.ctr@adlnet.gov',
'http://adlnet.gov/expapi/verbs/launched',
'http://vwf.adlnet.gov/xapi/virtual_world_sandbox'
);
stmt.generateId();
stmt.result = { 'response': 'Everything is a-okay!' };
>> {
"actor": {
"objectType": "Agent",
"mbox": "mailto:steve.vergenz.ctr@adlnet.gov" },
"verb": {
"id": "http://adlnet.gov/expapi/verbs/launched" },
"object": {
"objectType": "Activity",
"id": "http://vwf.adlnet.gov/xapi/virtual_world_sandbox" },
"id": "d60ffbaa-52af-44b6-932d-c08865c540ff",
"result": {
"response": "Everything is a-okay!" }}
Setting the context attribute
Considering the behavior of xapiwrapper.js:
XAPIWrapper.prototype.prepareStatement = function(stmt)
{
...........
if (this.lrs.grouping ||
this.lrs.registration ||
this.lrs.activity_platform) {
if (!stmt.context) {
stmt.context = {};
}
}
if (this.lrs.grouping) {
if (!stmt.context.contextActivities) {
stmt.context.contextActivities = {};
}
if (!Array.isArray(stmt.context.contextActivities.grouping)) {
stmt.context.contextActivities.grouping = [{ id : this.lrs.grouping }];
} else {
stmt.context.contextActivities.grouping.splice(0, 0, { id : this.lrs.grouping });
}
}
...........
}
Here we notice 2 behaviours:
In order to set statement.context.contextActivities you must set grouping.
In order to be able to set the context property of a statement you have to set at least one of grouping, registration or activity_platform during ADL configuration like so:
const configuration = {
'endpoint' : LRS.apiUrl,
'auth' : 'Basic ' + btoa(LRS.key + ':' + LRS.secret),
'registration': this.ADL.ruuid(),
'activity_platform': `${this.organizationId}_organizationId`
};
this.ADL.XAPIWrapper.changeConfig(configuration);
As you can see we set only registration and activity_platform.
Using Multiple Languages
Any of the name or description fields in the constructors can instead take a language map,
as defined in the xAPI specification.