Skip to content

Commit

Permalink
Fill empty targetUrl for link clicks with "about:invalid" to avoid fa…
Browse files Browse the repository at this point in the history
…iled events and output a warning (#1360)
  • Loading branch information
matus-tomlein committed Oct 28, 2024
1 parent fd7b975 commit 1e7960d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 7 additions & 1 deletion plugins/browser-plugin-link-click-tracking/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ export function trackLinkClick(

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

if (!targetUrl) {
_logger?.warn('Link click target URL empty', anchorElement);
}

// decodeUrl %xx
return buildLinkClick({
targetUrl,
targetUrl: targetUrl || 'about:invalid',
elementId,
elementClasses,
elementTarget,
Expand Down
20 changes: 20 additions & 0 deletions plugins/browser-plugin-link-click-tracking/test/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,26 @@ describe('LinkClickTrackingPlugin', () => {
});
});

it('tracks clicks on links without href', async () => {
enableLinkClickTracking();

const target = document.createElement('a');
document.body.appendChild(target);

target.click();

expect(
extractUeEvent('iglu:com.snowplowanalytics.snowplow/link_click/jsonschema/1-0-1').from(
await eventStore.getAllPayloads()
)
).toMatchObject({
schema: 'iglu:com.snowplowanalytics.snowplow/link_click/jsonschema/1-0-1',
data: {
targetUrl: 'about:invalid',
},
});
});

it('tracks clicks on child elements of links and contents', async () => {
enableLinkClickTracking({ trackContent: true });

Expand Down

0 comments on commit 1e7960d

Please sign in to comment.