Skip to content

Sleep tracker JS is a component for tracking user inactivity on a web application. This component manages several open pages

License

Notifications You must be signed in to change notification settings

GBonnaire/sleep-tracker-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sleeper-tracker-js

Sleep tracker JS is a component for tracking user inactivity on a web application. This component manages several pages opened

Get started

const sleepTrackerInstance = sleepTracker({
    timeout: 60, // in minutes, By default is 30 minutes
    delayCallAwake: 10
});
sleepTrackerInstance.on("beforeasleep", function() {
    // Your code to open modal to ask if user is awake ! If ok call sleepTrackerInstance.awake();
});
sleepTrackerInstance.on("awakened", function() {
    // Your code after user indicate on other page he awake. If modal is open, you can close it
});
sleepTrackerInstance.on("awake", function() {
    // Your code to execute after to indicate to the server user is awake
});
sleepTrackerInstance.on("asleep", function() {
    // Your code to execute because user is asleep. example logout, black screen, ...
});

CDN

You can use this CDN link

<script src="https://cdn.jsdelivr.net/gh/GBonnaire/sleep-tracker-js@latest/dist/sleeptracker.min.js"></script>

NPM

npm install @guillaumebonnaire/sleep-tracker-js

import sleepTracker from '@guillaumebonnaire/sleep-tracker-js';

Documentation

Options of component

Option name Description Type Default Value
timeout time out when user is considered comme asleep. In minutes int 30
timeoutBeforeAsleep Minutes before time out when application ask if user is awake. In minutes int 2
detectActivity If component detect activity like keydown, scroll, focus boolean true
delayCallAwake Delay between 2 trigger event "awake". In minutes int 5
keyStorage Key used to store data in localStorage string based on hostname
onload on load event function (Object: component) null
oninit on init event function (Object: component) null
debug debug mode, verbose all event triggered on console boolean false

functions of component

Function name Description Syntax Return
on Function to defined a function triggered by the component on(String: event, Function: callback) avoid
asleep Function to indicate user is asleep asleep() avoid
wakeup Function to indicate user is wakeup wakeup() avoid
awake Function to indicate user is awake awake() avoid
isAwake Function to check if user is awake isAwake() boolean

Events of component

Event name Description Syntax
asleep Triggered when user is asleep function (Object: component)
beforeasleep Triggered some minutes before time out will be trigger function (Object: component)
wakeup Triggered when user is wakeup (before asleep) function (Object: component)
awake Triggered when user is awake (activity) function (Object: component)
awakened Triggered when user is awakened (trigger is beforeasleep is triggered) function (Object: component)
init Triggered when component is init function (Object: component)
load Triggered when component is loaded function (Object: component)