Skip to content

chore(analytics-browser): add platform and web_environment tags to diagnostics - #1914

Merged
Mercy811 merged 5 commits into
mainfrom
xinyiye/sdkw-47-diagnostics-add-platform-web
Jul 31, 2026
Merged

chore(analytics-browser): add platform and web_environment tags to diagnostics#1914
Mercy811 merged 5 commits into
mainfrom
xinyiye/sdkw-47-diagnostics-add-platform-web

Conversation

@Mercy811

@Mercy811 Mercy811 commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds two diagnostics tags on the browser SDK, set at init in browser-client.ts next to the existing library and user_agent tags:

Tag Value
platform Web — reuses the BROWSER_PLATFORM constant backing the analytics event platform field (src/plugins/context.ts), and matches the mobile diagnostics values (Swift: iOS/tvOS/visionOS/macOS/watchOS, Kotlin: Android)
web_environment browser | web_worker | service_worker | chrome_extension | chrome_extension_service_worker | node | unknown

web_environment refines platform: Web by JS runtime — each value is a distinct event-loss regime (workers have no localStorage so the event queue falls back to MemoryStorage; service workers are additionally killed after ~30s idle and can't use xhr/beacon transports). Named web_environment because Datadog already auto-tracks an environment tag. Full design incl. the API-availability-per-environment table: SDKW-47 design comment.

Detection is internal to analytics-browser (src/utils/environment.ts, getRuntimeEnvironment()) — it's only consumed here, and core stays light since it's shared by all platforms. analytics-core is untouched. Check order is load-bearing:

  1. service worker before web worker — ServiceWorkerGlobalScope inherits from WorkerGlobalScope
  2. browser after the chrome.runtime.id split — extension pages and content scripts have a document
  3. browser before node — jsdom exposes both document and process.versions.node

Out of scope

Tracking the exact event-drop reason (storage fallback, retry exhaustion, transport unsupported): SDKW-48.

Testing

  • analytics-browser — 499 tests pass, coverage 100% on all four metrics (repo gate); new suite covers every getRuntimeEnvironment branch, incl. service-worker-vs-web-worker precedence, extension variants, and exposed-but-not-instance worker constructors
  • analytics-core — untouched, 869 tests pass
  • eslint clean on changed files

Closes SDKW-47 — https://linear.app/amplitude/issue/SDKW-47/diagnostics-add-platform-web

Checklist

  • Does your PR title have the correct title format?
  • Does your PR have a breaking change?: No

🤖 Generated with Claude Code


Note

Low Risk
Observability-only metadata at init; no changes to event pipeline, storage, or transports.

Overview
Adds platform (Web, from exported BROWSER_PLATFORM) and web_environment (from new getRuntimeEnvironment()) as diagnostics tags during browser SDK init, alongside existing library and user_agent tags.

getRuntimeEnvironment() in src/utils/environment.ts classifies the JS runtime (browser, workers, Chrome extension contexts, Node, or unknown) using getGlobalScope() with an explicit check order (service worker before web worker, extension split on main thread, browser before Node for jsdom). Tests cover init tag wiring and every classification branch.

Reviewed by Cursor Bugbot for commit 806320b. Configure here.

Report `platform: "Web"` as a diagnostics tag, alongside the existing
`library` and `user_agent` tags. Reuse the `BROWSER_PLATFORM` constant
already used for the analytics event `platform` field so the two stay
in sync.

Mirrors the mobile SDKs, which set the same `platform` tag key with
"iOS"/"macOS"/"Android".

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@Mercy811
Mercy811 requested a review from a team as a code owner July 29, 2026 19:32
@linear-code

linear-code Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

SDKW-47

@Mercy811 Mercy811 changed the title feat(analytics-browser): add platform tag to diagnostics chore(analytics-browser): add platform tag to diagnostics Jul 29, 2026
@Mercy811 Mercy811 closed this Jul 29, 2026
@github-actions

github-actions Bot commented Jul 29, 2026

Copy link
Copy Markdown

size-limit report 📦

Path Size
packages/analytics-browser/lib/scripts/amplitude-min.js.gz 61.45 KB (+0.3% 🔺)
packages/session-replay-browser/lib/scripts/session-replay-browser-min.js.gz 134.97 KB (0%)
packages/unified/lib/scripts/amplitude-min.umd.js.gz 215.55 KB (+0.09% 🔺)
@amplitude/element-selector (gzipped esm) 2.67 KB (0%)

@Mercy811 Mercy811 reopened this Jul 29, 2026
Mercy811 and others added 2 commits July 30, 2026 09:54
Add isBrowser/isWebWorker (copied from the paused #1908 so the two
branches converge), plus isServiceWorker, isNode, and a
getWebEnvironment() resolver returning one of: browser, web_worker,
service_worker, chrome_extension, chrome_extension_service_worker,
node, unknown.

Check order is load-bearing: ServiceWorkerGlobalScope inherits from
WorkerGlobalScope; extension pages have a document; jsdom has both
document and process.versions.node.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Report the JS runtime environment alongside the platform tag, so
diagnostics can distinguish page / worker / service worker / extension
traffic. Named web_environment because Datadog already auto-tracks an
environment tag.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@Mercy811 Mercy811 changed the title chore(analytics-browser): add platform tag to diagnostics chore(analytics-browser): add platform and web_environment tags to diagnostics Jul 30, 2026
The helpers are only consumed by the browser SDK, and core is shared by
all platforms — keep it as light as possible. Reverts analytics-core to
untouched (which also removes the merge conflict with #1908) and keeps
getWebEnvironment() internal to analytics-browser, with predicates
taking the resolved global scope so classification reads one consistent
object.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@Mercy811

Copy link
Copy Markdown
Contributor Author

bugbot run

@cursor cursor 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.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 806320b. Configure here.

Comment thread packages/analytics-browser/src/utils/environment.ts Outdated
…vironment

Some classified environments aren't web (node), so WebEnvironment was a
misnomer. The web_environment tag key is unchanged.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@Mercy811
Mercy811 enabled auto-merge (squash) July 31, 2026 20:57
@Mercy811
Mercy811 merged commit 7ec3411 into main Jul 31, 2026
19 of 20 checks passed
@Mercy811
Mercy811 deleted the xinyiye/sdkw-47-diagnostics-add-platform-web branch July 31, 2026 20:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants