Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ import { getManifestFlags } from '../../shared/lib/manifestFlags';
import { DISPLAY_GENERAL_STARTUP_ERROR } from '../../shared/constants/start-up-errors';
import { getPartnerByOrigin } from '../../shared/constants/defi-referrals';
import { getInstallAttribution } from '../../shared/lib/install-attribution';
import { createEvent } from '../../shared/lib/deep-links/metrics';
import {
createEvent,
shouldTrackDeepLinkNavigation,
} from '../../shared/lib/deep-links/metrics';
import {
backedUpStateKeys,
hasVault,
Expand Down Expand Up @@ -905,7 +908,7 @@ async function initialize(backup) {
})
.on('navigate', async ({ url, parsed }) => {
// don't track deep links that are immediately redirected (like /buy)
if (!('redirectTo' in parsed)) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'redirectTo' in parsed is always false, since parsed will never have a redirectTo property. The redirectTo property sometimes lives on parsed.destination.

if (shouldTrackDeepLinkNavigation(parsed)) {
trackEvent(createEvent({ signature: parsed.signature, url }));
}
})
Expand Down
25 changes: 24 additions & 1 deletion shared/lib/deep-links/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,30 @@ import {
MetaMetricsEventName,
} from '../../constants/metametrics';
import { MISSING, VALID, INVALID } from './verify';
import { createEvent } from './metrics';
import { createEvent, shouldTrackDeepLinkNavigation } from './metrics';

describe('shouldTrackDeepLinkNavigation', () => {
it('returns false for an external redirect destination', () => {
const result = shouldTrackDeepLinkNavigation({
destination: {
redirectTo: new URL('https://portfolio.metamask.io/buy'),
},
});

expect(result).toBe(false);
});

it('returns true for an extension route destination', () => {
const result = shouldTrackDeepLinkNavigation({
destination: {
path: '/perps',
query: new URLSearchParams(),
},
});

expect(result).toBe(true);
});
});

describe('createEvent', () => {
describe('basic functionality', () => {
Expand Down
17 changes: 17 additions & 0 deletions shared/lib/deep-links/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
MetaMetricsEventName,
} from '../../constants/metametrics';
import { UTM_PARAMETERS, type UTMParameter } from '../../types/metametrics';
import type { ParsedDeepLink } from './parse';
import type { SignatureStatus } from './verify';

export type Properties = {
Expand All @@ -20,6 +21,22 @@ export type EventDetails = {
signature: SignatureStatus;
};

/**
* Determines whether a parsed deep-link navigation should be tracked.
*
* Destinations that redirect outside the extension are excluded because the
* extension does not handle the resulting navigation.
*
* @param parsed - The parsed deep-link destination.
* @param parsed.destination
* @returns Whether the navigation should produce a deep-link analytics event.
*/
export function shouldTrackDeepLinkNavigation({
destination,
}: Pick<ParsedDeepLink, 'destination'>): boolean {
return !('redirectTo' in destination);
}

/**
* Creates a trackable analytics event representing deep link usage.
*
Expand Down
Loading