在 Azure Web App 中使用的 React 应用的用户信息

user information of react app using in azure web app

提问人:azuremv 提问时间:11/9/2023 最后编辑:Harshithaazuremv 更新时间:11/10/2023 访问量:126

问:

我有一个 react 应用程序,它包含多个页面,该页面包含不同的信息。若要托管此 react 应用,请创建 azure web 应用,它工作正常。现在这个应用程序可供用户使用。 所以我的问题是我想知道谁访问了该应用程序,哪个用户访问了该应用程序中的哪个页面,以及用户访问该页面的次数。是否可以在 Web 应用程序中获取此信息?如果是,我在哪里可以得到这个?

我尝试了 Web 应用程序中的多个日志,但它显示没有找到结果。

enter image description here

不确定,但检查此 Web 应用程序的 Application Insights 以获取此用户相关的 KPI,如图所示,这是我可以看到的

enter image description here

我正在检查 WebApp 的日志和应用程序见解,但没有运气。

请让我知道如何获取用户信息,例如谁访问了该页面以及访问了该页面的次数。

azure-log-analytics azure-appservice azure-webapps

评论


答:

0赞 Sampath 11/10/2023 #1
  • 若要跟踪 Web 应用程序的用户详细信息,需要在 Web 应用中添加 Application Insights。

  • 在应用服务器代码和网页中安装 Application Insights。npm install @microsoft/applicationinsights-web

  • 使用用户、会话和事件细分工具筛选和拆分数据,以发现有关不同页面和功能的相对使用情况的见解。

  • 下面的代码使用 和 包将 Azure Application Insights 集成到 React 应用程序中,以跟踪事件和用户行为。@microsoft/applicationinsights-web@microsoft/applicationinsights-react-js

  • 创建 Azure Application Insights 和 Azure Application Insights 检测密钥

法典:

// src/AppInsights.js

import { ApplicationInsights } from '@microsoft/applicationinsights-web';
import { ReactPlugin } from '@microsoft/applicationinsights-react-js';
import { createBrowserHistory } from 'history';

const browserHistory = createBrowserHistory({ basename: '' });
const reactPlugin = new ReactPlugin();
const appInsights = new ApplicationInsights({
  config: {
    instrumentationKey:  'Azure Application Insights Instrumentation Key',
    extensions: [reactPlugin],
    extensionConfig: {
      [reactPlugin.identifier]: { history: browserHistory },
    },
  },
});

appInsights.loadAppInsights();

export { reactPlugin, appInsights };

// src/App.js

import React, { useEffect } from 'react';
import { reactPlugin } from './AppInsights';
import { withAITracking } from '@microsoft/applicationinsights-react-js';

class MyComponent extends React.Component {
  componentDidMount() {
    // Example of tracking a custom event
    reactPlugin.trackEvent({ name: 'MyComponent Mounted' });
  }

  componentWillUnmount() {
    // Example of tracking a custom event when component unmounts
    reactPlugin.trackEvent({ name: 'MyComponent Unmounted' });
  }

  render() {
    return <div>Hello from MyComponent!</div>;
  }
}

// Wrap your component with withAITracking to enable tracking
const MyTrackedComponent = withAITracking(reactPlugin, MyComponent);

// Use a functional component to demonstrate useEffect
const App = () => {
  useEffect(() => {
    // Example of tracking a page view
    reactPlugin.trackPageView({ name: 'App Component Page View' });
  }, []);

  return (
    <div>
      <h1>Your React Application</h1>
      <MyTrackedComponent />
    </div>
  );
};

export default App;

输出:

enter image description here

enter image description here

在用户中:

enter image description here

在日志中:

enter image description here

会议和活动:

enter image description here

评论

0赞 azuremv 11/15/2023
谢谢。您能否在日志中提供您正在使用哪个查询来获得该结果。您能否提供该查询