Skip to content

Commit 1e7960d

Browse files
committed
Fill empty targetUrl for link clicks with "about:invalid" to avoid failed events and output a warning (#1360)
1 parent fd7b975 commit 1e7960d

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ export function trackLinkClick(
190190

191191
/**
192192
* Process a clicked element into a link_click event payload.
193+
*
194+
* In case the href of the element is empty, "about:invalid" is used as the target URL.
193195
*
194196
* @param sourceElement The trackable element to be used to build the payload
195197
* @param includeContent Whether to include the element's contents in the payload
@@ -210,9 +212,13 @@ function processClick(sourceElement: TrackableElement, includeContent: boolean =
210212
elementTarget = anchorElement.target;
211213
elementContent = includeContent ? anchorElement.innerHTML : undefined;
212214

215+
if (!targetUrl) {
216+
_logger?.warn('Link click target URL empty', anchorElement);
217+
}
218+
213219
// decodeUrl %xx
214220
return buildLinkClick({
215-
targetUrl,
221+
targetUrl: targetUrl || 'about:invalid',
216222
elementId,
217223
elementClasses,
218224
elementTarget,

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,26 @@ describe('LinkClickTrackingPlugin', () => {
207207
});
208208
});
209209

210+
it('tracks clicks on links without href', async () => {
211+
enableLinkClickTracking();
212+
213+
const target = document.createElement('a');
214+
document.body.appendChild(target);
215+
216+
target.click();
217+
218+
expect(
219+
extractUeEvent('iglu:com.snowplowanalytics.snowplow/link_click/jsonschema/1-0-1').from(
220+
await eventStore.getAllPayloads()
221+
)
222+
).toMatchObject({
223+
schema: 'iglu:com.snowplowanalytics.snowplow/link_click/jsonschema/1-0-1',
224+
data: {
225+
targetUrl: 'about:invalid',
226+
},
227+
});
228+
});
229+
210230
it('tracks clicks on child elements of links and contents', async () => {
211231
enableLinkClickTracking({ trackContent: true });
212232

0 commit comments

Comments
 (0)