我一直在尝试React和Azure App Insights。
const appInsights = useAppInsightsContext();
仅对于事件和度量,似乎有两种处理方法。为什么会这样?为什么只有这2件事,只有页面浏览量或例外,你只能使用第二种方式(appInsights。trackPageView appInsights.trackException)
//第一个方法const trackEventHook = useTrackEvent(
appInsights,
"AppInsightsPage Track Event Hook",
{ extraData: "some extra data important to this" },
false
);
trackEventHook({ extraData: "function call extra data" });
//2方法appInsights.trackEvent({ name: "AppInsightsPage Custom Event" }, undefined);
在使用Application Insight时,我们在代码中使用TrackEvent来计算各种事件。用户选择特定功能的频率,或者他们做出特定选择的频率。
例如,我们想了解用户在网站上的行为,我们想知道具体的操作,比如点击添加到购物车按钮。
这可以通过两种方式完成:
使用trackEvent Method
appInsights.trackEvent({ name: 'EventName', properties: { anyProperty } })
我们使用要导出的appInsights对象,并将一些数据传递给trackEvent(事件的名称)我们正在跟踪和任何自定义属性我们想在事件中包含。
使用React Plugin useTrackEvent钩子
const trackEventName = useTrackEvent(appInsights, "Event Name", condition);
useTrackEvent
Hook用于跟踪应用程序可能需要跟踪的任何自定义事件,例如按钮单击或其他API调用。它接受四个参数:
- Application Insights实例(可以从
useAppInsightsContext
Hook获得)。 - 事件名称。
- 事件数据对象,封装必须跟踪的更改。
- skipfirstrn(可选)标志,在初始化时跳过调用
trackEvent
调用。 默认为
true
。trackExpection用于记录与API相关的异常,我们不知道何时会发生,对于trackPageView,默认情况下,在加载每个屏幕或页面时发送页面视图遥测。因此,在trackexpect中和trackPageView我们没有任何数据对象来跟踪任何更改。这就是为什么我们不使用useTrackEvent
有关更多信息,请查看以下Microsoft文档:
- React plugin for Application Insights.
- Application Insight API.