forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtickevent.html
46 lines (43 loc) · 1.69 KB
/
tickevent.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
<script type="text/javascript">
if (!window.pxtTickEvent) {
let queuedTicks;
window.pxtTickEvent = function(id, data) {
if (window.appInsights && window.appInsights.trackEvent) {
sendTick(id, data);
} else {
if (!queuedTicks) {
queuedTicks = [];
var interval = window.setInterval(function() {
if (window.appInsights && window.appInsights.trackEvent) {
window.clearInterval(interval);
for (var i = 0; i < queuedTicks.length; i++) {
queuedTicks[i]();
}
queuedTicks = undefined;
}
}, 1000);
}
if (queuedTicks.length >= 100) {
queuedTicks.shift();
}
queuedTicks.push(function () {
sendTick(id, data);
});
}
function sendTick(id, data) {
const props = {};
const measures = {};
if (data) {
Object.keys(data).forEach(k => {
if (typeof data[k] == "string") props[k] = data[k];
else if (typeof data[k] == "number") measures[k] = data[k];
else props[k] = JSON.stringify(data[k] || '');
});
} else {
data = {};
}
window.appInsights.trackEvent({name: id, properties: data, measurements: measures});
}
}
}
</script>