User behavior tracker
Main git repository with source code: https://gitlab.com/eduplex-api/user-behavior-tracker
Scope of this repository
The purpose of this project is to provide an alternative to google analytics using javascript to store information about pages and user interactions in the form of xAPI statements. Those xAPI statements can be stored in a xAPI compatible LRS like, for example, Learning Locker.
Required files
JsTracking.js
is the main file from this project , which exports all the functions needed to make the tracking work.verbs.js
is just a collection of verbs used in the xAPI statements to describe user actions.xapiwrapper.min.js
is a script injected automatically in the page head byJsTracking.js
. This is needed to create thewindow.ADL
object, which provide all the functions needed to store xAPI data in the LRS.
Configuration
You will need to set several configuration values in order to communicate with the LRS where the tracking data will be stored:
We have implemented (in the same backend repository as the Module Occupation Matching) a proxy to forward xAPI requests from our backend to the LRS in order to keep credentials safe.
userId
: logged-in user ID.userToken
: logged-in user access token
How does it work
When you add this project as a package into your application you can access 2 modules:
jsTrackingFunctions
which contains all the functions you will need to call to track in your platform. Those functions are:initXapiTrack
- This function initializes the LRS and other configurations needed to start tracking. It is an asynchronous function, and it is needed to be called before any other tracking function. It receives one parameter, an object like the one in the section above with the lrs configuration.sendXapiStatement
- This one triggers a tracking event, which is stored in the LRS as a statement. It receives a parameter with the name of the event, for example:viewed
,progressed
,responded
,log in
...
verbsList
- This is a group of constants for the available verbs. For example if you want to trigger aviewed
event you can use the constantverbsList.viewed.key
to access the name of the event.
Some verbs have a callback function to process some extra data, for example when tracking a search
event you can include in the xAPI data what the user was looking for and even filters applied to the search. This callback function is optional but each verb will expect different objects in this function.
Before sending any tracking event the identifier of the logged-in user must be stored in window.JsTrackingUserLogged
.
Every statement saved in the LRS includes data from the current page. Also, if the page has structured data with https://schema.org/Course format in a application/ld+json
script all this data is stored inside the statement.
How to extend and customize statements
To modify statements is as simple as editing the file verbs.js.
How to create new verbs or modify existing ones
Verbs have 3 basic properties:
id
: A unique identifier of the verb (it needs to be a URL).key
: Internal name used when you call thesendXapiStatement
function.display
: The name displayed on the LRS.
With this in mind you can modify properties of the existing verbs on the file or just add a new one.
First it is recommended to check if there is already a verb on the xAPI registry that fits your case. If you need or want to create your own custom verb you can do it but remember to use always a URL as the id
. We always use the same URLs under our own domain https://proto.eduplex.eu, but you can use your own.
To create a new verb just add one JSON object like this on the verbs file and change it as you like:
Once you have added the new verb you can use it on the statements you are sending like this:
How to extend a statement
Besides the 3 basic properties of the verb we implemented a new one called customOptions
.
It is a function that allows to customize the object
of the statement or adding more data to the extensions
section inside context
.
Params:
You can send any parameter when you do the call of the tracking function
sendXapiStatement
to pass the relevant data for that event.
Return:
This function is expected to return an object with the following properties:
object
: This will overwrite the object of the statement.extensions
: This will not overwrite but add them to the existing ones.
They are not mandatory but if you do not need to change any of them you should not include this function and keep the verb simple with the basic properties. Some of the already defined verbs use it. For example, the searched
verb, with its own objectType
and one additional extension
:
In the section below you can find an example of how to call the track function with custom options.
Example
In this example we are initializing the tracking and sending a viewed
event. This is storing an xAPI statement in the LRS for the user tracking-test-user
with the data from the current page.
Also, an example of an event with custom options like the search
mentioned before:
Where searchQuery
is a string and filters
is an object.
Example statement
Other considerations
In projects with webpack and babel we found a recurring error with optional chaining operator (?.
), it shows something like this in build time: Module parse failed: Unexpected token You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.
It can be fixed by adding this plugin to your webpack/babel config:
'@babel/plugin-proposal-optional-chaining'
Also make sure webpack transpile our code, in a nuxt project this is as simple as adding the next line inside the build
property in nuxt.config.js
.
transpile: ['user-behavior-tracker']
License
The source code for the site is licensed under the MIT license, which you can find in the LICENSE file.
xapiwrapper.min.js
file has been taken from another open source repository (https://github.com/adlnet/xAPIWrapper) which is under Apache License
Last updated