Skip to content

Commit ebbd9a9

Browse files
authored
Merge pull request #190176 from pizzaz93/patch-3
Update javascript-react-plugin.md
2 parents 35fe472 + acd9a3d commit ebbd9a9

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

articles/azure-monitor/app/javascript-react-plugin.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@ npm install @microsoft/applicationinsights-react-js @microsoft/applicationinsigh
3131
Initialize a connection to Application Insights:
3232

3333
```javascript
34-
// AppInsights.js
34+
import React from 'react';
3535
import { ApplicationInsights } from '@microsoft/applicationinsights-web';
36-
import { ReactPlugin } from '@microsoft/applicationinsights-react-js';
37-
import { createBrowserHistory } from 'history';
38-
36+
import { ReactPlugin, withAITracking } from '@microsoft/applicationinsights-react-js';
37+
import { createBrowserHistory } from "history";
3938
const browserHistory = createBrowserHistory({ basename: '' });
40-
const reactPlugin = new ReactPlugin();
41-
const appInsights = new ApplicationInsights({
39+
var reactPlugin = new ReactPlugin();
40+
var appInsights = new ApplicationInsights({
4241
config: {
4342
instrumentationKey: 'YOUR_INSTRUMENTATION_KEY_GOES_HERE',
4443
extensions: [reactPlugin],
@@ -48,7 +47,6 @@ const appInsights = new ApplicationInsights({
4847
}
4948
});
5049
appInsights.loadAppInsights();
51-
export { reactPlugin, appInsights };
5250
```
5351

5452
Wrap your component with the higher-order component function to enable Application Insights on it:
@@ -71,6 +69,21 @@ class MyComponent extends React.Component {
7169
export default withAITracking(reactPlugin, MyComponent);
7270
```
7371

72+
For `react-router v6` or other scenarios where router history is not exposed, appInsights config `enableAutoRouteTracking` can be used to auto track router changes:
73+
74+
```javascript
75+
var reactPlugin = new ReactPlugin();
76+
var appInsights = new ApplicationInsights({
77+
config: {
78+
instrumentationKey: 'YOUR_INSTRUMENTATION_KEY_GOES_HERE',
79+
enableAutoRouteTracking: true,
80+
extensions: [reactPlugin]
81+
}
82+
}
83+
});
84+
appInsights.loadAppInsights();
85+
```
86+
7487
## Configuration
7588

7689
| Name | Default | Description |

0 commit comments

Comments
 (0)