Sample of main.js - Developer Documentation
Skip to content

main.js

Create an main.js file with the following content:

function eventAnalytics() {

  const eventAnalyticsInput = document.getElementById('eventAnalyticsInput').value;

  // region request for training model.
  const xhr = new XMLHttpRequest();
  xhr.open('POST', `/api/eventcount/v3/countEvents`, true);
  xhr.setRequestHeader('Content-Type', 'application/json');
  xhr.setRequestHeader('X-XSRF-TOKEN', getCookie("XSRF-TOKEN"));
  xhr.onreadystatechange = function() {
    if (xhr.readyState != 4) return;
    if (xhr.status != 200 && xhr.status != 201) {
      console.log(xhr.status + ': ' + xhr.statusText);
    }
    else {
      console.log(xhr.responseText);
      const response = JSON.parse(xhr.responseText);
      document.getElementById('eventAnalytics-result').textContent = JSON.stringify(response, undefined, 4);
    }
  }
  xhr.send(eventAnalyticsInput);
  // endregion.
}

function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

document.addEventListener('DOMContentLoaded', () => {
  document.getElementById('eventAnalyticsInput').value = JSON.stringify({
  "eventsMetadata": {
    "eventTextPropertyName": "text",
    "splitInterval": 5000
  },
  "events": [
    {
      "_time": "2017-10-01T12:00:00.001Z",
      "text": "INTRODUCING FUEL",
      "text_qc": 0
    },
    {
      "_time": "2017-10-01T12:00:00.023Z",
      "text": "INTRODUCING FUEL",
      "text_qc": 0
    },
    {
      "_time": "2017-10-01T12:00:01.001Z",
      "text": "Status@Flame On",
      "text_qc": 0
    },
    {
      "_time": "2017-10-01T12:00:02.001Z",
      "text": "Status@Flame Off",
      "text_qc": 0
    },
    {
      "_time": "2017-10-01T12:00:03.001Z",
      "text": "Error code: 340",
      "text_qc": 0
    },
    {
      "_time": "2017-10-01T12:00:04.001Z",
      "text": "Error code: 340",
      "text_qc": 0
    },
    {
      "_time": "2017-10-01T12:00:04.002Z",
      "text": "Error code: 340",
      "text_qc": 0
    },
    {
      "_time": "2017-10-01T12:00:05.001Z",
      "text": "INTRODUCING FUEL",
      "text_qc": 0
    },
    {
      "_time": "2017-10-01T12:00:08.001Z",
      "text": "Status@Flame On",
      "text_qc": 0
    },
    {
      "_time": "2017-10-01T12:00:09.001Z",
      "text": "Status@Flame Off",
      "text_qc": 0
    }
  ]
}, undefined, 4);
});

Last update: March 5, 2019

Except where otherwise noted, content on this site is licensed under the Development License Agreement.