@analytics/activity-utils
Version:
User activity listener utilities
171 lines • 6.5 kB
TypeScript
/**
* @typedef {Object} ActivityEvent
* @property {('load'|'init'|'mousemove'|'mousedown'|'touchmove'|'touchstart'|'touchend'|'keydown'|'scroll'|'tabVisible')} type - The type of activity event
*/
/**
* @typedef {Function} ActivityCallback
* @param {ActivityEvent} event - The activity event that triggered the callback
*/
/**
* @typedef {Object} ActivityOptions
* @property {number} [throttle=10000] - Throttle time in milliseconds to limit callback frequency
*/
/**
* @typedef {Function} DisableFunction
* @returns {EnableFunction} Function to re-enable the activity tracker
*/
/**
* @typedef {Function} EnableFunction
* @returns {DisableFunction} Function to disable the activity tracker
*/
/**
* Attaches DOM event listeners to track user activity and calls a callback when activity is detected
* @param {ActivityCallback} callback - Function to call when DOM activity is detected
* @param {ActivityOptions} opts - Configuration options
* @returns {DisableFunction} Function that when called removes all listeners and returns a function to reattach them
*/
export function onDomActivity(callback: ActivityCallback, opts?: ActivityOptions): DisableFunction;
/**
* @typedef {Function} OnIdleCallback
* @param {number} activeTime - Time in seconds the user was active before becoming idle
* @param {ActivityEvent} event - The activity event that triggered the idle state
*/
/**
* @typedef {Object} OnIdleOptions
* @property {number} [timeout=10000] - Time in milliseconds before user is considered idle
* @property {number} [throttle=2000] - Throttle time in milliseconds for activity detection
*/
/**
* @typedef {Object} UserActivityStatus
* @property {boolean} isIdle - Whether the user is currently idle
* @property {boolean} isDisabled - Whether the activity tracker is disabled
* @property {number} active - Time in seconds the user has been active
* @property {number} idle - Time in seconds the user has been idle
*/
/**
* @typedef {Object} UserActivityReturn
* @property {DisableFunction} disable - Function to disable the activity tracker
* @property {Function} getStatus - Function that returns the current status
* @returns {UserActivityStatus} Current status of the activity tracker
*/
/**
* Creates an idle detector that calls a callback when the user becomes idle
* @param {OnIdleCallback} onIdle - Function to call when user becomes idle
* @param {OnIdleOptions} opts - Configuration options
* @returns {UserActivityReturn} Object with disable and getStatus methods
*/
export function onIdle(onIdle: OnIdleCallback, opts?: OnIdleOptions): UserActivityReturn;
/**
* @typedef {Function} WakeUpCallback
* @param {number} idleTime - Time in seconds the user was idle before becoming active
* @param {ActivityEvent} event - The activity event that triggered the wake up
*/
/**
* Creates a wake-up detector that calls a callback when the user becomes active after being idle
* @param {WakeUpCallback} onWakeUp - Function to call when user becomes active after being idle
* @param {OnIdleOptions} opts - Configuration options
* @returns {UserActivityReturn} Object with disable and getStatus methods
*/
export function onWakeUp(onWakeUp: WakeUpCallback, opts?: OnIdleOptions): UserActivityReturn;
/**
* @typedef {Function} HeartbeatCallback
* @param {number} activeTime - Time in seconds the user has been active
* @param {ActivityEvent} event - The activity event that triggered the heartbeat
*/
/**
* @typedef {Object} UserActivityOptions
* @property {OnIdleCallback} [onIdle] - Function to call when user becomes idle
* @property {WakeUpCallback} [onWakeUp] - Function to call when user becomes active after being idle
* @property {HeartbeatCallback} [onHeartbeat] - Function to call periodically while user is active
* @property {number} [timeout=10000] - Time in milliseconds before user is considered idle
* @property {number} [throttle=2000] - Throttle time in milliseconds for activity detection
*/
/**
* Creates a comprehensive user activity tracker that can detect idle, wake-up, and heartbeat events
* @param {UserActivityOptions} config - Configuration object
* @returns {UserActivityReturn} Object with disable and getStatus methods
*/
export function onUserActivity({ onIdle, onWakeUp, onHeartbeat, timeout, throttle }: UserActivityOptions): UserActivityReturn;
export type ActivityEvent = {
/**
* - The type of activity event
*/
type: ("load" | "init" | "mousemove" | "mousedown" | "touchmove" | "touchstart" | "touchend" | "keydown" | "scroll" | "tabVisible");
};
export type ActivityCallback = Function;
export type ActivityOptions = {
/**
* - Throttle time in milliseconds to limit callback frequency
*/
throttle?: number;
};
export type DisableFunction = Function;
export type EnableFunction = Function;
export type OnIdleCallback = Function;
export type OnIdleOptions = {
/**
* - Time in milliseconds before user is considered idle
*/
timeout?: number;
/**
* - Throttle time in milliseconds for activity detection
*/
throttle?: number;
};
export type UserActivityStatus = {
/**
* - Whether the user is currently idle
*/
isIdle: boolean;
/**
* - Whether the activity tracker is disabled
*/
isDisabled: boolean;
/**
* - Time in seconds the user has been active
*/
active: number;
/**
* - Time in seconds the user has been idle
*/
idle: number;
};
export type UserActivityReturn = {
/**
* - Function to disable the activity tracker
*/
disable: DisableFunction;
/**
* - Function that returns the current status
*/
getStatus: Function;
};
export type WakeUpCallback = Function;
export type HeartbeatCallback = Function;
export type UserActivityOptions = {
/**
* - Function to call when user becomes idle
*/
onIdle?: OnIdleCallback;
/**
* - Function to call when user becomes active after being idle
*/
onWakeUp?: WakeUpCallback;
/**
* - Function to call periodically while user is active
*/
onHeartbeat?: HeartbeatCallback;
/**
* - Time in milliseconds before user is considered idle
*/
timeout?: number;
/**
* - Throttle time in milliseconds for activity detection
*/
throttle?: number;
};
/**
* Function to call when page is unloading or becoming hidden
*/
export type UnloadCallback = Function;
//# sourceMappingURL=index.d.ts.map