Skip to content

Commit 8626747

Browse files
Support clicks on child elements in button click tracking (close snowplow#1280)
PR snowplow#1281 --------- Co-authored-by: Jethro Nederhof <jethro.nederhof@snowplowanalytics.com>
1 parent a377d94 commit 8626747

File tree

5 files changed

+44
-6
lines changed

5 files changed

+44
-6
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-button-click-tracking",
5+
"comment": "Support clicks on child elements in button click tracking (#1280)",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/browser-plugin-button-click-tracking"
10+
}
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/javascript-tracker",
5+
"comment": "Support clicks on child elements in button click tracking (#1280)",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/javascript-tracker"
10+
}

plugins/browser-plugin-button-click-tracking/src/api.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,18 @@ export function disableButtonClickTracking() {
9797
* @param context - The dynamic context which will be evaluated for each button click event
9898
*/
9999
function eventHandler(event: MouseEvent, trackerId: string, filter: FilterFunction, context?: DynamicContext) {
100-
const elem = event.target as HTMLElement;
101-
if (elem.tagName === 'BUTTON' || (elem.tagName === 'INPUT' && (elem as HTMLInputElement).type === 'button')) {
102-
if (filter(elem)) {
103-
const buttonClickEvent = createEventFromButton(elem as HTMLButtonElement | HTMLInputElement);
104-
buttonClickEvent.context = resolveDynamicContext(context, buttonClickEvent);
105-
trackButtonClick(buttonClickEvent, [trackerId]);
100+
let elem = event.target as HTMLElement | null;
101+
while (elem) {
102+
if (elem instanceof HTMLButtonElement || (elem instanceof HTMLInputElement && elem.type === 'button')) {
103+
if (filter(elem)) {
104+
const buttonClickEvent = createEventFromButton(elem);
105+
buttonClickEvent.context = resolveDynamicContext(context, buttonClickEvent);
106+
trackButtonClick(buttonClickEvent, [trackerId]);
107+
}
108+
// presume nested buttons aren't a thing
109+
return;
106110
}
111+
112+
elem = elem.parentElement;
107113
}
108114
}

trackers/javascript-tracker/test/integration/buttonClick.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ describe('Snowplow Micro integration', () => {
6565
await (await $('#button7')).click();
6666
await browser.pause(500);
6767

68+
// Nested child
69+
await (await $('#button-child')).click();
70+
await browser.pause(500);
71+
6872
// Disable/enable
6973

7074
await (await $('#disable')).click();
@@ -131,6 +135,11 @@ describe('Snowplow Micro integration', () => {
131135
logContainsButtonClick(ev);
132136
});
133137

138+
it('should get button when click was on a child element', async () => {
139+
const ev = makeEvent({ label: 'TestChildren' }, method);
140+
logContainsButtonClick(ev);
141+
});
142+
134143
it('should not get disabled-click', () => {
135144
const ev = makeEvent({ id: 'disabled-click', label: 'DisabledClick' }, method);
136145
expect(logContains(ev)).toBe(false);

trackers/javascript-tracker/test/pages/button-click-tracking.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
<!-- A button that adds a new button dynamically -->
3434
<button id="addDynamic" onclick="addDynamicButton()">AddButton</button>
3535

36+
<!-- Ensure button tracked when children are clicked -->
37+
<button><span id="button-child">TestChildren</span></button>
38+
3639
<!-- Enable/disable testing -->
3740
<button id="disable" onclick="snowplow('disableButtonClickTracking')">Disable</button>
3841
<button id="disabled-click">DisabledClick</button>

0 commit comments

Comments
 (0)