-
Notifications
You must be signed in to change notification settings - Fork 324
feat(react-native): record automatic exception steps #4573
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
Open
jakesciotto
wants to merge
3
commits into
main
Choose a base branch
from
feat/rn-automatic-exception-steps
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --- | ||
| '@posthog/core': minor | ||
| 'posthog-react-native': minor | ||
| --- | ||
|
|
||
| Add automatic exception steps to React Native, so a captured exception carries a timeline even where nobody called `addExceptionStep`. Set `errorTracking.exceptionSteps.automatic` to `true`, or to an object, to record a step for a screen change (`navigation`), an autocaptured tap (`taps`), or an app lifecycle transition (`lifecycle`). Every signal stays off by default, because each step adds bytes to every captured exception. | ||
|
|
||
| Automatic steps carry `$type`, which the error tracking timeline already renders, and they share the byte-bounded buffer and the native forwarding that manual steps use. A manual step stays untyped. An event that `before_send` dropped leaves no step. | ||
|
|
||
| The buffer keeps its existing lifetime. A capture keeps the steps, and `reset()` keeps them too, because steps scope to the app session rather than to the user session. |
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
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
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
168 changes: 168 additions & 0 deletions
168
packages/react-native/src/error-tracking/automatic-steps.ts
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,168 @@ | ||
| import { isString, isArray, isObject } from '@posthog/core' | ||
|
|
||
| /** | ||
| * `$type` values the SDK sets on an automatic step, so the error tracking timeline labels the step. | ||
| * The signals are the ones React Native observes, so the vocabulary lives here rather than in | ||
| * `@posthog/core`, which every SDK shares. | ||
| */ | ||
| export const EXCEPTION_STEP_TYPES = { | ||
| NAVIGATION: 'navigation', | ||
| TAP: 'tap', | ||
| LIFECYCLE: 'lifecycle', | ||
| } as const | ||
|
|
||
| export type ExceptionStepType = (typeof EXCEPTION_STEP_TYPES)[keyof typeof EXCEPTION_STEP_TYPES] | ||
|
|
||
| /** | ||
| * Which app signals the SDK turns into automatic steps. Every signal is opt-in, because a step adds | ||
| * bytes to each captured exception and the buffer already carries whatever the caller added by hand. | ||
| */ | ||
| export type AutomaticExceptionStepsOptions = { | ||
| /** Screen changes. @default false */ | ||
| navigation?: boolean | ||
| /** Taps the SDK already autocaptures. @default false */ | ||
| taps?: boolean | ||
| /** App lifecycle transitions such as open, foreground and background. @default false */ | ||
| lifecycle?: boolean | ||
| } | ||
|
|
||
| export type ResolvedAutomaticExceptionStepsOptions = { | ||
| navigation: boolean | ||
| taps: boolean | ||
| lifecycle: boolean | ||
| } | ||
|
|
||
| const ALL_SIGNALS_OFF: ResolvedAutomaticExceptionStepsOptions = { | ||
| navigation: false, | ||
| taps: false, | ||
| lifecycle: false, | ||
| } | ||
|
|
||
| const ALL_SIGNALS_ON: ResolvedAutomaticExceptionStepsOptions = { | ||
| navigation: true, | ||
| taps: true, | ||
| lifecycle: true, | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the automatic-steps options. `true` enables every signal, `false` and `undefined` disable | ||
| * every signal, and an object enables only the signals it sets. | ||
| */ | ||
| export function resolveAutomaticExceptionStepsOptions( | ||
| options?: boolean | AutomaticExceptionStepsOptions | null | ||
| ): ResolvedAutomaticExceptionStepsOptions { | ||
| if (options === true) { | ||
| return { ...ALL_SIGNALS_ON } | ||
| } | ||
|
|
||
| if (!options || !isObject(options)) { | ||
| return { ...ALL_SIGNALS_OFF } | ||
| } | ||
|
|
||
| return { | ||
| navigation: options.navigation ?? ALL_SIGNALS_OFF.navigation, | ||
| taps: options.taps ?? ALL_SIGNALS_OFF.taps, | ||
| lifecycle: options.lifecycle ?? ALL_SIGNALS_OFF.lifecycle, | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * App lifecycle events the SDK captures in `captureAppLifecycleEvents`. The step message is the | ||
| * event name itself, so the timeline reads the same as the event list. | ||
| */ | ||
| const LIFECYCLE_EVENTS = new Set<string>([ | ||
| 'Application Installed', | ||
| 'Application Updated', | ||
| 'Application Opened', | ||
| 'Application Became Active', | ||
| 'Application Backgrounded', | ||
| ]) | ||
|
|
||
| const TOUCH_EVENT_TYPE = 'touch' | ||
|
|
||
| export type AutomaticExceptionStep = { | ||
| type: ExceptionStepType | ||
| message: string | ||
| } | ||
|
|
||
| /** | ||
| * Maps an enqueued event to the automatic exception step it should leave behind, or `undefined` when | ||
| * the event is not a signal the caller enabled. | ||
| * | ||
| * The function is pure and reads only the event name and its properties, so the caller can run it on | ||
| * the capture path without touching the SDK's state. | ||
| */ | ||
| export function buildAutomaticExceptionStep( | ||
| config: ResolvedAutomaticExceptionStepsOptions, | ||
| event: unknown, | ||
| properties: unknown | ||
| ): AutomaticExceptionStep | undefined { | ||
| if (!isString(event)) { | ||
| return undefined | ||
| } | ||
|
|
||
| if (config.navigation && event === '$screen') { | ||
| return buildNavigationStep(properties) | ||
| } | ||
|
|
||
| if (config.taps && event === '$autocapture') { | ||
| return buildTapStep(properties) | ||
| } | ||
|
|
||
| if (config.lifecycle && LIFECYCLE_EVENTS.has(event)) { | ||
| return { type: EXCEPTION_STEP_TYPES.LIFECYCLE, message: event } | ||
| } | ||
|
|
||
| return undefined | ||
| } | ||
|
|
||
| function buildNavigationStep(properties: unknown): AutomaticExceptionStep | undefined { | ||
| const screenName = readStringProperty(properties, '$screen_name') | ||
| if (!screenName) { | ||
| return undefined | ||
| } | ||
|
|
||
| return { type: EXCEPTION_STEP_TYPES.NAVIGATION, message: `Screen: ${screenName}` } | ||
| } | ||
|
|
||
| /** | ||
| * Only a touch leaves a tap step. The label comes from the innermost autocaptured element, which | ||
| * holds either a `ph-label` or a component display name. | ||
| * | ||
| * The step never carries `$el_text` or touch coordinates. Element text is user-visible copy and can | ||
| * hold personal data, and an exception timeline does not need the pixel the user hit. | ||
| */ | ||
| function buildTapStep(properties: unknown): AutomaticExceptionStep | undefined { | ||
| if (readStringProperty(properties, '$event_type') !== TOUCH_EVENT_TYPE) { | ||
| return undefined | ||
| } | ||
|
|
||
| const label = readTapLabel(properties) | ||
| return { | ||
| type: EXCEPTION_STEP_TYPES.TAP, | ||
| message: label ? `Tap: ${label}` : 'Tap', | ||
| } | ||
| } | ||
|
|
||
| function readTapLabel(properties: unknown): string | undefined { | ||
| const elements = isObject(properties) ? (properties as Record<string, unknown>).$elements : undefined | ||
| if (!isArray(elements) || elements.length === 0) { | ||
| return undefined | ||
| } | ||
|
|
||
| // Elements run innermost first, so the first entry is the element the user actually hit. | ||
| return readStringProperty(elements[0], 'tag_name') | ||
| } | ||
|
|
||
| function readStringProperty(source: unknown, key: string): string | undefined { | ||
| if (!isObject(source)) { | ||
| return undefined | ||
| } | ||
|
|
||
| const value = (source as Record<string, unknown>)[key] | ||
| if (!isString(value) || value.trim().length === 0) { | ||
| return undefined | ||
| } | ||
|
|
||
| return value | ||
| } |
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.