refactor: convert app/scripts/lib/segment/analytics.js to TypeScript#41476
refactor: convert app/scripts/lib/segment/analytics.js to TypeScript#41476DDDDDanica merged 5 commits intomainfrom
Conversation
Agent-Logs-Url: https://github.com/MetaMask/metamask-extension/sessions/c41c9e1c-fd6b-4210-9509-9f24baf0d610 Co-authored-by: DDDDDanica <12678455+DDDDDanica@users.noreply.github.com>
Builds ready [d380bd7]
⚡ Performance Benchmarks (Total: 🟢 6 pass · 🟡 12 warn · 🔴 0 fail)
🌐 Dapp Page Load BenchmarksCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
Bundle size diffs
|
|
CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes. |
Builds ready [183cf0f]
⚡ Performance Benchmarks (Total: 🟢 6 pass · 🟡 12 warn · 🔴 0 fail)
🌐 Dapp Page Load BenchmarksCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
Bundle size diffs
|
Builds ready [160d8b1]
⚡ Performance Benchmarks (Total: 🟢 7 pass · 🟡 12 warn · 🔴 0 fail)
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
|
Builds ready [8565b08]
⚡ Performance Benchmarks (Total: 🟢 7 pass · 🟡 12 warn · 🔴 0 fail)
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
| * @property {number} [flushAt] (default: 20) | ||
| * @property {number} [flushInterval] (default: 10000) | ||
| * @property {string} [host] (default: 'https://api.segment.io') | ||
| * @param writeKey - The Segment project write key. |
There was a problem hiding this comment.
Q: still need JSDocs with these?



[skip-e2e]
Converts
app/scripts/lib/segment/analytics.jsand its test file to TypeScript as part of the ongoing TS migration, removing both entries fromfiles-to-convert.json.Changes
analytics.ts— Full conversion with typed interfaces (AnalyticsMessage,AnalyticsOptions,AnalyticsCallback,QueueItem,FlushData,RetryableError). Also fixes a latent bug influshAtinitialization: the original JS relied onNaN || 20as the undefined fallback, which breaks when naively rewritten with??:analytics.test.ts— Converted test file;global.setImmediateandglobal.fetchoverrides cast viaas anyto satisfy Node type constraints.types/segment.d.ts— New declaration file providing module types for three untyped packages:@segment/loosely-validate-event,remove-trailing-slash,is-retry-allowed.metametrics-controller.test.ts— TwomockImplementationcalls requiredas anycast since the stub(...args: unknown[]) => voidno longer matches the now-typedAnalytics.track/.pagereturn signatures.Note
Medium Risk
Touches the in-house Segment batching client used for telemetry delivery, including default
flushAtinitialization and retry/timer handling, so mistakes could drop or delay metrics despite being a mostly type-focused refactor.Overview
Converts the Segment
Analyticsbatching client from JS to typedanalytics.ts, adding explicit message/queue/flush types and fixing theflushAtdefaulting logic (ensuringundefinedstill defaults to20while0clamps to1). It also formalizes timer tracking (timer) and tightens retry/error typing in_sendRequest/_isErrorRetryable.Replaces
analytics.test.jswith a more comprehensiveanalytics.test.tscovering host normalization, queue flushing behavior, timer clearing, payload normalization for non-string IDs, and retry/non-retry error paths. Updatesmetametrics-controller.test.tsmocks to match the now-typedAnalytics.track/pagesignatures, addstypes/segment.d.tsfor untyped dependencies, and removes the converted files fromfiles-to-convert.json.Reviewed by Cursor Bugbot for commit 8565b08. Bugbot is set up for automated code reviews on this repo. Configure here.