aboutsummaryrefslogtreecommitdiff
path: root/record.js
blob: a97ebbd3c16d986e5dca853faadb17541bd85de5 (plain)
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
// generate a random UID for this user, on page load.
const UID = Math.round(Math.random() * 1000000);
// takes in an event object and updates local storage to contain that event
const recordAction = event => {
    let data = localStorage.getItem("cs1300-ab-testing-data");
    data = JSON.parse(data);
    // check if parsing is correct
    if (!Array.isArray(data)) {
        console.error("DATA is not an array")
        return;
    }

    // get version form the meta tag on the html file
    const version = document.querySelector("meta[name='version']").getAttribute("content");
    const uid = UID;
    const timestamp = Date.now().toString();
    const action = event.type;
    let target = event.target.tagName;
    let targetId = event.target.id;
    if (target == null) {
        target = 'N/A'
    }
    if (targetId) {
        target += `#${targetId}`
    }
    data.push({uid, version, action, target, timestamp});

    localStorage.setItem("cs1300-ab-testing-data", JSON.stringify(data));
}
// to be called on the click that determined when the task is completed to clean up event listeners
const done = event => {
    // record this event
    recordAction(event);

    // TODO: remove event listeners
    window.removeEventListener('load', recordAction);
    window.removeEventListener('click', recordAction);

    location.href = 'index.html';
}

window.addEventListener('load', recordAction);
window.addEventListener('click', recordAction);