Skip to content

refactor(analytics): make trackEvent error-safe, add set-once trait helper#8061

Open
talissoncosta wants to merge 3 commits into
feat/onboarding-analytics-7738from
feat/onboarding-variant-set-once
Open

refactor(analytics): make trackEvent error-safe, add set-once trait helper#8061
talissoncosta wants to merge 3 commits into
feat/onboarding-analytics-7738from
feat/onboarding-variant-set-once

Conversation

@talissoncosta

@talissoncosta talissoncosta commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #7960. Two robustness fixes to the shared analytics helpers, deferred to keep #7960 onboarding-only.

Changes

  • trackTraitsOnce (amplitude setOnce) so the onboarding variant is pinned to a user's first exposure and can't be overwritten on later loads (Wadii's note). The getting-started gate uses it.
  • trackEvent wrapped in try/catch so a failing tracker can't break the caller, e.g. the onboarding bootstrap firing milestone events (kyle's note).

How did you test this code?

Sign up, reach onboarding, confirm onboarding_variant is set once and unchanged on revisit, and events still fire.

@vercel

vercel Bot commented Jul 21, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
flagsmith-frontend-preview Ready Ready Preview, Comment Jul 22, 2026 12:50pm
flagsmith-frontend-staging Ready Ready Preview, Comment Jul 22, 2026 12:50pm
1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Ignored Ignored Preview Jul 22, 2026 12:50pm

Request Review

@coderabbitai

coderabbitai Bot commented Jul 21, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 2460f755-6a3f-43dd-99ec-930bd5ed8fe1

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Use amplitude setOnce for onboarding_variant so it's pinned to the user's
first exposure and doesn't get overwritten on later loads. Adds
API.trackTraitsOnce; the getting-started gate uses it. Addresses Wadii's
review note on #7960.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
A failing tracker (GA/amplitude) can't break the caller, e.g. the
onboarding bootstrap firing milestone events. Addresses kyle's note on
#7960.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@talissoncosta talissoncosta changed the title refactor(onboarding): tag variant with setOnce refactor(analytics): harden shared analytics helpers Jul 21, 2026
@talissoncosta
talissoncosta marked this pull request as ready for review July 21, 2026 18:30
@talissoncosta
talissoncosta requested a review from a team as a code owner July 21, 2026 18:30
@talissoncosta
talissoncosta requested review from kyle-ssg and removed request for a team July 21, 2026 18:30
@talissoncosta
talissoncosta requested review from Zaimwa9 and removed request for kyle-ssg July 21, 2026 18:31
@github-actions

github-actions Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Docker builds report

Image Build Status Security report
ghcr.io/flagsmith/flagsmith-e2e:pr-8061 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-api-test:pr-8061 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-api:pr-8061 Finished ✅ Results
ghcr.io/flagsmith/flagsmith:pr-8061 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-private-cloud:pr-8061 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-frontend:pr-8061 Finished ✅ Results

@talissoncosta talissoncosta changed the title refactor(analytics): harden shared analytics helpers refactor(analytics): make trackEvent error-safe, add set-once trait helper Jul 21, 2026
@github-actions

github-actions Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor
✅ private-cloud · depot-ubuntu-latest-16 — run #18629 (attempt 1)

Playwright Test Results (private-cloud - depot-ubuntu-latest-16)

passed  3 passed

Details

stats  3 tests across 3 suites
duration  37.5 seconds
commit  09cb982
info  🔄 Run: #18629 (attempt 1)

🗂️ Previous results
✅ private-cloud · depot-ubuntu-latest-arm-16 — run #18629 (attempt 1)

Playwright Test Results (private-cloud - depot-ubuntu-latest-arm-16)

passed  2 passed

Details

stats  2 tests across 2 suites
duration  38.1 seconds
commit  09cb982
info  🔄 Run: #18629 (attempt 1)

✅ oss · depot-ubuntu-latest-arm-16 — run #18629 (attempt 1)

Playwright Test Results (oss - depot-ubuntu-latest-arm-16)

passed  1 passed

Details

stats  1 test across 1 suite
duration  42.2 seconds
commit  09cb982
info  🔄 Run: #18629 (attempt 1)

✅ oss · depot-ubuntu-latest-16 — run #18629 (attempt 1)

Playwright Test Results (oss - depot-ubuntu-latest-16)

passed  1 passed

Details

stats  1 test across 1 suite
duration  34.7 seconds
commit  09cb982
info  🔄 Run: #18629 (attempt 1)

✅ private-cloud · depot-ubuntu-latest-16 — run #18578 (attempt 1)

Playwright Test Results (private-cloud - depot-ubuntu-latest-16)

passed  1 passed

Details

stats  1 test across 1 suite
duration  1 minute, 4 seconds
commit  1707416
info  🔄 Run: #18578 (attempt 1)

✅ private-cloud · depot-ubuntu-latest-arm-16 — run #18578 (attempt 1)

Playwright Test Results (private-cloud - depot-ubuntu-latest-arm-16)

passed  2 passed

Details

stats  2 tests across 2 suites
duration  1 minute, 2 seconds
commit  1707416
info  🔄 Run: #18578 (attempt 1)

✅ oss · depot-ubuntu-latest-16 — run #18578 (attempt 1)

Playwright Test Results (oss - depot-ubuntu-latest-16)

passed  1 passed

Details

stats  1 test across 1 suite
duration  44.2 seconds
commit  1707416
info  🔄 Run: #18578 (attempt 1)

✅ oss · depot-ubuntu-latest-arm-16 — run #18578 (attempt 1)

Playwright Test Results (oss - depot-ubuntu-latest-arm-16)

passed  1 passed

Details

stats  1 test across 1 suite
duration  39.5 seconds
commit  1707416
info  🔄 Run: #18578 (attempt 1)

@github-actions

github-actions Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Visual Regression

19 screenshots compared. See report for details.
View full report

@Zaimwa9

Zaimwa9 commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

@themis-blindfold review this pull request. Take into account that this a follow up from PR #7960. Use the review from #7960 to contextualize this PR

Comment thread frontend/web/project/api.ts
@themis-blindfold

Copy link
Copy Markdown

⚖️ Themis judgement: 🧹 Ship it, nits inside

Two small, targeted robustness fixes for the analytics helpers introduced in #7960: trackEvent is wrapped in try/catch so a failing tracker cannot break callers (e.g. onboarding bootstrap), and a new trackTraitsOnce method uses Amplitude's setOnce to pin the onboarding variant to the user's first exposure. Both are sensible, the scope is tight, and CI is fully green.

🎯 Correctness 4/5
🧪 Test coverage 3/5
📐 Code quality 4/5
🚀 Product impact 3/5

🧹 Nits

  • frontend/web/project/api.tstrackTraitsOnce (and the existing trackTraits) lack the same try/catch guard just added to trackEvent. See inline.
📝 Walkthrough
  • api.tstrackEvent hardening — The entire body is wrapped in try/catch, logging on failure. Prevents analytics errors from propagating to callers.
  • api.tstrackTraitsOnce — New sibling of trackTraits using Amplitude's Identify.setOnce instead of .set, so a trait value is written only on first exposure.
  • GettingStartedGate.tsx — Switches from trackTraits to trackTraitsOnce for the onboarding_variant trait, ensuring the variant is pinned and cannot be overwritten on revisit.
🧪 How to verify
  1. Sign up for a new account, reach the onboarding gate, and confirm the onboarding_variant user property appears in Amplitude.
  2. Revisit the getting-started page and verify the property value does not change (setOnce semantics).
  3. Temporarily throw inside amplitude.track (or break the GA config) and confirm the caller (e.g. the bootstrap milestone events) does not crash — the error should appear in the console instead.
  4. Run npm run test in frontend/ — unit tests should pass (CI confirms).

Automate: A unit test for trackEvent that stubs amplitude.track to throw and asserts no exception propagates would lock in the try/catch behaviour permanently.

Product take: Solid quality-of-life improvement for analytics reliability. Pinning the onboarding variant with setOnce prevents silent data corruption in the analytics funnel, and the try/catch on trackEvent is basic hygiene that protects every caller. Minor impact individually, but both reduce the chance of silent analytics failures or misleading data.

🧭 Assumptions & unverified claims
  • The review assumes Identify.setOnce is supported by the pinned Amplitude SDK version (@amplitude/analytics-browser). Node modules were not available to confirm, but setOnce is a long-standing Amplitude Identify API (unverified against the exact pinned version).

A try/catch and a setOnce walk into a bar — one catches everything, the other remembers only the first drink. · reviewed at 1707416

@Zaimwa9

Zaimwa9 commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

@themis-blindfold is there still anything left to address from the #7960 pull request, related to the use of the feature flag ?

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 9bce5b1b-ba3d-43fa-8949-7a30bd203294

📥 Commits

Reviewing files that changed from the base of the PR and between c67bc32 and 1707416.

📒 Files selected for processing (2)
  • frontend/web/components/pages/onboarding/GettingStartedGate.tsx
  • frontend/web/project/api.ts

Comment thread frontend/web/project/api.ts
Comment thread frontend/web/project/api.ts
@themis-blindfold

Copy link
Copy Markdown

I don't have access to the #7960 conversation, but the PR description for this PR (#8061) explicitly names two deferred feature-flag items from #7960. Both are resolved in the current code:

  1. setOnce for onboarding_variant (Wadii's note) — done. GettingStartedGate.tsx:14 calls API.trackTraitsOnce({ onboarding_variant: variant }), which uses the new trackTraitsOnce helper (api.ts:361-368) backed by Amplitude's setOnce. The variant is pinned on first exposure and can't be overwritten on later loads.

  2. trackEvent error safety (Kyle's note) — done. The entire trackEvent body is now wrapped in a try/catch (api.ts:312-339), so a failing tracker can't break callers like the bootstrap milestone events.

The feature-flag wiring itself (onboarding_quickstart_flow) also looks clean:

  • Single source of truth in getOnboardingVariant.ts:6 — one getFlagsmithHasFeature call, exported as typed helpers (getOnboardingVariant, isSinglePageOnboarding).
  • Gate in GettingStartedGate.tsx:17 — renders OnboardingFlow or GettingStartedPage based on the helper.
  • E2e tests (initialise-tests.pw.ts:21, onboarding-tests.pw.ts:17) correctly skip/run based on the same flag.
  • No raw 'onboarding_quickstart_flow' string literals elsewhere — all consumers go through the helpers.

If there were other flag-related items raised on #7960 beyond those two, I'd need the original thread to check — but the two items this PR was opened to address are both resolved.

Match the try/catch just added to trackEvent (and the existing identify
helper): amplitude.identify() is called from a useEffect in
GettingStartedGate, so a failing tracker shouldn't propagate into the
component lifecycle.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

front-end Issue related to the React Front End Dashboard refactor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants