Skip to content

✨ Add url option to startView API#4199

Merged
BeltranBulbarellaDD merged 4 commits into
DataDog:mainfrom
brettburley:feat/startview-url-option
Mar 6, 2026
Merged

✨ Add url option to startView API#4199
BeltranBulbarellaDD merged 4 commits into
DataDog:mainfrom
brettburley:feat/startview-url-option

Conversation

@brettburley
Copy link
Copy Markdown
Contributor

Motivation

The startView() API currently always captures the URL from window.location.href at the moment it's called. This is a problem for frameworks like React Router v7 in framework mode (and similar async routing setups) where loaders/actions run before window.location is updated. This means the view gets recorded with the previous page's URL, leading to incorrect view data in RUM.

Today, ViewOptions accepts name, service, version, and context, but not url, so there is no way to customize this behavior without adding some non-trivial logic in the beforeSend hook.

Changes

Adds an optional url field to ViewOptions so callers can explicitly provide the destination URL when the browser hasn't updated window.location yet. Falls back to location.href when not provided (no behavior change for existing users).

Usage:

DD_RUM.startView({
  name: '/dashboard',
  url: 'https://myapp.com/dashboard',
})

Test instructions

  1. Call startView({ name: 'test', url: 'https://example.com/custom-path' }) and verify the view event has view.url set to the provided URL instead of window.location.href
  2. Call startView('test') without a URL and verify existing behavior is unchanged (uses location.href)

Checklist

  • Tested locally
  • Tested on staging
  • Added unit tests for this change.
  • Added e2e/integration tests for this change.
  • Updated documentation and/or relevant AGENTS.md file

@brettburley brettburley requested a review from a team as a code owner February 17, 2026 22:55
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 17, 2026

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@brettburley brettburley force-pushed the feat/startview-url-option branch from 7e83510 to 37f000a Compare February 17, 2026 23:03
@brettburley
Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

Allow passing an explicit URL when calling startView() so frameworks
like React Router v7 (framework mode) that update the URL after
loaders complete can provide the correct destination URL upfront.

Falls back to location.href when no url is provided.
@BeltranBulbarellaDD BeltranBulbarellaDD force-pushed the feat/startview-url-option branch from 37f000a to 4356546 Compare March 5, 2026 13:39
@BeltranBulbarellaDD
Copy link
Copy Markdown
Contributor

@brettburley thanks for the contribution!. I'll take over the PR to add some tests and run the CI so we can merge it.

@datadog-datadog-prod-us1
Copy link
Copy Markdown

datadog-datadog-prod-us1 Bot commented Mar 5, 2026

🎯 Code Coverage (details)
Patch Coverage: 66.67%
Overall Coverage: 76.87% (-0.08%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 9132945 | Docs | Datadog PR Page | Was this helpful? React with 👍/👎 or give us feedback!

…wUrl

add undefined url test

add test resource also gets the manual url
@BeltranBulbarellaDD BeltranBulbarellaDD force-pushed the feat/startview-url-option branch from 109262d to 7f6ff27 Compare March 5, 2026 14:49
Copy link
Copy Markdown
Collaborator

@bcaudan bcaudan left a comment

Choose a reason for hiding this comment

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

LGTM

A small note on public API doc

Image

We should probably tweak it to:

  • not misleading people to think that there is only name, service and version configurable
  • not list every options available here

Copy link
Copy Markdown
Contributor

@mormubis mormubis left a comment

Choose a reason for hiding this comment

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

No URL validation

The url field goes through unvalidated — if someone passes /dashboard instead of a full URL, view.url in the RUM event ends up as an origin-less string. The existing window.location.href path always gives a fully-qualified URL, so this quietly breaks in a way that didn't exist before.

Could normalize with new URL(url, location.href).href to handle relative URLs gracefully, or at least warn + fall back to location.href when the value isn't absolute.

@BeltranBulbarellaDD BeltranBulbarellaDD merged commit a8fbcae into DataDog:main Mar 6, 2026
21 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 6, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants