Comparing version 0.0.4 to 0.0.5-beta1
97
index.js
@@ -1,9 +0,12 @@ | ||
const uuidv1 = require('uuid/v1'); | ||
const uuidv4 = require('uuid/v4'); | ||
const Uploader = require('./uploader'); | ||
// default config | ||
let config = { | ||
upload: { | ||
url: "/data", | ||
frequency: 2, | ||
} | ||
// Upload the website interaction data object when every `frequency` events are captured. | ||
frequency: 50, | ||
// The website interaction data object will be encoded by `encoder` before uploading to the server. | ||
encoder: JSON.stringify, | ||
// The response data will be decoded by `decoder` | ||
decoder: x => x | ||
}; | ||
@@ -24,3 +27,6 @@ | ||
let uuid; | ||
let uploader = new Uploader(); | ||
let impressionId; | ||
let serverUrl; | ||
let websiteId; | ||
let eventsList; | ||
@@ -53,24 +59,26 @@ let pageLoadTime; | ||
function uploadTrace(evts) { | ||
const width = window.document.body.scrollWidth; | ||
const height = window.document.body.scrollHeight; | ||
const trace = { | ||
function newTrace() { | ||
let trace = { | ||
id: '0', | ||
idx: uploadIdx, | ||
id: window.location.pathname, // path | ||
uid: uuid, | ||
width: width, | ||
height: height, | ||
url: window.location.hostname ? window.location.hostname : "localhost", | ||
path: window.location.pathname, | ||
width: document.body.scrollWidth, | ||
height: document.body.scrollHeight, | ||
pageLoadTime: pageLoadTime, | ||
label: -1, | ||
guess: -1, | ||
events: evts | ||
}; | ||
let traceStr = JSON.stringify(trace); | ||
events: [] | ||
} | ||
uploadIdx += 1; | ||
return trace; | ||
} | ||
return fetch(config.upload.url, { | ||
method: 'POST', | ||
credentials: 'include', | ||
body: JSON.stringify({key: "value"}), | ||
}).then(res => res.json()); | ||
function uploadTrace(init=false) { | ||
let trace = newTrace(); | ||
if (!init) { | ||
trace.events = eventsList; | ||
eventsList = []; | ||
} | ||
uploader.upload(trace); | ||
} | ||
@@ -89,3 +97,4 @@ | ||
} | ||
let t = { | ||
let tmpEvt = { | ||
id: eventsList.length, | ||
timestamp: getRelativeTimestampInSeconds(), | ||
@@ -99,10 +108,9 @@ type: evt.type, | ||
if (evt.type == "wheel") { | ||
t.deltaX = evt.deltaX; | ||
t.deltaY = evt.deltaY; | ||
tmpEvt.deltaX = evt.deltaX; | ||
tmpEvt.deltaY = evt.deltaY; | ||
} | ||
eventsList.push(t); | ||
eventsList.push(tmpEvt); | ||
if ( eventsList.length % config.upload.frequency == 0 ) { | ||
uploadTrace(eventsList.slice( uploadIdx*config.upload.frequency, (uploadIdx+1)*config.upload.frequency)); | ||
uploadIdx += 1; | ||
if ( eventsList.length % config.frequency == 0 ) { | ||
uploadTrace(); | ||
} | ||
@@ -115,9 +123,18 @@ } | ||
uploadIdx = 0; | ||
uploader.start( | ||
serverUrl, | ||
websiteId, | ||
impressionId, | ||
{ encoder: config.encoder, decoder: config.decoder } | ||
); | ||
} | ||
export function run(_config) { | ||
if (_config) { | ||
config = _config; | ||
export function run(_serverUrl, _websiteId, options) { | ||
if (options) { | ||
config = options; | ||
} | ||
uuid = uuidv1(); | ||
serverUrl = _serverUrl; | ||
websiteId = _websiteId; | ||
impressionId = uuidv4(); | ||
refresh(); | ||
@@ -127,3 +144,6 @@ | ||
window.document.addEventListener(s, (evt) => mouseHandler(evt)); | ||
}) | ||
}); | ||
// Send an empty trace data when mouselog is activated | ||
uploadTrace(true); | ||
} | ||
@@ -134,3 +154,8 @@ | ||
window.document.removeEventListener(s, (evt) => mouseHandler(evt)); | ||
}) | ||
}); | ||
uploader.stop(); | ||
} | ||
export function test() { | ||
console.log("TEST FUNCTION"); | ||
} |
{ | ||
"name": "mouselog", | ||
"version": "0.0.4", | ||
"version": "0.0.5-beta1", | ||
"description": "The mouse tracking agent for Mouselog.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,6 +0,40 @@ | ||
# Configuration | ||
[![NPM version](https://img.shields.io/npm/v/mouselog)](https://www.npmjs.com/package/mouselog) | ||
![Repo Size](https://img.shields.io/github/repo-size/microsoft/mouselog.js) | ||
TODO | ||
# Usage | ||
## NPM | ||
Install Mouselog.js via | ||
``` | ||
npm i mouselog --save | ||
``` | ||
Then load and configure mouselog | ||
```Javascript | ||
const mouselog = require('mouselog'); | ||
let config = { | ||
// Upload the data object when every `frequency` events are captured. | ||
frequency: 50, | ||
// Data objects will be encoded by `encoder` before uploading to the server. | ||
encoder: JSON.stringify, | ||
// The response data will be decoded by `decoder` | ||
decoder: x => x | ||
} | ||
``` | ||
Run Mouselog and it will automatically collect all you want. | ||
```Javascript | ||
mouselog.run("YOUR_SERVER_URL", "YOUR_WEBSITE_NAME", config); | ||
``` | ||
You can also deactivate Mouselog by calling `mouselog.stop()`. | ||
# Demo | ||
[Mouselog-demo](https://github.com/hsluoyz/mouselog-demo) | ||
# Schema | ||
![image](schema.jpg) | ||
# Contributing | ||
@@ -7,0 +41,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34984
8
207
53