import { BrowserPlugin, BrowserTracker, dispatchToTrackersInCollection } from '@snowplow/browser-tracker-core'; import { DynamicContext, CommonEventProperties, resolveDynamicContext } from '@snowplow/tracker-core'; import { createEventFromButton, filterFunctionFromFilter } from './util'; import { buildButtonClick } from './core'; import { ButtonClickEvent, ButtonClickTrackingConfiguration, FilterFunction } from './types'; const _trackers: Record = {}; // The event listeners added to the document, for each tracker // This allows them to be removed with `removeEventListener` if button click tracking is disabled const _listeners: Record void> = {}; /** * Button click tracking * * Will automatically tracking button clicks once enabled with 'enableButtonClickTracking' * or you can manually track button clicks with 'trackButtonClick' */ export function ButtonClickTrackingPlugin(): BrowserPlugin { return { activateBrowserPlugin: (tracker: BrowserTracker) => { _trackers[tracker.id] = tracker; }, }; } /** * Manually log a click * * @param event - The event information * @param trackers - The tracker identifiers which the event will be sent to */ export function trackButtonClick( event: ButtonClickEvent & CommonEventProperties, trackers: Array = Object.keys(_trackers) ) { dispatchToTrackersInCollection(trackers, _trackers, (t) => { t.core.track(buildButtonClick(event), event.context, event.timestamp); }); } /** * Enable automatic click tracking for all `