Comparing version 0.0.8 to 0.0.9-beta1
@@ -16,11 +16,7 @@ | ||
// upload protocol, "https" or "http" | ||
// If you declare it in `uploadEndpoint`, this property will be ignored. | ||
uploadProtocol: "https", | ||
// Upload mode, "mixed", "periodic" or "event-triggered" | ||
uploadMode: "mixed", | ||
// Upload mode, "periodic" or "event-triggered" | ||
uploadMode: "periodic", | ||
// Type: number | ||
// If `uploadMode` == "periodic", data will be uploaded every `uploadPeriod` ms. | ||
// If `uploadMode` is "mixed", "periodic", data will be uploaded every `uploadPeriod` ms. | ||
// If no data are collected in a period, no data will be uploaded | ||
@@ -74,7 +70,7 @@ uploadPeriod: 5000, | ||
if (config.endpointType == "relative") { | ||
// Format the head -> "/*" | ||
// Format the head (the path should start with '/') | ||
if (url.startsWith("./")) { | ||
url = url.slice(1); | ||
} else if (url[0] !== "/") { | ||
url = `/${url}`; | ||
} else if (url[0] !== "/") { | ||
url = "/" + url; | ||
} | ||
@@ -86,8 +82,4 @@ // Format the tail | ||
// Construct absolute URL | ||
url = `${config.uploadProtocol}://${window.location.hostname}${url}`; | ||
} else if (config.endpointType == "absolute") { | ||
if (!(url.startsWith("http://") || url.startsWith("https://"))) { | ||
url = `${config.uploadProtocol}://${url}`; | ||
} | ||
} else { | ||
url = `${window.location.origin}${url}` | ||
} else if (config.endpointType !== "absolute") { | ||
throw new Error('`endpointType` can only be "absolute" or "relative"'); | ||
@@ -94,0 +86,0 @@ } |
113
index.js
@@ -24,3 +24,4 @@ const uuidv4 = require('uuid/v4'); | ||
let uploadIdx; | ||
let uploadInterval; | ||
let uploadInterval; // For "periodic" upload mode | ||
let uploadTimeout; // For "mixed" upload mode | ||
@@ -64,2 +65,20 @@ function getRelativeTimestampInSeconds() { | ||
function periodUploadTimeout() { | ||
clearTimeout(uploadTimeout); | ||
uploadTimeout = setTimeout(() => { | ||
if (eventsList.length > 0) { | ||
uploadTrace(); | ||
} | ||
}, config.uploadPeriod); | ||
} | ||
function periodUploadInterval() { | ||
clearInterval(uploadInterval); | ||
uploadInterval = setInterval(() => { | ||
if (eventsList.length != 0) { | ||
uploadTrace(); | ||
} | ||
}, config.uploadPeriod); | ||
} | ||
function mouseHandler(evt) { | ||
@@ -90,6 +109,11 @@ // PC's Chrome on Mobile mode can still receive "contextmenu" event with zero X, Y, so we ignore these events. | ||
eventsList.push(tmpEvt); | ||
if ( config.uploadMode == "event-triggered" && eventsList.length % config.frequency == 0 ) { | ||
uploadTrace(); | ||
} | ||
if ( config.uploadMode == "mixed" && eventsList.length % config.frequency == 0) { | ||
periodUploadTimeout(); | ||
uploadTrace(); | ||
} | ||
} | ||
@@ -103,29 +127,27 @@ | ||
function init(params) { | ||
return new Promise((resolve) => { | ||
clearBuffer(); | ||
pageLoadTime = new Date(); | ||
uploadIdx = 0; | ||
uploader = new Uploader(); | ||
impressionId = uuidv4(); | ||
uploader.setImpressionId(impressionId); | ||
if (buildConfig(params)) { | ||
// Upload an empty data to fetch config from backend | ||
uploadTrace().then( result => { | ||
if (result.status === 0) { // Success | ||
// clean up the buffer before unloading the window | ||
onbeforeunload = (evt) => { | ||
if (eventsList.length != 0) { | ||
uploadTrace(); | ||
} | ||
} | ||
resolve({status: 0}); | ||
} else { // Fail | ||
console.log(result.msg); | ||
resolve({status: -1, msg: `Fail to initialize config.`}); | ||
} | ||
}); | ||
} else { | ||
resolve({status: -1, msg: `Fail to initialize config.`}); | ||
clearBuffer(); | ||
pageLoadTime = new Date(); | ||
uploadIdx = 0; | ||
uploader = new Uploader(); | ||
impressionId = uuidv4(); | ||
uploader.setImpressionId(impressionId); | ||
if (buildConfig(params)) { | ||
// Async: Upload an empty data to ofetch config from backend | ||
uploadTrace().then( result => { | ||
if (result.status === 0) { // Config is updated successfully | ||
resetCollector(); | ||
} else { | ||
console.log(result.msg); | ||
console.log("Fail to overwrite config with server config.") | ||
} | ||
}); | ||
onbeforeunload = (evt) => { | ||
if (eventsList.length != 0) { | ||
uploadTrace(); | ||
} | ||
} | ||
}) | ||
return {status: 0, msg: `Invalid configuration.`}; | ||
} else { | ||
return {status: -1, msg: `Invalid configuration.`} | ||
} | ||
} | ||
@@ -139,7 +161,7 @@ | ||
if (config.uploadMode === "periodic") { | ||
uploadInterval = setInterval(() => { | ||
if (eventsList.length != 0) { | ||
uploadTrace(); | ||
} | ||
}, config.uploadPeriod); | ||
periodUploadInterval(); | ||
} | ||
if (config.uploadMode == "mixed") { | ||
periodUploadTimeout(); | ||
} | ||
@@ -153,15 +175,20 @@ } | ||
clearInterval(uploadInterval); | ||
clearTimeout(uploadTimeout); | ||
} | ||
function resetCollector(removeData = false) { | ||
stopCollector(); | ||
runCollector(); | ||
} | ||
export function run(params) { | ||
init(params).then( res => { | ||
if (res.status === 0) { | ||
runCollector(); | ||
uploader.start(impressionId); | ||
console.log("Mouselog agent is activated!"); | ||
} else { | ||
console.log(res.msg); | ||
console.log("Fail to initialize Mouselog agent."); | ||
} | ||
}) | ||
let res = init(params); | ||
if (res.status == 0) { | ||
runCollector(); | ||
uploader.start(impressionId); | ||
console.log("Mouselog agent is activated!"); | ||
} else { | ||
console.log(res.msg); | ||
console.log("Fail to initialize Mouselog agent."); | ||
} | ||
} | ||
@@ -168,0 +195,0 @@ |
{ | ||
"name": "mouselog", | ||
"version": "0.0.8", | ||
"version": "0.0.9-beta1", | ||
"description": "The mouse tracking agent for Mouselog.", | ||
@@ -8,3 +8,4 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build": "cd webpack-build && webpack && webpack --config comp.js && cd .." | ||
}, | ||
@@ -21,3 +22,7 @@ "keywords": [], | ||
"uuid": "^3.3.3" | ||
}, | ||
"devDependencies": { | ||
"webpack": "^4.41.5", | ||
"webpack-cli": "^3.3.10" | ||
} | ||
} |
@@ -10,6 +10,6 @@ [![NPM version](https://img.shields.io/npm/v/mouselog)](https://www.npmjs.com/package/mouselog) | ||
## Embedded JS | ||
## CDN | ||
Embed Mouselog in your HTML files: | ||
```html | ||
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/mouselog@latest/mouselog.js"></script> | ||
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/mouselog@latest/build/mouselog.js"></script> | ||
<script> | ||
@@ -23,7 +23,7 @@ mouselog.run({ | ||
``` | ||
You can also refer mouselog dynamically in Javascript: | ||
You can also include mouselog dynamically in Javascript: | ||
```Javascript | ||
(function() { | ||
var script = document.createElement("script"); | ||
script.src = "https://cdn.jsdelivr.net/npm/mouselog@latest/mouselog.js"; | ||
script.src = "https://cdn.jsdelivr.net/npm/mouselog@latest/build/mouselog.js"; | ||
script.onload = () => { | ||
@@ -41,2 +41,9 @@ mouselog.run({ | ||
``` | ||
## Build Manually | ||
You can also bundle Mouselog.js manually via | ||
``` | ||
npm i | ||
npm run build | ||
``` | ||
This will generate the bundled scripts in `./build`. | ||
@@ -77,9 +84,8 @@ ## NPM | ||
// upload protocol, "https" or "http" | ||
// If you declare it in `uploadEndpoint`, this property will be ignored. | ||
uploadProtocol: "https", | ||
// Upload mode, "mixed", "periodic" or "event-triggered" | ||
// "periodic": upload data in every period. | ||
// "event-triggered": upload data when a number of interaction data is captured | ||
// "mixed": the mixture of the previous two upload mode | ||
uploadMode: "mixed", | ||
// Upload mode, "periodic" or "event-triggered" | ||
uploadMode: "periodic", | ||
// Type: number | ||
@@ -97,18 +103,9 @@ // If `uploadMode` == "periodic", data will be uploaded every `uploadPeriod` ms. | ||
// Use GET method to upload data? (stringified data will be embedded in URI) | ||
enableGet: false, | ||
// Type: number | ||
// Time interval for resending the failed trace data | ||
resendInterval: 3000, | ||
enableGet: false | ||
} | ||
``` | ||
# Demo | ||
[Mouselog-demo](https://github.com/hsluoyz/mouselog-demo) | ||
<!-- # Demo | ||
[Mouselog-demo](https://github.com/hsluoyz/mouselog-demo) --> | ||
# Schema | ||
![image](schema.jpg) | ||
# Contributing | ||
@@ -115,0 +112,0 @@ |
@@ -48,3 +48,3 @@ let {config, updateConfig} = require('./config'); | ||
}).catch(err => { | ||
_appendFailedData(data); | ||
this._appendFailedData(data); | ||
resolve({status: -1, msg: `Fail to upload a bunch of data: ${err.message}`}); | ||
@@ -61,2 +61,3 @@ }) | ||
let i = 0; | ||
let obj = this.resendQueue[i]; | ||
while (i < this.resendQueue.length) { | ||
@@ -63,0 +64,0 @@ if (obj.status == StatusEnum.SUCCESS) { |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
880
45115
2
10
119