question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React: User Interaction only returns "Click - div"

See original GitHub issue

This issue is possibly related to the intended fix from PR elastic/apm-agent-rum-js#951, however applying the changes there do not resolve the below issue for me.

Whenever a target is clicked, the USER_INTERACTION event is fired. As an example button:

<button name="applyFiltersBtn" class="ui icon primary left labeled button">
  <i aria-hidden="true" class="check icon"></i>
  Apply
</button>

This then fires the following log:

[Elastic APM] startTransaction(86115b370489ca7c, Click - div, user-interaction)

As you can see, the name of this transaction is titled Click - div, even though the (what looks like) intended behaviour of performance-monitoring.js:189 is to grab the name attribute from the element, in this case applyFiltersBtn. I’d expect this transaction to be named Click - button["applyFiltersBtn"] and for this to be recorded in the APM under a “user-interaction”, whilst capturing network traffic to include spans such as API requests, etc.

When doing some debugging, I logged out what was being passed to shouldInstrumentEvent (or from elastic/apm-agent-rum-js#951, I have this applied change, along with the other changes from the PR). When logging the target variable from the shouldInstrumentEvent function and also logging the target from the getEventTargetSub, in all cases both parts of the aforementioned give back one of the following:

  • <div id="root">
  • Window
  • <react><react/>.

It never gives back the actual target of (in the example above) a Button. Is this intentional, if so, how would one implement a transaction to follow up on user interactions with buttons and log them correctly in APM, with React?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:22 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
vigneshshanmugamcommented, Mar 5, 2021

We can fix this today by using the technique described here - https://github.com/elastic/apm-agent-rum-js/pull/951/files#r588056071. the resulting value of transaction would be Click - button["applyFiltersBtn"]

I would add this check to the PR and it should be good to go.

1reaction
vigneshshanmugamcommented, Mar 17, 2021

@reecebenson Thanks for the extensive test. I did a quick look around the new React 17 event system. It seems like React is registering two click events on the application root where the react app is mounted One with useCapture flag set to true and other with false.

As a result, I can see your test producing redefining transactions with no changes and also document.activeElement also not having the correct focus.

I am still doing some testing to figure out what would be the best solution here.

@clyfish Not sure why you closed the PR. Using window.onclick is easier, but it does not guarantee that

  • our listeners always fires before everyone elses
  • We have to also account for events that have been explicity called with e.stopPropagation or e.preventDefault.

So if we can fix it using eventtarget, it would be a better solution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Show a returned object on a div when button is clicked using ...
The simplest solution will be to use a flag to render the component and toggle the flag when the user clicks on the...
Read more >
Handling Events - React
Handling events with React elements is very similar to handling events on DOM elements. There are some syntax differences: React events are named...
Read more >
How to create a button with a div in React (and why you ...
1. onClick handler ... This is the obvious first step to add an interaction to a div . But, as its name suggests,...
Read more >
React onClick event handlers: A complete guide
The React onClick event handler enables you to call a function and trigger an action when a user clicks an element, such as...
Read more >
How To Handle DOM and Window Events with React
In React apps, you can use event handlers to update state data, trigger prop changes, or prevent default browser actions. To do this,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found