Skip to content
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

[React RUM] Ignore element.id when selecting DOM source #210

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module.exports = {
rules: {
'import/extensions': [2, 'ignorePackages'],
'import/prefer-default-export': 0,
'no-underscore-dangle': ['error', { allow: ['_reactRootContainer'] }],
},
globals: {
__rootdir: true,
Expand Down
4 changes: 3 additions & 1 deletion modules/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import { isReactApp } from './utils.js';

export const getTargetValue = (element) => element.getAttribute('data-rum-target') || element.getAttribute('href')
|| element.currentSrc || element.getAttribute('src') || element.dataset.action || element.action;

Expand Down Expand Up @@ -45,7 +47,7 @@ export const sourceSelector = (element) => {
}

const blockName = element.closest('.block') ? element.closest('.block').getAttribute('data-block-name') : '';
if (element.id || formElementSelector) {
if ((element.id && !isReactApp()) || formElementSelector) {
const id = element.id ? `#${element.id}` : '';
return blockName ? `.${blockName} ${formElementSelector}${id}` : `${formElementSelector}${id}`;
}
Expand Down
14 changes: 14 additions & 0 deletions modules/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,17 @@ export const urlSanitizers = {
return `${u.origin}${u.pathname}`;
},
};

/**
* Determines if the current page is running a React application
* by inspecting React-related elements in the DOM.
* @returns {bool}
*/
export const isReactApp = () => {
// https://gist.github.com/rambabusaravanan/1d594bd8d1c3153bc8367753b17d074b
if (!!window.React
|| !!document.querySelector('[data-reactroot], [data-reactid]')
|| Array.from(document.querySelectorAll('*')).some((e) => e._reactRootContainer !== undefined || Object.keys(e).some((k) => k.startsWith('__reactContainer')))
) return true;
return false;
};