File size: 1,784 Bytes
11276e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
document.addEventListener('DOMContentLoaded', function() {
    const array = new Uint32Array(1);
    window.crypto.getRandomValues(array);
    const userId = "id" + array[0].toString(16);
    const textarea = document.getElementById('prompt');
    const recom = document.getElementById('recommendation');
    
    let activityData = [];

    // waits for the page to fully load before attaching event listeners
    function logActivity(event) {
      //if (event.target.parentNode.id == "recommendation") console.log(event.target.textContent);
      // function records the data and logs it to the console
      const timestamp = new Date().toISOString();
      const buttonId = event.target.getAttribute('bx--tag');
      const activity = {
        //records event type, target element, timestamp for a userid
        userId: userId,
        type: event.type,
        target: event.target.tagName,
        timestamp: timestamp,
        text: textarea.value,
        recommentation: recom.textContent,
        label: event.target.textContent
      };
      activityData.push(activity);
      // logs to console
      console.log(JSON.stringify(activity)); 
    }
  
    ['click', 'keypress'].forEach(eventType => {
      document.addEventListener(eventType, logActivity, true); 
    });
  
    window.addEventListener('beforeunload', function() {
        // send data to server before page unload or when user navigates away or closes the page
        //  with navigator.sendBeacon
        if (activityData.length > 0) {
            localStorage.setItem('analyticsData', JSON.stringify(activityData));
            var blob = new Blob([JSON.stringify({activityData})], {type : 'application/json; charset=UTF-8'});
            navigator.sendBeacon('/log', blob);
        }
    });
 });