aboutsummaryrefslogtreecommitdiff
path: root/record.js
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2024-01-18 13:26:24 -0500
committersotech117 <michael_foiani@brown.edu>2024-01-18 13:26:24 -0500
commitc4371f61a68dcc25c42fa9fc78c624d75b9964dc (patch)
treeca38a60e03a7a42d46ebb1f517377bb5744148de /record.js
add files via upload
Diffstat (limited to 'record.js')
-rw-r--r--record.js43
1 files changed, 43 insertions, 0 deletions
diff --git a/record.js b/record.js
new file mode 100644
index 0000000..a97ebbd
--- /dev/null
+++ b/record.js
@@ -0,0 +1,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);