ApplicationInsights-JS icon indicating copy to clipboard operation
ApplicationInsights-JS copied to clipboard

Don't work with React HOOKS

Open ecatugy opened this issue 7 years ago • 14 comments

Please send suport for React Hooks, components class is obsolete.

ecatugy avatar Aug 02 '19 18:08 ecatugy

Please support the function components with hooks.

veerprabakar avatar Oct 02 '19 05:10 veerprabakar

Yes I would suggest a hooks implementation ASAP for react since that is the direction it's headed.

enterpriseSoftware avatar Oct 25 '19 21:10 enterpriseSoftware

wrote a custom hook to replace withAITracking, seams to work ok, i have a 'key' paramter because i have a tabbed UI in the component i am tracking, and the key is the tab state

function useAITracking(componentName, key) { useEffect( () => { let start= new Date() return () => { reactPlugin.trackMetric({ average: (new Date() - start)/1000, name: 'React Component (seconds)', sampleCount: 1 }, { 'Component Name': componentName, 'Navigation': key }); }; }, [componentName, key])

}

khowling avatar Nov 08 '19 19:11 khowling

I've done a write up on how you can create custom hooks to work with AppInsights, you'll find it here: https://dev.to/aaronpowell/combining-react-hooks-with-appinsights-5692

In it there is a Context, allowing you to useContext to access the React plugin, a hook for tracking a normal event (using trackEvent for AppInsights) and one for doing metric tracking like the HOC does.

Does it look like what people would expect from a programmatic model of AppInsights + Hooks?

aaronpowell avatar Nov 19 '19 03:11 aaronpowell

Included in v 2.5.5 which is now fully deployed

MSNev avatar Jun 04 '20 19:06 MSNev

@aaronpowell @MSNev is there any official documentation on how to use the new stuff for react hooks?

fab7jj avatar Oct 22 '20 13:10 fab7jj

I second this question. Is there any documentation?

ghost avatar Nov 19 '20 20:11 ghost

Docs have been written and are currently being reviewed by our content team. Fingers crossed they should be available soon.

aaronpowell avatar Nov 19 '20 23:11 aaronpowell

Docs are now published - https://docs.microsoft.com/en-us/azure/azure-monitor/app/javascript-react-plugin#using-react-hooks

aaronpowell avatar Nov 22 '20 23:11 aaronpowell

thanks for this!

ghost avatar Dec 14 '20 07:12 ghost

@aaronpowell It would be great to have those docs in the react Readme.md as well

dps88 avatar Jan 09 '21 01:01 dps88

Good suggestion @dps88. I've opened a PR to add it.

aaronpowell avatar Jan 11 '21 05:01 aaronpowell

I have been attempting to get the Hooks based implementation to work in one of my current apps and I am having a few issues being certain if it is working as intended.

Setup MS AppInsights + React-Plugin https://docs.microsoft.com/en-us/azure/azure-monitor/app/javascript-react-plugin#basic-usage

Context Usage Within my component, I accessing the AppInsights context, and then simply want to track metrics at my page component level. To do that I followed the exact recommendation listed in the documentation (linked above)

  const metricData = {
    average: 10, // Not explained in the docs, so I just put this as a 1 as I assume it is up to us to define a mechanism for calculating this client-side?
    name: 'React Component Engaged Time (seconds)',
    sampleCount: 1
  };
  const additionalProperties = { 'Component Name': 'ListView' };
  appInsights.trackMetric(metricData, additionalProperties);

In the console, I see the "track" network request, but it seems like the "additionalProperties" are not logged or tracked. This makes it impossible to do any splitting or aggregation on the AI side.

image

So I feel like Im on the right track, but I must be missing something. Additonally, I tried following the user-invoked useTrackMetric approach, though this did not seem to work at all. Manually triggering the callback provided via useTrackMetric did to appear to trigger any network traffic / tracking

HJordan35 avatar Nov 15 '21 20:11 HJordan35

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Nov 16 '22 00:11 github-actions[bot]