-
Notifications
You must be signed in to change notification settings - Fork 2
/
h5p-ga.js
123 lines (106 loc) · 3.76 KB
/
h5p-ga.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'CHANGEME', 'auto');
ga('send', 'pageview');
(function () {
// Improve performance by mapping IDs
var subContentIdToLibraryMap = {};
/**
* Look through params to find library name.
*
* @private
* @param {number} id
* @param {object} params
*/
function findSubContentLibrary(id, params) {
for (var prop in params) {
if (!params.hasOwnProperty(prop)) {
continue;
}
if (prop === 'subContentId' && params[prop] === id) {
return params.library; // Found it
}
else if (typeof params[prop] === 'object') {
// Look in next level
var result = findSubContentLibrary(id, params[prop]);
if (result) {
return result;
}
}
}
}
if (window.H5P) {
H5P.jQuery(window).on('ready', function () {
H5P.externalDispatcher.on('xAPI', function (event) {
try {
if (!window.H5P) {
return;
}
// First we need to find the category.
var category;
// Determine content IDs
var contentId = event.data.statement.object.definition.extensions['http://h5p.org/x-api/h5p-local-content-id'];
var subContentId = event.data.statement.object.definition.extensions['http://h5p.org/x-api/h5p-subContentId'];
if (subContentId) {
if (subContentIdToLibraryMap[subContentId]) {
// Fetch from cache
category = subContentIdToLibraryMap[subContentId];
}
else {
// Find
category = findSubContentLibrary(subContentId, JSON.parse(H5PIntegration.contents['cid-' + contentId].jsonContent));
if (!category) {
return; // No need to continue
// TODO: Remember that it wasnt found?
}
// Remember for next time
subContentIdToLibraryMap[subContentId] = category;
}
}
else {
// Use main content library
category = H5PIntegration.contents['cid-' + contentId].library;
}
// Strip version number
category = category.split(' ', 2)[0];
// Next we need to determine the action.
var action = event.getVerb();
// Now we need to find an unique label
var label = event.data.statement.object.definition.name['en-US']; // Title
// Add contentID to make it eaiser to find
label += ' (' + contentId;
if (subContentId) {
label += ' ' + subContentId;
}
label += ')';
// Find value
var value;
// Use result if possible
var result = event.data.statement.result;
if (result) {
// Calculate percentage
value = result.score.raw / ((result.score.max - result.score.min) / 100);
}
// ... or slide number
if (action === 'Progressed') {
var progress = event.data.statement.object.definition.extensions['http://id.tincanapi.com/extension/ending-point'];
if (progress) {
value = progress;
}
}
// Validate number
value = Number(value);
if (isNaN(value)) {
value = undefined;
}
ga('send', 'event', category, action, label, value);
}
catch (err) {
// No error handling
}
});
});
}
})();