Skip to content

Instantly share code, notes, and snippets.

@gopalanand333
Last active March 15, 2021 11:12
Show Gist options
  • Save gopalanand333/999fca59e0119abaf066a56b72091127 to your computer and use it in GitHub Desktop.
Save gopalanand333/999fca59e0119abaf066a56b72091127 to your computer and use it in GitHub Desktop.
This script reads all the clicks done on a webpage's body, and pushing it into an array, a service is called where this array holding click related information can be pushed. There's a check for the length of array and if there is no event the service call is canceled. This gives the user of this script a privilege to understand the usage patter…
var myEventList = [];
function writeData() {
document.body.onclick = function(event) {
myEventList.push(formatData(event));
};
function formatData(data) {
var event = [];
event.push({
"x": data.clientX,
"y": data.clientY,
"screenH": data.view.screen.availHeight,
"screenY": data.view.screen.availWidth,
"url": data.currentTarget.baseURI,
"id": data.target.id
});
return event;
}
var url = "https://yourServiceToRecieveUsageData/";
if (myEventList.length > 0) {
$.ajax({
type: "POST",
url: url,
dataType: "json",
crossDomain: true,
data: JSON.stringify(myEventList),
success: function(result) {
myEventList = [];
},
error: function(response) {}
});
}
}
setInterval(writeData, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment