Skip to content

Commit

Permalink
Fix circular
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-el committed Nov 22, 2023
1 parent 1d35283 commit 9c69c77
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { BrowserTracker } from '@snowplow/browser-tracker-core';
import { DynamicContext } from '@snowplow/tracker-core';
import { clickHandler } from './clickHandler';
import { FilterFunction } from './util';

export function addListenersToButtonElements(
trackerId: string,
tracker: BrowserTracker,
filter: FilterFunction,
context?: DynamicContext,
document = window.document
) {
const buttons = Array.from(document.getElementsByTagName('button'));
for (const button of buttons.filter(filter)) {
button.addEventListener('click', () => {
clickHandler(trackerId, button, context);
clickHandler(tracker, button, context);
});
}
}
7 changes: 4 additions & 3 deletions plugins/browser-plugin-button-click-tracking/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { BrowserPlugin, BrowserTracker, dispatchToTrackersInCollection } from '@
import { DynamicContext, CommonEventProperties } from '@snowplow/tracker-core';

import { Filter, FilterFunction, filterFunctionFromFilter } from './util';
import { addListenersToButtonElements } from './button';
import { ButtonClickEvent, buildButtonClick } from './core';
import { addListenersToButtonElements } from './addListeners';

/** The configuration for automatic button click tracking */
export interface ButtonClickTrackingConfiguration {
Expand Down Expand Up @@ -72,7 +72,8 @@ export function enableButtonClickTracking(
};
_configuration[trackerId] = internalConfig;

const addListeners = () => addListenersToButtonElements(trackerId, internalConfig.filter, internalConfig.context);
const addListeners = () =>
addListenersToButtonElements(_trackers[trackerId], internalConfig.filter, internalConfig.context);
if (_trackers[trackerId]?.sharedState.hasLoaded) {
// the load event has already fired, add the click listeners now
addListeners();
Expand All @@ -93,7 +94,7 @@ export function refreshButtonClickTracking(trackers: Array<string> = Object.keys
trackers.forEach((trackerId) => {
// Retrieve the filter and context from the configuration
const { filter, context } = _configuration[trackerId];
const addListeners = () => addListenersToButtonElements(trackerId, filter, context);
const addListeners = () => addListenersToButtonElements(_trackers[trackerId], filter, context);
if (_trackers[trackerId]?.sharedState.hasLoaded) {
addListeners();
} else {
Expand Down
15 changes: 6 additions & 9 deletions plugins/browser-plugin-button-click-tracking/src/clickHandler.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { DynamicContext, resolveDynamicContext } from '@snowplow/tracker-core';
import { trackButtonClick } from './api';
import { BrowserTracker } from '@snowplow/browser-tracker-core';
import { createEventFromButton } from './util';
import { buildButtonClick } from './core';

// note: This is required to be in its own file to be able to spy on it with jest.spyOn
export function clickHandler(trackerId: string, button: HTMLButtonElement, context?: DynamicContext): void {
const event = createEventFromButton(button);

if (context) {
event.context = resolveDynamicContext(context);
}

trackButtonClick({ ...event }, [trackerId]);
export function clickHandler(tracker: BrowserTracker, button: HTMLButtonElement, context?: DynamicContext): void {
const buttonClickEvent = createEventFromButton(button);
const builtEvent = buildButtonClick(buttonClickEvent);
tracker.core.track(builtEvent, resolveDynamicContext(context));
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { createEventFromButton, filterFunctionFromFilter } from '../src/util';
// Required to be imported with this syntax to be able to spy with:
// jest.spyOn(clickTools, 'clickHandler')
import * as clickTools from '../src/clickHandler';
import { addListenersToButtonElements } from '../src/button';
import { addListenersToButtonElements } from '../src/addListeners';
import { BrowserTracker } from '@snowplow/browser-tracker-core';

describe('Button Click Tracking Plugin', () => {
describe('filterToFunction', () => {
Expand Down Expand Up @@ -114,7 +115,7 @@ describe('Button Click Tracking Plugin', () => {
it('should track an event on click', () => {
const spy = jest.spyOn(clickTools, 'clickHandler').mockImplementation(() => {});

const trackerId = 'testTracker';
const tracker = {} as BrowserTracker;

const context: DynamicContext = [
{
Expand All @@ -134,10 +135,10 @@ describe('Button Click Tracking Plugin', () => {
testDoc.body.appendChild(button);

const filter = filterFunctionFromFilter();
addListenersToButtonElements(trackerId, filter, context, testDoc);
addListenersToButtonElements(tracker, filter, context, testDoc);
button.click();

expect(spy).toHaveBeenCalledWith(trackerId, button, context);
expect(spy).toHaveBeenCalledWith(tracker, button, context);
expect(spy).toHaveBeenCalledTimes(1);
});
});
Expand Down

0 comments on commit 9c69c77

Please sign in to comment.