diff --git a/app/scripts/background.js b/app/scripts/background.js index 5608dec89434..0e8d050ceb09 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -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, @@ -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)) { + if (shouldTrackDeepLinkNavigation(parsed)) { trackEvent(createEvent({ signature: parsed.signature, url })); } }) diff --git a/shared/lib/deep-links/metrics.test.ts b/shared/lib/deep-links/metrics.test.ts index d09722a872a0..5934836289a2 100644 --- a/shared/lib/deep-links/metrics.test.ts +++ b/shared/lib/deep-links/metrics.test.ts @@ -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', () => { diff --git a/shared/lib/deep-links/metrics.ts b/shared/lib/deep-links/metrics.ts index 084344cd9ba3..10505edf1ff9 100644 --- a/shared/lib/deep-links/metrics.ts +++ b/shared/lib/deep-links/metrics.ts @@ -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 = { @@ -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): boolean { + return !('redirectTo' in destination); +} + /** * Creates a trackable analytics event representing deep link usage. *