UNPKG

@randyd45/web-behavior-tracker

Version:

A framework-agnostic package for tracking user behavior on web forms

96 lines 3.6 kB
import { TrackingUtils } from './utils.js'; /** * Handles mouse events like clicks, mouseover, mouseout */ export class MouseEventHandler { constructor(options, throttleDelay = 100) { this.options = options; this.throttleDelay = throttleDelay; } /** * Handles general mouse events (clicks, mouseover, mouseout) */ handleMouseEvent(event, onEventCreated) { // Ignore keyboard events if (event instanceof KeyboardEvent) { return; } const target = event.target; if (!target) return; // Check if the element is a form element or has a form-related role const isFormElement = TrackingUtils.isFormElement(target); if (!isFormElement) return; // Check if we should track this event type based on options if (!TrackingUtils.shouldTrackEvent(event.type, this.options)) { return; } // Throttle events const throttledHandler = TrackingUtils.throttle(() => { const elementValue = TrackingUtils.getElementValue(target); const elementState = TrackingUtils.getElementState(target); const behaviorEvent = { type: event.type, elementId: target.id || '', elementType: target.tagName.toLowerCase(), timestamp: Date.now(), value: elementValue, pageUrl: window.location.pathname, elementAttributes: TrackingUtils.getElementAttributes(target), elementState }; onEventCreated(behaviorEvent); }, this.throttleDelay); throttledHandler(); } /** * Handles click events specifically */ handleClickEvent(event, onEventCreated) { if (!this.options.trackClicks) return; const target = event.target; if (!target) return; // Check if the element is a form element or has a form-related role const isFormElement = TrackingUtils.isFormElement(target); if (!isFormElement) return; const behaviorEvent = TrackingUtils.createBehaviorEvent('click', target, TrackingUtils.getElementValue(target)); onEventCreated(behaviorEvent); } /** * Handles mouse movement events (mouseover, mouseout) */ handleMouseMovementEvent(event, onEventCreated) { if (!this.options.trackMouseMovements) return; const target = event.target; if (!target) return; // Check if the element is a form element or has a form-related role const isFormElement = TrackingUtils.isFormElement(target); if (!isFormElement) return; const behaviorEvent = TrackingUtils.createBehaviorEvent(event.type, target, TrackingUtils.getElementValue(target)); onEventCreated(behaviorEvent); } /** * Handles focus and blur events */ handleFocusBlurEvent(event, onEventCreated) { if (!this.options.trackFocusBlur) return; const target = event.target; if (!target) return; // Check if the element is a form element or has a form-related role const isFormElement = TrackingUtils.isFormElement(target); if (!isFormElement) return; const behaviorEvent = TrackingUtils.createBehaviorEvent(event.type, target, TrackingUtils.getElementValue(target)); onEventCreated(behaviorEvent); } } //# sourceMappingURL=MouseEventHandler.js.map