Skip to content

Commit 48e2532

Browse files
committed
Fix multiple link click events tracked for a single tracker in case multiple trackers are initialized on the page (close #1384)
PR #1385
1 parent c4a1b2a commit 48e2532

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@snowplow/browser-plugin-link-click-tracking",
5+
"comment": "Fix multiple link click events tracked for a single tracker in case multiple trackers are initialized on the page (#1384)",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/browser-plugin-link-click-tracking"
10+
}

plugins/browser-plugin-link-click-tracking/src/index.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,13 @@ function clickHandler(tracker: string, evt: MouseEvent | undefined): void {
274274

275275
// Using evt.type (added in IE4), we avoid defining separate handlers for mouseup and mousedown.
276276
if (event.type === 'click') {
277-
trackLinkClick({
278-
element: target,
279-
context: resolveDynamicContext(context, target),
280-
});
277+
trackLinkClick(
278+
{
279+
element: target,
280+
context: resolveDynamicContext(context, target),
281+
},
282+
[tracker]
283+
);
281284
} else if (event.type === 'mousedown') {
282285
if (button === 1 || button === 2) {
283286
_configuration[tracker].lastButton = button;
@@ -287,10 +290,13 @@ function clickHandler(tracker: string, evt: MouseEvent | undefined): void {
287290
}
288291
} else if (event.type === 'mouseup') {
289292
if (button === _configuration[tracker].lastButton && target === _configuration[tracker].lastTarget) {
290-
trackLinkClick({
291-
element: target,
292-
context: resolveDynamicContext(context, target),
293-
});
293+
trackLinkClick(
294+
{
295+
element: target,
296+
context: resolveDynamicContext(context, target),
297+
},
298+
[tracker]
299+
);
294300
}
295301
delete _configuration[tracker].lastButton;
296302
delete _configuration[tracker].lastTarget;

plugins/browser-plugin-link-click-tracking/test/events.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,26 @@ describe('LinkClickTrackingPlugin', () => {
342342

343343
expect(await eventStore.getAllPayloads()).toHaveLength(1);
344344
});
345+
346+
it('tracks a single link click with each tracker', async () => {
347+
addTracker('sp2', 'sp2', 'js-3.0.0', '', new SharedState(), {
348+
stateStorageStrategy: 'cookie',
349+
encodeBase64: false,
350+
plugins: [LinkClickTrackingPlugin()],
351+
eventStore,
352+
customFetch: async () => new Response(null, { status: 500 }),
353+
});
354+
355+
enableLinkClickTracking();
356+
357+
const target = document.createElement('a');
358+
target.href = 'https://www.example.com/exists';
359+
document.body.appendChild(target);
360+
361+
target.click();
362+
363+
expect(await eventStore.getAllPayloads()).toHaveLength(2);
364+
});
345365
});
346366

347367
describe('disableLinkClickTracking', () => {
@@ -351,7 +371,7 @@ describe('LinkClickTrackingPlugin', () => {
351371

352372
const addCalls = $addEventListener.mock.calls;
353373

354-
expect(addCalls).toHaveLength(1);
374+
expect(addCalls).toHaveLength(2);
355375
expect($removeEventListener.mock.calls).toContainEqual(addCalls[0]);
356376
});
357377
});

0 commit comments

Comments
 (0)