-
Notifications
You must be signed in to change notification settings - Fork 53
[RUM-11519] Add RUM Action Tracking configuration options to the Babel Plugin #968
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
Merged
cdn34dd
merged 6 commits into
develop
from
carlosnogueira/RUM-11519/babel-plugin-rum-action-tracking-config-options
Sep 4, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fc48905
Prevent node_modules files from being affected the babel plugin
cdn34dd 71f182c
Ensure babel plugin is initialized once per platform
cdn34dd ea3fb09
Update babel plugin types & constants to accommodate the new RUM Acti…
cdn34dd d165770
Add Babel config options for RUM Action Tracking & support content ex…
cdn34dd 936a45b
Update Babel Plugin's unit tests to accommodate for plugin options
cdn34dd 9ab4982
Update Babel plugin's README.md to include new configuration options
cdn34dd 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
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
146 changes: 146 additions & 0 deletions
146
packages/core/src/rum/instrumentation/interactionTracking/ddBabelUtils.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,146 @@ | ||
| import * as React from 'react'; | ||
|
|
||
| type ExtractChild = | ||
| | string | ||
| | number | ||
| | boolean | ||
| | React.ReactElement | ||
| | Iterable<React.ReactNode> | ||
| | React.ReactPortal; | ||
|
|
||
| const LABEL_PROPS = ['children', 'label', 'title', 'text']; | ||
|
|
||
| const normalize = (s: string) => s.replace(/\s+/g, ' ').trim(); | ||
|
|
||
| /** | ||
| * Extracts readable text from arbitrary values commonly found in React trees. | ||
| * | ||
| * @param node - Any value: primitives, arrays, iterables, functions, or React elements. | ||
| * @param prefer - Optional list of preferred values (e.g., title/label) to attempt first. | ||
| * @returns Array of strings. | ||
| */ | ||
| export function __ddExtractText(node: any, prefer?: any[]): string[] { | ||
| // If caller provided preferred values (title/label/etc.), use those first. | ||
| if (Array.isArray(prefer)) { | ||
| const preferred = prefer | ||
| .flatMap(v => __ddExtractText(v)) // recurse so expressions/arrays work | ||
| .map(normalize) | ||
| .filter(Boolean); | ||
|
|
||
| if (preferred.length) { | ||
| return preferred; | ||
| } | ||
| } | ||
|
|
||
| // Base cases | ||
| if (node == null || typeof node === 'boolean') { | ||
| return []; | ||
| } | ||
|
|
||
| if (typeof node === 'string' || typeof node === 'number') { | ||
| return [normalize(String(node))]; | ||
| } | ||
|
|
||
| // Arrays / iterables → flatten results (don’t concatenate yet) | ||
| if (Array.isArray(node)) { | ||
| return node | ||
| .flatMap(x => __ddExtractText(x)) | ||
| .map(normalize) | ||
| .filter(Boolean); | ||
| } | ||
|
|
||
| if (typeof node === 'object' && Symbol.iterator in node) { | ||
| return Array.from(node as Iterable<any>) | ||
| .flatMap(x => __ddExtractText(x)) | ||
| .map(normalize) | ||
| .filter(Boolean); | ||
| } | ||
|
|
||
| // Zero-arg render prop | ||
| if (typeof node === 'function' && node.length === 0) { | ||
| try { | ||
| return __ddExtractText(node()); | ||
| } catch { | ||
| return []; | ||
| } | ||
| } | ||
cdn34dd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // React elements | ||
| if (React.isValidElement(node)) { | ||
| const props: any = (node as any).props ?? {}; | ||
|
|
||
| // If the element itself has a direct label-ish prop, prefer it. | ||
| for (const propKey of LABEL_PROPS) { | ||
| if (propKey === 'children') { | ||
| continue; // handle children below | ||
| } | ||
|
|
||
| const propValue = props[propKey]; | ||
| if (propValue != null) { | ||
| const got = __ddExtractText(propValue) | ||
| .map(normalize) | ||
| .filter(Boolean); | ||
|
|
||
| if (got.length) { | ||
| return got; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Inspect children. Decide whether to return ONE joined label or MANY. | ||
| const rawChildData = (Array.isArray(props.children) | ||
| ? props.children | ||
| : [props.children]) as ExtractChild[]; | ||
|
|
||
| const children = rawChildData.filter(c => c != null && c !== false); | ||
|
|
||
| if (children.length === 0) { | ||
| return []; | ||
| } | ||
|
|
||
| // Extract each child to a list of strings (not joined) | ||
| const perChild = children.map(child => __ddExtractText(child)); | ||
|
|
||
| // Heuristic: treat as *compound* if multiple children look like “items” | ||
| // e.g., at least two direct children have a label-ish prop or yield non-empty text individually. | ||
| let labeledChildCount = 0; | ||
| children.forEach((child, i) => { | ||
| let hasLabelProp = false; | ||
|
|
||
| if (React.isValidElement(child)) { | ||
| const childProps: any = (child as any).props ?? {}; | ||
| hasLabelProp = LABEL_PROPS.some(k => childProps?.[k] != null); | ||
| } | ||
|
|
||
| const childTextCount = perChild[i].filter(Boolean).length; | ||
| if (hasLabelProp || childTextCount > 0) { | ||
| labeledChildCount++; | ||
| } | ||
| }); | ||
|
|
||
| const flat = perChild.flat().map(normalize).filter(Boolean); | ||
|
|
||
| // If there are multiple *direct* labelled children, return many (compound). | ||
| // Otherwise, return a single joined label. | ||
| if (labeledChildCount > 1) { | ||
| // De-duplicate while preserving order | ||
| const seen = new Set<string>(); | ||
| const out: string[] = []; | ||
|
|
||
| for (const str of flat) { | ||
| const key = str; | ||
| if (!seen.has(key)) { | ||
| seen.add(key); | ||
| out.push(str); | ||
| } | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| // Not “compound”: join everything into one readable string | ||
| const joined = normalize(flat.join(' ')); | ||
| return joined ? [joined] : []; | ||
| } | ||
|
|
||
| return []; | ||
| } | ||
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.