-
-
Notifications
You must be signed in to change notification settings - Fork 360
Add deeplinkIntegration for automatic deep link breadcrumbs #5983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+448
−0
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f8614a8
feat(core): add deeplinkIntegration for automatic deep link breadcrumbs
alwx 81ce886
docs: add changelog entry for deeplinkIntegration
alwx 560ceb9
Merge branch 'main' into alwx/improvement/deeplink-5424
alwx 2f72cfa
fix(core): Address review feedback in deeplinkIntegration
alwx 1a19735
refactor(core): Reuse existing sanitizeUrl from tracing/utils in deep…
alwx 4b134bc
fix(core): Fix hostname corruption and repeated setup leak in deeplin…
alwx 15924aa
style: Fix lint issues in deeplinkIntegration
alwx 2ccb63e
style: Move interfaces to top and attach JSDoc to tryGetLinking
alwx 5c4aa99
fix(core): Rename Linking variable to avoid duplicate declaration in …
alwx 5f4ed90
fix(core): Fix Linking duplicate declaration and revert unrelated rep…
alwx 6e295a9
fix(core): Use direct property access for Linking to avoid Babel dupl…
alwx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import type { IntegrationFn } from '@sentry/core'; | ||
|
|
||
| import { addBreadcrumb, defineIntegration, getClient } from '@sentry/core'; | ||
|
|
||
| import { sanitizeUrl } from '../tracing/utils'; | ||
|
|
||
| export const INTEGRATION_NAME = 'DeepLink'; | ||
|
|
||
| interface LinkingSubscription { | ||
| remove: () => void; | ||
| } | ||
|
|
||
| interface RNLinking { | ||
| getInitialURL: () => Promise<string | null>; | ||
| addEventListener: (event: string, handler: (event: { url: string }) => void) => LinkingSubscription; | ||
| } | ||
|
|
||
| /** | ||
| * Replaces dynamic path segments (UUID-like or numeric values) with a placeholder | ||
| * to avoid capturing PII in path segments when `sendDefaultPii` is off. | ||
| * | ||
| * Only replaces segments that look like identifiers (all digits, UUIDs, or hex strings). | ||
| */ | ||
| function sanitizeDeepLinkUrl(url: string): string { | ||
| const stripped = sanitizeUrl(url); | ||
|
|
||
| // Split off the scheme+authority (e.g. "myapp://host") so the regex | ||
| // only operates on the path and cannot corrupt the hostname. | ||
| const authorityEnd = stripped.indexOf('/', stripped.indexOf('//') + 2); | ||
| if (authorityEnd === -1) { | ||
| return stripped; | ||
| } | ||
|
|
||
| const authority = stripped.slice(0, authorityEnd); | ||
| const path = stripped.slice(authorityEnd); | ||
|
|
||
| // Replace path segments that look like dynamic IDs: | ||
| // - Numeric segments (e.g. /123) | ||
| // - UUID-formatted segments (e.g. /a1b2c3d4-e5f6-7890-abcd-ef1234567890) | ||
| // - Hex strings ≥8 chars (e.g. /deadbeef1234) | ||
| const sanitizedPath = path.replace( | ||
| /\/([0-9]+|[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}|[a-f0-9]{8,})(?=\/|$)/gi, | ||
| '/<id>', | ||
| ); | ||
|
|
||
| return authority + sanitizedPath; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the URL to include in the breadcrumb, respecting `sendDefaultPii`. | ||
| * When PII is disabled, query strings and ID-like path segments are removed. | ||
| */ | ||
| function getBreadcrumbUrl(url: string): string { | ||
| const sendDefaultPii = getClient()?.getOptions()?.sendDefaultPii ?? false; | ||
| return sendDefaultPii ? url : sanitizeDeepLinkUrl(url); | ||
| } | ||
|
|
||
| function addDeepLinkBreadcrumb(url: string): void { | ||
| const breadcrumbUrl = getBreadcrumbUrl(url); | ||
| addBreadcrumb({ | ||
| category: 'deeplink', | ||
| type: 'navigation', | ||
| message: breadcrumbUrl, | ||
| data: { | ||
| url: breadcrumbUrl, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| const _deeplinkIntegration: IntegrationFn = () => { | ||
| let subscription: LinkingSubscription | undefined; | ||
|
|
||
| return { | ||
| name: INTEGRATION_NAME, | ||
| setup(client) { | ||
| const linking = tryGetLinking(); | ||
|
|
||
| if (!linking) { | ||
| return; | ||
| } | ||
|
|
||
| // Remove previous subscription if setup is called again (e.g. repeated Sentry.init) | ||
| subscription?.remove(); | ||
|
|
||
| // Cold start: app opened via deep link | ||
| linking | ||
| .getInitialURL() | ||
| .then((url: string | null) => { | ||
| if (url) { | ||
| addDeepLinkBreadcrumb(url); | ||
| } | ||
| }) | ||
| .catch(() => { | ||
| // Ignore errors from getInitialURL | ||
| }); | ||
|
|
||
| // Warm open: deep link received while app is running | ||
| subscription = linking.addEventListener('url', (event: { url: string }) => { | ||
| if (event?.url) { | ||
| addDeepLinkBreadcrumb(event.url); | ||
| } | ||
| }); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| client.on('close', () => { | ||
| subscription?.remove(); | ||
| subscription = undefined; | ||
| }); | ||
|
sentry[bot] marked this conversation as resolved.
|
||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Attempts to import React Native's Linking module without a hard dependency. | ||
|
antonis marked this conversation as resolved.
|
||
| * Returns null if not available (e.g. in web environments). | ||
| */ | ||
| function tryGetLinking(): RNLinking | null { | ||
| try { | ||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| return (require('react-native') as { Linking: RNLinking }).Linking ?? null; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Integration that automatically captures breadcrumbs when deep links are received. | ||
| * | ||
| * Intercepts links via React Native's `Linking` API: | ||
| * - `getInitialURL` for cold starts (app opened via deep link) | ||
| * - `addEventListener('url', ...)` for warm opens (link received while running) | ||
| * | ||
| * Respects `sendDefaultPii`: when disabled, query params and ID-like path segments | ||
| * are stripped from the URL before it is recorded. | ||
| * | ||
| * Compatible with both Expo Router and plain React Navigation deep linking. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * Sentry.init({ | ||
| * integrations: [deeplinkIntegration()], | ||
| * }); | ||
| * ``` | ||
| */ | ||
| export const deeplinkIntegration = defineIntegration(_deeplinkIntegration); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.