The stats
plugin records entries with the Eko stats server, allowing your project to query and display worldwide statistics on user choice history, average scores, etc.
This plugin is not loaded by default, so you’ll have to explicitly load it using the onIntroReady intro.js hook.
In order to make your project compatible for download and offline viewing, you should use the external resources JSON instead of hard-coded URLs.
Example
// playerOptions.json
{
"plugins": {
"stats": {
"projectId": "zDY36B"
}
}
}
// intro.js
import loadjs from 'loadjs';
export default {
onIntroReady: function(player) {
// load stats plugin
loadjs('//eko.com/resources/js/plugins/stats/stats.min.gz.js', { returnPromise: true })
.then(() => {
player.initPlugin('stats');
});
}
};
// In your project's code
// Once we've calculated this user's score:
player.stats.put('score', player.variables.getValue('score'));
// When we need to display the most frequent score:
player.stats.getDistribution('score').then(function(distribution) {
// .....
})
// ....
// Returns a promise that resolves with the number of users that scored x
function howManyUsersScoredX(x) {
return player.stats.get('score')
.then(function(stats) {
return stats.entryCounts[x];
});
}
- Table of Contents
- Init Options
- Properties
- versionpro
- Methods
Init Options
options : object
Initialization options for the stats plugin.
options.projectId : string
The short ID of the project.
Stats will be aggregated under this identifier.
Default: "default"
Example
// playerOptions.json
{
"plugins": {
"stats": {
"projectId": "zDY36B"
}
}
}
Properties
player.stats.version : string
The stats plugin’s version string.
Example
console.log('The stats plugin version is', player.stats.version);
Methods
player.stats.getDistribution(key) ⇒ Promise
getDistribution()
returns a promise that resolves with a StatsReport object for this key.
The distribution can be used to determine leaders, averages, etc.
Returns: Promise
- A promise that resolves with a StatsReport object
Param | Type | Description |
---|---|---|
key | string |
The key that these stats have been logged for (e.g. using put('score') in other sessions, the key would be ‘score’) |
player.stats.put(key, value) ⇒ Promise
put()
asynchronously records an entry value with the stats server under key key
.
Returns: Promise
- A promise that resolves with the success of the put operation
Param | Type | Description |
---|---|---|
key | string |
The key to assign the value to, e.g. finalScore or decision1Result . |
value | number | string |
The value to store for the key for this entry |
Callbacks
StatsReport : object
An object containing information about all the entries put for a particular key in a particular scope.
- StatsReport :
object
- .totalEntries :
number
- .entryCounts :
object
- .totalEntries :
statsReport.totalEntries : number
The total number of entries recorded for this key in this scope.
statsReport.entryCounts : object
An object mapping a value to the number of times an entry was logged for that value.
For example, if the value ‘node1’ was entered 5 times, the object would contain the key-value pair { node1: 5 }.
Example
console.log(distribution.entryCounts);
// {
// 19: 200,
// 20: 899,
// 21: 1001,
// 22: 100,
// 23: 26,
// 24: 20,
// 25: 77
// }
// Where 200 people had a score of 19, 899 people had a score of 20, etc...