1

I've been playing around with React and Azure App Insights.

const appInsights = useAppInsightsContext();

For Events and Metrics only, there seems to be 2 ways of doing things. Why is this? And why is it only for these 2 things only ie for PageViews or exceptions you can only use the second way (appInsights.trackPageView, appInsights.trackException)

//first way

    const trackEventHook = useTrackEvent(
    appInsights,
    "AppInsightsPage Track Event Hook",
    { extraData: "some extra data important to this" },
    false
  );

trackEventHook({ extraData: "function call extra data" });

//2nd way

appInsights.trackEvent({ name: "AppInsightsPage Custom Event" }, undefined);
72GM
  • 2,926
  • 3
  • 27
  • 33

1 Answers1

0

While using Application Insight, we use TrackEvent in our code to count various events. How often users choose a particular feature or maybe how often they make particular choices.

For Example, we want to understand the user behavior on a site and we want to know about specific actions like clicking the Add to Cart button.

This can be done by two ways :

Using trackEvent Method

appInsights.trackEvent({ name: 'EventName', properties: { anyProperty } })

We use appInsights object that we are exporting and pass some data to trackEvent, the name of the event we are tracking and any custom properties we want to include in the event.

Using React Plugin useTrackEvent Hook

const trackEventName = useTrackEvent(appInsights, "Event Name", condition);

The useTrackEvent Hook is used to track any custom event that an application may need to track, such as a button click or other API call. It takes four arguments:

  1. Application Insights instance (which can be obtained from the useAppInsightsContext Hook).
  2. Name for the event.
  3. Event data object that encapsulates the changes that has to be tracked.
  4. skipFirstRun (optional) flag to skip calling the trackEvent call on initialization. Default value is set to true.

trackExpection is used to log exception which are related to API, we don't know when they will happen and for trackPageView, page view telemetry is sent by default when each screen or page is loaded. So, in trackExpection and trackPageView we don't have any data object to track any changes. That's why we don't use useTrackEvent hook for this two.

For more information please check the following Microsoft Documents:

SauravDas-MT
  • 1,224
  • 1
  • 4
  • 10
  • I think you've answered your own question here, not mine – 72GM Sep 15 '21 at 08:54
  • I described both the methods and summarized the answer in the last paragraph. Still if you are not satisfied can you please elaborate your question so that I can help. – SauravDas-MT Sep 15 '21 at 11:26
  • the question asks why are there 2 different ways of doing things for track event (and track metric)? Why would I use one over the other...I have already read the documentation... quite clearly you wouldn't use useTrackEvent for trackPageView, why is there no useTrackPageView hook? – 72GM Sep 15 '21 at 15:19