-
Notifications
You must be signed in to change notification settings - Fork 0
/
analytics-frame.html
125 lines (116 loc) · 3.85 KB
/
analytics-frame.html
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
124
125
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="referrer" content="origin" />
<script>
/* jshint ignore:start */
// jscs:disable
// https://gist.github.com/982883
var uuid = function(a) {
return a // if the placeholder was passed, return
? ( // a random number from 0 to 15
a ^ // unless b is 8,
Math.random() // in which case
* 16 // a random number from
>> a/4 // 8 to 11
).toString(16) // in hexadecimal
: ( // or otherwise a concatenated string:
[1e7] + // 10000000 +
-1e3 + // -1000 +
-4e3 + // -4000 +
-8e3 + // -80000000 +
-1e11 // -100000000000,
).replace( // replacing
/[018]/g, // zeroes, ones, and eights with
uuid // random hex digits
);
};
window.frameHash = window.location.hash.replace(/^#/, '');
var postToParent = function(message) {
if (window.parent.postMessage != null) {
window.parent.postMessage(message, '*');
}
};
var onDidReceiveMessage = function(e) {
if (e.data.id === frameHash) {
var xhr = new XMLHttpRequest(),
now = new Date(),
data = {
messageId: uuid(),
sentAt: now.toISOString(),
batch: [ {
event: e.data.event,
properties: e.data.properties,
type: 'track',
messageId: uuid(),
timestamp: now.toISOString(),
context: {
ip: '0.0.0.0',
page: {
path: '/',
referrer: '',
search: '',
title: '',
url: 'http://'
}
},
integrations: {},
userId: e.data.aid
} ]
};
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
postToParent({
id: frameHash,
message: 'xhr sent'
});
window.onDidReceiveMessage = null;
}
};
xhr.open('POST', 'https://metrics.articulate.com/v1/import');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(data));
}
};
if ('onmessage' in window) {
window.addEventListener('message', onDidReceiveMessage, false);
} else {
// coming from flash, we will use older JS since we can expect some older IE versions
var validProps = [
'os', 'browser', 'playerVersion', 'playerType', 'lmsPresent', 'tinCanPresent',
'aoSupport', 'publishSource', 'protocol', 'productChannel', 'cid'
];
var aid,
props = window.location.search.replace(/^\?/, '').split('&'),
config = {};
for (var i = 0, ii = props.length, currProp; i < ii; i++) {
currProp = props[i].split('=');
if (validProps.indexOf(currProp[0]) > -1 && currProp.length === 2) {
config[currProp[0]] = currProp[1];
} else if (currProp[0] === 'aid') {
aid = currProp[1];
}
}
window.onDidReceiveMessage({
data: {
id: frameHash,
event: 'player_course_load',
properties: config,
aid: aid
}
})
}
/* jshint ignore:end */
// jscs:enable
</script>
</head>
<body>
</body>
<script>
postToParent({
id: window.frameHash,
message: 'loaded'
});
</script>
</html>