@equinor/mad-insights 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/CODE_OF_CONDUCT.md +49 -0
- package/LICENSE +21 -0
- package/README.md +205 -0
- package/dist/appInsights.d.ts +108 -0
- package/dist/appInsights.js +37 -0
- package/dist/chunk-2YSWHN2G.js +15 -0
- package/dist/chunk-GUSP74WR.js +38 -0
- package/dist/chunk-PACV3Q7O.js +195 -0
- package/dist/encrypt.d.ts +13 -0
- package/dist/encrypt.js +6 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +41 -0
- package/dist/useAppInsights.d.ts +17 -0
- package/dist/useAppInsights.js +8 -0
- package/package.json +51 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# How we work
|
|
2
|
+
|
|
3
|
+
In Equinor, [how we deliver is as important as what we
|
|
4
|
+
deliver](https://www.equinor.com/en/careers/our-culture.html).
|
|
5
|
+
|
|
6
|
+
This set the expectations for how we collaborate in our open source projects,
|
|
7
|
+
and applies to all community members and participants in any Equinor maintained
|
|
8
|
+
project.
|
|
9
|
+
|
|
10
|
+
## Handling issues within the communities
|
|
11
|
+
|
|
12
|
+
We expect all to have a low threshold for raising issues, or in general discuss
|
|
13
|
+
how we live up to our
|
|
14
|
+
[values](https://www.equinor.com/en/about-us.html#our-values). Equally, we also
|
|
15
|
+
encourage all community members to appreciate when concerns are raised, and make
|
|
16
|
+
its best effort in solving them.
|
|
17
|
+
|
|
18
|
+
As well as responsibility for what is delivered the project maintainers are also
|
|
19
|
+
responsible creating an environment for proper handling of issues raised within
|
|
20
|
+
the communities.
|
|
21
|
+
|
|
22
|
+
## Call out for assistance
|
|
23
|
+
|
|
24
|
+
For any problem not directly resolvable within the community, we encourage you
|
|
25
|
+
to call out for assistance. Getting an outsiders perspective on the topic might
|
|
26
|
+
be just what is needed for you to proceed.
|
|
27
|
+
|
|
28
|
+
Send an e-mail to opensource_at_equinor.com and invite for a discussion. The
|
|
29
|
+
email will be handled by a team within the Equinor organization.
|
|
30
|
+
|
|
31
|
+
Your request will be kept confidential from the team or community in question,
|
|
32
|
+
unless you chose to disclose it yourself.
|
|
33
|
+
|
|
34
|
+
## Ethics helpline
|
|
35
|
+
|
|
36
|
+
In Equinor, we want you to speak up whenever you see unethical behaviour that
|
|
37
|
+
conflicts with our values or threatens our reputation.
|
|
38
|
+
|
|
39
|
+
To underline this, we continuously encourage and remind our employees and any
|
|
40
|
+
external third parties interacting with us to raise concerns or report any
|
|
41
|
+
suspected or potential breaches of law or company policies.
|
|
42
|
+
|
|
43
|
+
For any questions or issues that one suspect falls outside the scope of
|
|
44
|
+
behaviour regulated, and handled within the open source code of conduct, or you
|
|
45
|
+
wish to place an anonymous, confidential report we encourage to use the [Equinor
|
|
46
|
+
Ethics
|
|
47
|
+
helpline](https://secure.ethicspoint.eu/domain/media/en/gui/102166/index.html).
|
|
48
|
+
|
|
49
|
+
This helpline is hosted by a third party helpline provider.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Equinor ASA
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
# MAD Insights
|
|
2
|
+
|
|
3
|
+
MAD insights is an easy to use package for tracking events in application insights.
|
|
4
|
+
|
|
5
|
+
### Before implementation
|
|
6
|
+
|
|
7
|
+
You need a resource in Azure you can use. Head to
|
|
8
|
+
[https://portal.azure.com](https://portal.azure.com), and head to AppInsights. Search for your
|
|
9
|
+
application and see if it already has a resource you can use. If not, create one for each
|
|
10
|
+
environment (dev, test, qa, prod). If your project wants to implement long term logging, you need an
|
|
11
|
+
additional resource for each environment.
|
|
12
|
+
|
|
13
|
+
In order to connect to AppInsights, you need the instrumentation key or connection string from a
|
|
14
|
+
resource. instrumentation keys are shorter, but might be deprecated in the future.
|
|
15
|
+
|
|
16
|
+
**_IMPORTANT: for any custom tracking/logging you add to your app, please make sure you don_**’**_t
|
|
17
|
+
log sensitive information. Please also keep this in mind when looking at pull requests related to
|
|
18
|
+
tracking._**
|
|
19
|
+
|
|
20
|
+
### Implementation
|
|
21
|
+
|
|
22
|
+
Install `@equinor/mad-insights` and `react-native-device-info`.
|
|
23
|
+
|
|
24
|
+
Then, you need to initiate appInsights on app startup. This can be done with `useAppInsights` in
|
|
25
|
+
`App.tsx`:
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
import { useAppInsights } from "@equinor/mad-insights";
|
|
29
|
+
|
|
30
|
+
export default function App() {
|
|
31
|
+
useAppInsights({
|
|
32
|
+
instrumentationKey: "KEY FROM ENV CONFIG",
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
return <>...</>;
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
If you want to use connection string instead, replace instrumentationKey with connectionString:
|
|
40
|
+
|
|
41
|
+
```tsx
|
|
42
|
+
import { useAppInsights } from "@equinor/mad-insights";
|
|
43
|
+
|
|
44
|
+
export default function App() {
|
|
45
|
+
useAppInsights({
|
|
46
|
+
connectionString: "CONNECTION STRING FROM ENV CONFIG",
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
return <>...</>;
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
If you need a long term log, you can add this as well. Please note that you will need an additional
|
|
54
|
+
Application Insights resource, and long term logs will hash the user’s ID automatically (using
|
|
55
|
+
SHA256 by default, SHA1 is optional and should only be used in special circumstances, as it is not
|
|
56
|
+
secure).
|
|
57
|
+
|
|
58
|
+
```tsx
|
|
59
|
+
import { useAppInsights } from "@equinor/mad-insights";
|
|
60
|
+
|
|
61
|
+
export default function App() {
|
|
62
|
+
useAppInsights({
|
|
63
|
+
connectionString: "CONNECTION STRING FROM ENV CONFIG",
|
|
64
|
+
longTermLog: {
|
|
65
|
+
connectionString: "ANOTHER CONNECTION STRING FROM ENV CONFIG",
|
|
66
|
+
|
|
67
|
+
//ONLY USE SHA1 IN SPECIAL CIRCUMSTANCES. IT'S _NOT_ SECURE
|
|
68
|
+
useSHA1: false,
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
return <>...</>;
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
If implemented correctly, you should be able to test it by starting the app. The app should
|
|
77
|
+
automatically log “Application STARTED”. Go to App Insights, click “Events” in the sidebar, and then
|
|
78
|
+
“View More Insights” to check.
|
|
79
|
+
|
|
80
|
+

|
|
81
|
+
|
|
82
|
+
**Navigation**
|
|
83
|
+
|
|
84
|
+
Navigation logging can be implemented in your app. We have a `trackNavigation` method you can use
|
|
85
|
+
for this.
|
|
86
|
+
[Here’s React Navigation’s official doc regarding this issue](https://reactnavigation.org/docs/screen-tracking/).
|
|
87
|
+
Below is an example implementation taken from Expense and Pay (with some additional lines of code to
|
|
88
|
+
make TypeScript happy). If implemented correctly, it should log all navigation route changes to
|
|
89
|
+
Application Insights!
|
|
90
|
+
|
|
91
|
+
```tsx
|
|
92
|
+
export default function Navigation() {
|
|
93
|
+
const navigationRef = useNavigationContainerRef();
|
|
94
|
+
const routeNameRef = useRef<string | undefined>();
|
|
95
|
+
|
|
96
|
+
return (
|
|
97
|
+
<NavigationContainer
|
|
98
|
+
ref={navigationRef}
|
|
99
|
+
onReady={() => {
|
|
100
|
+
const currentRoute = navigationRef.getCurrentRoute();
|
|
101
|
+
if (!currentRoute) return;
|
|
102
|
+
routeNameRef.current = currentRoute.name;
|
|
103
|
+
}}
|
|
104
|
+
onStateChange={async () => {
|
|
105
|
+
const currentRoute = navigationRef.getCurrentRoute();
|
|
106
|
+
if (!currentRoute) return;
|
|
107
|
+
const previousRouteName = routeNameRef.current;
|
|
108
|
+
const currentRouteName = currentRoute.name;
|
|
109
|
+
|
|
110
|
+
if (previousRouteName !== currentRouteName) {
|
|
111
|
+
trackNavigation(currentRouteName);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
routeNameRef.current = currentRouteName;
|
|
115
|
+
}}
|
|
116
|
+
>
|
|
117
|
+
<RootNavigator />
|
|
118
|
+
</NavigationContainer>
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Alternatively, if you use `@equinor/mad-navigation`, this part is made easier for you:
|
|
124
|
+
|
|
125
|
+
```tsx
|
|
126
|
+
import { NavigationContainer } from "@equinor/mad-navigation";
|
|
127
|
+
|
|
128
|
+
export default function Navigation() {
|
|
129
|
+
return (
|
|
130
|
+
<NavigationContainer onRouteChange={currentRouteName => trackNavigation(currentRouteName)}>
|
|
131
|
+
<RootNavigator />
|
|
132
|
+
</NavigationContainer>
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
**Custom tracking**
|
|
138
|
+
|
|
139
|
+
If you need to add additional tracking to your app, we have some helper methods for that! The
|
|
140
|
+
easiest way to track something is to use `trackCustom`, which takes a string for the title, and
|
|
141
|
+
optionally an object with data. `trackCustom` logs to both long term log and short term log. If you
|
|
142
|
+
want more control over how the tracking should occur, use `track` if you want both long term and
|
|
143
|
+
short term tracking, or `trackShortTerm` and `trackLongTerm` respectively.
|
|
144
|
+
|
|
145
|
+
Example:
|
|
146
|
+
|
|
147
|
+
```tsx
|
|
148
|
+
//this is the easiest way to add some custom tracking
|
|
149
|
+
trackCustom("Custom event!", { param1: "param1", param2: "param2" });
|
|
150
|
+
|
|
151
|
+
//this is identical to the line above
|
|
152
|
+
track(metricKeys.CUSTOM, undefined, "Custom event!", { param1: "param1", param2: "param2" });
|
|
153
|
+
|
|
154
|
+
//this will only log to the short term log
|
|
155
|
+
trackShortTerm(metricKeys.CUSTOM, undefined, "Custom event!", {
|
|
156
|
+
param1: "param1",
|
|
157
|
+
param2: "param2",
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
//this will only log to the long term log
|
|
161
|
+
trackLongTerm(metricKeys.CUSTOM, undefined, "Custom event!", {
|
|
162
|
+
param1: "param1",
|
|
163
|
+
param2: "param2",
|
|
164
|
+
});
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Adding common information to all events
|
|
168
|
+
|
|
169
|
+
If you want to add common telemetry to all events, use the `addTelemetryInitializer` function.
|
|
170
|
+
|
|
171
|
+
Usage of `addTelemetryInitializer` can be found
|
|
172
|
+
[here](https://github.com/microsoft/ApplicationInsights-JS#telemetry-initializers). You can add
|
|
173
|
+
multiple envelopes to our application insights instances.
|
|
174
|
+
|
|
175
|
+
Import the function from `@equinor/mad-insights` in order to use our own instances of application
|
|
176
|
+
insights.
|
|
177
|
+
|
|
178
|
+
```tsx
|
|
179
|
+
import { ITelemetryItem, addTelemetryInitializer, useAppInsights } from "@equinor/mad-insights";
|
|
180
|
+
import * as APP from "./app.json";
|
|
181
|
+
|
|
182
|
+
export default function App() {
|
|
183
|
+
useAppInsights({
|
|
184
|
+
instrumentationKey: "SHORT TERM LOG INSTRUMENTATION KEY",
|
|
185
|
+
longTermLog: { instrumentationKey: "LONG TERM LOG INSTRUMENTATION KEY" },
|
|
186
|
+
});
|
|
187
|
+
useEffect(() => {
|
|
188
|
+
// envelope for adding app version to all events
|
|
189
|
+
const appVersionEnvelope = (item: ITelemetryItem) => {
|
|
190
|
+
if (item.data) {
|
|
191
|
+
item.data["app-version"] = APP.expo.version;
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
// add envelope to our application insights instances
|
|
196
|
+
addTelemetryInitializer(appVersionEnvelope);
|
|
197
|
+
}, []);
|
|
198
|
+
|
|
199
|
+
return <>...</>;
|
|
200
|
+
}
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
Once logged, the data can be found in the customDimensions tab:
|
|
204
|
+
|
|
205
|
+

|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { ReactPlugin } from "@microsoft/applicationinsights-react-js";
|
|
2
|
+
import { ApplicationInsights, ICustomProperties, ITelemetryItem } from "@microsoft/applicationinsights-web";
|
|
3
|
+
declare let appInsightsMain: ApplicationInsights;
|
|
4
|
+
declare let appInsightsLongTermLog: ApplicationInsights;
|
|
5
|
+
declare let reactPluginWeb: ReactPlugin;
|
|
6
|
+
declare let useSHA1: boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Initialize appInsights. This should be called at app startup (useEffect inside App.tsx might be a good place to put it).
|
|
9
|
+
* You need to provide a connectionString OR instrumentationKey.
|
|
10
|
+
* @example initializeTracking({connectionString: "STRING"})
|
|
11
|
+
* @example initializeTracking({instrumentationKey: "STRING"})
|
|
12
|
+
* @param payload: { connectionString } OR { instrumentationKey }
|
|
13
|
+
*/
|
|
14
|
+
export declare const appInsightsInit: (payload: ({
|
|
15
|
+
connectionString: string;
|
|
16
|
+
instrumentationKey?: undefined;
|
|
17
|
+
} | {
|
|
18
|
+
instrumentationKey: string;
|
|
19
|
+
connectionString?: undefined;
|
|
20
|
+
}) & {
|
|
21
|
+
/**
|
|
22
|
+
* Long term log hides user id
|
|
23
|
+
*/
|
|
24
|
+
longTermLog?: ({
|
|
25
|
+
connectionString: string;
|
|
26
|
+
instrumentationKey?: undefined;
|
|
27
|
+
} | {
|
|
28
|
+
instrumentationKey: string;
|
|
29
|
+
connectionString?: undefined;
|
|
30
|
+
}) & {
|
|
31
|
+
/**@deprecated ONLY use in special circumstances. SHA1 is _NOT_ secure*/
|
|
32
|
+
useSHA1?: boolean;
|
|
33
|
+
};
|
|
34
|
+
}) => void;
|
|
35
|
+
export declare const validateAppInsightsInit: () => void;
|
|
36
|
+
export declare const setUsername: (username: string, userIdentifier: string | undefined) => void;
|
|
37
|
+
/**
|
|
38
|
+
* Track something for both long term and short term logs. status, modifier & extraData is optional
|
|
39
|
+
* @param {metricKeys} eventName - Name of the event
|
|
40
|
+
* @param {metricStatus} status - Status of the event
|
|
41
|
+
* @param {string} [modifier] - extra text in the name of the event
|
|
42
|
+
* @param {Object} [extraData] - object to send as a customDimension property. Detailed information should be sent here
|
|
43
|
+
*/
|
|
44
|
+
export declare const track: (eventName: metricKeys, eventStatus?: metricStatus, extraText?: string, extraData?: ICustomProperties) => Promise<void>;
|
|
45
|
+
export declare const addTelemetryInitializer: (envelope: (item: ITelemetryItem) => boolean | void) => void;
|
|
46
|
+
/**
|
|
47
|
+
* Track something for short term logs. status, modifier & extraData is optional
|
|
48
|
+
* @param {metricKeys} eventName - Name of the event
|
|
49
|
+
* @param {metricStatus} status - Status of the event
|
|
50
|
+
* @param {string} [modifier] - extra text in the name of the event
|
|
51
|
+
* @param {Object} [extraData] - object to send as a customDimension property. Detailed information should be sent here
|
|
52
|
+
*/
|
|
53
|
+
export declare const trackShortTerm: (eventName: metricKeys, eventStatus?: metricStatus, extraText?: string, extraData?: ICustomProperties) => void;
|
|
54
|
+
/**
|
|
55
|
+
* WARNING: THIS DATA WILL BE STORED FOR UP TO 2 YEARS. PLEASE BE CAUTIOUS WHEN USING THIS METHOD!
|
|
56
|
+
*
|
|
57
|
+
* Track something for long term log. status, modifier & extraData is optional
|
|
58
|
+
* @param {metricKeys} eventName - Name of the event
|
|
59
|
+
* @param {metricStatus} status - Status of the event
|
|
60
|
+
* @param {string} [modifier] - extra text in the name of the event
|
|
61
|
+
* @param {Object} [extraData] - object to send as a customDimension property. Detailed information should be sent here
|
|
62
|
+
*/
|
|
63
|
+
export declare const trackLongTerm: (eventName: metricKeys, eventStatus?: metricStatus, extraText?: string, extraData?: ICustomProperties) => void;
|
|
64
|
+
/**
|
|
65
|
+
* Track navigation
|
|
66
|
+
* @param routeName - name of route
|
|
67
|
+
* @param longTerm - if you want to track in long term log as well
|
|
68
|
+
*/
|
|
69
|
+
export declare const trackNavigation: (routeName: string, longTerm?: boolean) => void;
|
|
70
|
+
/**
|
|
71
|
+
* Add a custom tracking event
|
|
72
|
+
* @param {string} text - text you want to add to the name of the event
|
|
73
|
+
* @param {Object} [extraData] - object to send as a customDimension property. Detailed information should be sent here
|
|
74
|
+
*/
|
|
75
|
+
export declare const trackCustom: (text: string, extraData?: ICustomProperties) => void;
|
|
76
|
+
/**
|
|
77
|
+
* Handle changes in app state
|
|
78
|
+
* @param nextState - name of next state
|
|
79
|
+
*/
|
|
80
|
+
export declare const handleAppStatusChange: (nextState: appStateStatus) => void;
|
|
81
|
+
export declare enum appStateStatus {
|
|
82
|
+
ACTIVE = "active",
|
|
83
|
+
BACKGROUND = "background",
|
|
84
|
+
INACTIVE = "inactive"
|
|
85
|
+
}
|
|
86
|
+
export declare enum metricStatus {
|
|
87
|
+
STARTED = "STARTED",
|
|
88
|
+
SUCCESS = "SUCCESS",
|
|
89
|
+
FAILED = "FAILED"
|
|
90
|
+
}
|
|
91
|
+
export declare enum metricKeys {
|
|
92
|
+
APP_ACTIVE = "Application ACTIVE",
|
|
93
|
+
APP_BACKGROUND = "Application BACKGROUND",
|
|
94
|
+
APP_STARTED = "Application STARTED",
|
|
95
|
+
AUTHENTICATION = "Authentication",
|
|
96
|
+
AUTHENTICATION_DEMO = "Authentication DEMO MODE",
|
|
97
|
+
AUTHENTICATION_AUTOMATIC = "Authentication AUTOMATIC",
|
|
98
|
+
API_GET = "GET",
|
|
99
|
+
API_POST = "POST",
|
|
100
|
+
API_PUT = "PUT",
|
|
101
|
+
API_PATCH = "PATCH",
|
|
102
|
+
API_DELETE = "DELETE",
|
|
103
|
+
API_UPLOAD = "UPLOAD",
|
|
104
|
+
API_DOWNLOAD = "DOWNLOAD",
|
|
105
|
+
NAVIGATE = "NAVIGATE",
|
|
106
|
+
CUSTOM = "CUSTOM"
|
|
107
|
+
}
|
|
108
|
+
export { reactPluginWeb, appInsightsMain, appInsightsLongTermLog };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import {
|
|
2
|
+
addTelemetryInitializer,
|
|
3
|
+
appInsightsInit,
|
|
4
|
+
appInsightsLongTermLog,
|
|
5
|
+
appInsightsMain,
|
|
6
|
+
appStateStatus,
|
|
7
|
+
handleAppStatusChange,
|
|
8
|
+
metricKeys,
|
|
9
|
+
metricStatus,
|
|
10
|
+
reactPluginWeb,
|
|
11
|
+
setUsername,
|
|
12
|
+
track,
|
|
13
|
+
trackCustom,
|
|
14
|
+
trackLongTerm,
|
|
15
|
+
trackNavigation,
|
|
16
|
+
trackShortTerm,
|
|
17
|
+
validateAppInsightsInit
|
|
18
|
+
} from "./chunk-PACV3Q7O.js";
|
|
19
|
+
import "./chunk-GUSP74WR.js";
|
|
20
|
+
export {
|
|
21
|
+
addTelemetryInitializer,
|
|
22
|
+
appInsightsInit,
|
|
23
|
+
appInsightsLongTermLog,
|
|
24
|
+
appInsightsMain,
|
|
25
|
+
appStateStatus,
|
|
26
|
+
handleAppStatusChange,
|
|
27
|
+
metricKeys,
|
|
28
|
+
metricStatus,
|
|
29
|
+
reactPluginWeb,
|
|
30
|
+
setUsername,
|
|
31
|
+
track,
|
|
32
|
+
trackCustom,
|
|
33
|
+
trackLongTerm,
|
|
34
|
+
trackNavigation,
|
|
35
|
+
trackShortTerm,
|
|
36
|
+
validateAppInsightsInit
|
|
37
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
appInsightsInit
|
|
3
|
+
} from "./chunk-PACV3Q7O.js";
|
|
4
|
+
|
|
5
|
+
// src/useAppInsights.ts
|
|
6
|
+
import { useEffect } from "react";
|
|
7
|
+
var useAppInsights = (...args) => {
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
appInsightsInit(...args);
|
|
10
|
+
}, [args]);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export {
|
|
14
|
+
useAppInsights
|
|
15
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/encrypt.ts
|
|
23
|
+
import CryptoJS from "crypto-js";
|
|
24
|
+
function obfuscateUser(username, userIdentifier, useSHA1) {
|
|
25
|
+
let domain;
|
|
26
|
+
if (username.includes("@") === false)
|
|
27
|
+
domain = "no domain";
|
|
28
|
+
else
|
|
29
|
+
domain = username.split("@")[1].toLowerCase();
|
|
30
|
+
const stringToEncrypt = `${userIdentifier}`;
|
|
31
|
+
const hash = useSHA1 ? CryptoJS.SHA1(stringToEncrypt) : CryptoJS.SHA256(stringToEncrypt);
|
|
32
|
+
return { id: hash.toString(), domain };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export {
|
|
36
|
+
__async,
|
|
37
|
+
obfuscateUser
|
|
38
|
+
};
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__async,
|
|
3
|
+
obfuscateUser
|
|
4
|
+
} from "./chunk-GUSP74WR.js";
|
|
5
|
+
|
|
6
|
+
// src/appInsights.ts
|
|
7
|
+
import { ReactPlugin } from "@microsoft/applicationinsights-react-js";
|
|
8
|
+
import { ReactNativePlugin } from "@microsoft/applicationinsights-react-native";
|
|
9
|
+
import {
|
|
10
|
+
ApplicationInsights
|
|
11
|
+
} from "@microsoft/applicationinsights-web";
|
|
12
|
+
import { createBrowserHistory } from "history";
|
|
13
|
+
import { Platform } from "react-native";
|
|
14
|
+
var appInsightsMain;
|
|
15
|
+
var appInsightsLongTermLog;
|
|
16
|
+
var reactPluginWeb;
|
|
17
|
+
var useSHA1;
|
|
18
|
+
var appInsightsInit = (payload) => {
|
|
19
|
+
var _a;
|
|
20
|
+
const { connectionString, instrumentationKey } = payload;
|
|
21
|
+
useSHA1 = ((_a = payload.longTermLog) == null ? void 0 : _a.useSHA1) || false;
|
|
22
|
+
if (Platform.OS === "web") {
|
|
23
|
+
const browserHistory = createBrowserHistory();
|
|
24
|
+
reactPluginWeb = new ReactPlugin();
|
|
25
|
+
appInsightsMain = new ApplicationInsights({
|
|
26
|
+
config: {
|
|
27
|
+
connectionString,
|
|
28
|
+
instrumentationKey,
|
|
29
|
+
disableFetchTracking: false,
|
|
30
|
+
extensions: [reactPluginWeb],
|
|
31
|
+
extensionConfig: {
|
|
32
|
+
[reactPluginWeb.identifier]: { history: browserHistory }
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
if (payload.longTermLog) {
|
|
37
|
+
appInsightsLongTermLog = new ApplicationInsights({
|
|
38
|
+
config: {
|
|
39
|
+
connectionString: payload.longTermLog.connectionString,
|
|
40
|
+
instrumentationKey: payload.longTermLog.instrumentationKey,
|
|
41
|
+
disableFetchTracking: false,
|
|
42
|
+
extensions: [reactPluginWeb],
|
|
43
|
+
extensionConfig: {
|
|
44
|
+
[reactPluginWeb.identifier]: { history: browserHistory }
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
appInsightsLongTermLog.loadAppInsights();
|
|
49
|
+
}
|
|
50
|
+
appInsightsMain.loadAppInsights();
|
|
51
|
+
} else {
|
|
52
|
+
const RNPlugin = new ReactNativePlugin();
|
|
53
|
+
appInsightsMain = new ApplicationInsights({
|
|
54
|
+
config: {
|
|
55
|
+
disableFetchTracking: false,
|
|
56
|
+
connectionString,
|
|
57
|
+
instrumentationKey,
|
|
58
|
+
extensions: [RNPlugin]
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
if (payload.longTermLog) {
|
|
62
|
+
appInsightsLongTermLog = new ApplicationInsights({
|
|
63
|
+
config: {
|
|
64
|
+
disableFetchTracking: false,
|
|
65
|
+
connectionString: payload.longTermLog.connectionString,
|
|
66
|
+
instrumentationKey: payload.longTermLog.instrumentationKey,
|
|
67
|
+
extensions: [RNPlugin]
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
appInsightsLongTermLog.loadAppInsights();
|
|
71
|
+
}
|
|
72
|
+
appInsightsMain.loadAppInsights();
|
|
73
|
+
}
|
|
74
|
+
track("Application STARTED" /* APP_STARTED */);
|
|
75
|
+
};
|
|
76
|
+
var validateAppInsightsInit = () => {
|
|
77
|
+
if (!appInsightsMain) {
|
|
78
|
+
throw new Error(
|
|
79
|
+
"mad-insights error: AppInsights has not been initialized. please run appInsightsInit(args) at startup"
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
var setUsername = (username, userIdentifier) => {
|
|
84
|
+
validateAppInsightsInit();
|
|
85
|
+
appInsightsMain.setAuthenticatedUserContext(username, userIdentifier, true);
|
|
86
|
+
if (appInsightsLongTermLog) {
|
|
87
|
+
const obfuscatedUserName = obfuscateUser(userIdentifier || "", username, useSHA1).id;
|
|
88
|
+
const obfuscatedUserId = obfuscateUser(username, userIdentifier || "", useSHA1).id;
|
|
89
|
+
appInsightsLongTermLog.setAuthenticatedUserContext(
|
|
90
|
+
obfuscatedUserName,
|
|
91
|
+
obfuscatedUserId,
|
|
92
|
+
true
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
var trackEvent = (event, customProperties) => {
|
|
97
|
+
validateAppInsightsInit();
|
|
98
|
+
appInsightsMain.trackEvent(event, customProperties);
|
|
99
|
+
};
|
|
100
|
+
var trackEventLongTerm = (event, customProperties) => {
|
|
101
|
+
if (!appInsightsLongTermLog)
|
|
102
|
+
throw "No long term log set up";
|
|
103
|
+
appInsightsLongTermLog.trackEvent(event, customProperties);
|
|
104
|
+
};
|
|
105
|
+
var track = (eventName, eventStatus, extraText, extraData) => __async(void 0, null, function* () {
|
|
106
|
+
const eventString = `${eventName} ${eventStatus || ""}. ${extraText || ""}`;
|
|
107
|
+
if (excludeLogFilter(eventString, ["Ping", "ServiceMessage"]))
|
|
108
|
+
return;
|
|
109
|
+
trackEvent({ name: eventString }, extraData);
|
|
110
|
+
if (appInsightsLongTermLog) {
|
|
111
|
+
trackEventLongTerm({ name: eventString }, extraData);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
var addTelemetryInitializer = (envelope) => {
|
|
115
|
+
appInsightsMain.addTelemetryInitializer(envelope);
|
|
116
|
+
appInsightsLongTermLog.addTelemetryInitializer(envelope);
|
|
117
|
+
};
|
|
118
|
+
var excludeLogFilter = (eventString, excludeStrings) => excludeStrings.some((excludeString) => eventString.includes(excludeString));
|
|
119
|
+
var trackShortTerm = (eventName, eventStatus, extraText, extraData) => {
|
|
120
|
+
const eventString = `${eventName} ${eventStatus || ""}. ${extraText || ""}`;
|
|
121
|
+
trackEvent({ name: eventString }, extraData);
|
|
122
|
+
};
|
|
123
|
+
var trackLongTerm = (eventName, eventStatus, extraText, extraData) => {
|
|
124
|
+
const eventString = `${eventName} ${eventStatus || ""}. ${extraText || ""}`;
|
|
125
|
+
trackEventLongTerm({ name: eventString }, extraData);
|
|
126
|
+
};
|
|
127
|
+
var trackNavigation = (routeName, longTerm) => {
|
|
128
|
+
track("NAVIGATE" /* NAVIGATE */, void 0, routeName);
|
|
129
|
+
if (longTerm)
|
|
130
|
+
trackLongTerm("NAVIGATE" /* NAVIGATE */, void 0, routeName);
|
|
131
|
+
};
|
|
132
|
+
var trackCustom = (text, extraData) => {
|
|
133
|
+
track("CUSTOM" /* CUSTOM */, void 0, text, extraData);
|
|
134
|
+
};
|
|
135
|
+
var handleAppStatusChange = (nextState) => {
|
|
136
|
+
switch (nextState) {
|
|
137
|
+
case "active": {
|
|
138
|
+
track("Application ACTIVE" /* APP_ACTIVE */);
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
case "background": {
|
|
142
|
+
track("Application BACKGROUND" /* APP_BACKGROUND */);
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
var appStateStatus = /* @__PURE__ */ ((appStateStatus2) => {
|
|
148
|
+
appStateStatus2["ACTIVE"] = "active";
|
|
149
|
+
appStateStatus2["BACKGROUND"] = "background";
|
|
150
|
+
appStateStatus2["INACTIVE"] = "inactive";
|
|
151
|
+
return appStateStatus2;
|
|
152
|
+
})(appStateStatus || {});
|
|
153
|
+
var metricStatus = /* @__PURE__ */ ((metricStatus2) => {
|
|
154
|
+
metricStatus2["STARTED"] = "STARTED";
|
|
155
|
+
metricStatus2["SUCCESS"] = "SUCCESS";
|
|
156
|
+
metricStatus2["FAILED"] = "FAILED";
|
|
157
|
+
return metricStatus2;
|
|
158
|
+
})(metricStatus || {});
|
|
159
|
+
var metricKeys = /* @__PURE__ */ ((metricKeys2) => {
|
|
160
|
+
metricKeys2["APP_ACTIVE"] = "Application ACTIVE";
|
|
161
|
+
metricKeys2["APP_BACKGROUND"] = "Application BACKGROUND";
|
|
162
|
+
metricKeys2["APP_STARTED"] = "Application STARTED";
|
|
163
|
+
metricKeys2["AUTHENTICATION"] = "Authentication";
|
|
164
|
+
metricKeys2["AUTHENTICATION_DEMO"] = "Authentication DEMO MODE";
|
|
165
|
+
metricKeys2["AUTHENTICATION_AUTOMATIC"] = "Authentication AUTOMATIC";
|
|
166
|
+
metricKeys2["API_GET"] = "GET";
|
|
167
|
+
metricKeys2["API_POST"] = "POST";
|
|
168
|
+
metricKeys2["API_PUT"] = "PUT";
|
|
169
|
+
metricKeys2["API_PATCH"] = "PATCH";
|
|
170
|
+
metricKeys2["API_DELETE"] = "DELETE";
|
|
171
|
+
metricKeys2["API_UPLOAD"] = "UPLOAD";
|
|
172
|
+
metricKeys2["API_DOWNLOAD"] = "DOWNLOAD";
|
|
173
|
+
metricKeys2["NAVIGATE"] = "NAVIGATE";
|
|
174
|
+
metricKeys2["CUSTOM"] = "CUSTOM";
|
|
175
|
+
return metricKeys2;
|
|
176
|
+
})(metricKeys || {});
|
|
177
|
+
|
|
178
|
+
export {
|
|
179
|
+
appInsightsMain,
|
|
180
|
+
appInsightsLongTermLog,
|
|
181
|
+
reactPluginWeb,
|
|
182
|
+
appInsightsInit,
|
|
183
|
+
validateAppInsightsInit,
|
|
184
|
+
setUsername,
|
|
185
|
+
track,
|
|
186
|
+
addTelemetryInitializer,
|
|
187
|
+
trackShortTerm,
|
|
188
|
+
trackLongTerm,
|
|
189
|
+
trackNavigation,
|
|
190
|
+
trackCustom,
|
|
191
|
+
handleAppStatusChange,
|
|
192
|
+
appStateStatus,
|
|
193
|
+
metricStatus,
|
|
194
|
+
metricKeys
|
|
195
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Obfuscate user identity using sha1 or sha256 encryption. Used in app insights
|
|
3
|
+
* @param username - e.g. 'user@equinor.com'
|
|
4
|
+
* @param userIdentifier - GUID
|
|
5
|
+
* @param useSHA1 - default false. ONLY USE SHA1 IN SPECIAL CIRCUMSTANCES, AS SHA1 IS _NOT_ SECURE
|
|
6
|
+
*/
|
|
7
|
+
export declare function obfuscateUser(username: string, userIdentifier: string,
|
|
8
|
+
/**@deprecated */
|
|
9
|
+
useSHA1?: boolean): ObfuscatedUser;
|
|
10
|
+
export type ObfuscatedUser = {
|
|
11
|
+
id: string;
|
|
12
|
+
domain: string;
|
|
13
|
+
};
|
package/dist/encrypt.js
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useAppInsights
|
|
3
|
+
} from "./chunk-2YSWHN2G.js";
|
|
4
|
+
import {
|
|
5
|
+
addTelemetryInitializer,
|
|
6
|
+
appInsightsInit,
|
|
7
|
+
appInsightsLongTermLog,
|
|
8
|
+
appInsightsMain,
|
|
9
|
+
appStateStatus,
|
|
10
|
+
handleAppStatusChange,
|
|
11
|
+
metricKeys,
|
|
12
|
+
metricStatus,
|
|
13
|
+
reactPluginWeb,
|
|
14
|
+
setUsername,
|
|
15
|
+
track,
|
|
16
|
+
trackCustom,
|
|
17
|
+
trackLongTerm,
|
|
18
|
+
trackNavigation,
|
|
19
|
+
trackShortTerm,
|
|
20
|
+
validateAppInsightsInit
|
|
21
|
+
} from "./chunk-PACV3Q7O.js";
|
|
22
|
+
import "./chunk-GUSP74WR.js";
|
|
23
|
+
export {
|
|
24
|
+
addTelemetryInitializer,
|
|
25
|
+
appInsightsInit,
|
|
26
|
+
appInsightsLongTermLog,
|
|
27
|
+
appInsightsMain,
|
|
28
|
+
appStateStatus,
|
|
29
|
+
handleAppStatusChange,
|
|
30
|
+
metricKeys,
|
|
31
|
+
metricStatus,
|
|
32
|
+
reactPluginWeb,
|
|
33
|
+
setUsername,
|
|
34
|
+
track,
|
|
35
|
+
trackCustom,
|
|
36
|
+
trackLongTerm,
|
|
37
|
+
trackNavigation,
|
|
38
|
+
trackShortTerm,
|
|
39
|
+
useAppInsights,
|
|
40
|
+
validateAppInsightsInit
|
|
41
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare const useAppInsights: (payload: ({
|
|
2
|
+
connectionString: string;
|
|
3
|
+
instrumentationKey?: undefined;
|
|
4
|
+
} | {
|
|
5
|
+
instrumentationKey: string;
|
|
6
|
+
connectionString?: undefined;
|
|
7
|
+
}) & {
|
|
8
|
+
longTermLog?: (({
|
|
9
|
+
connectionString: string;
|
|
10
|
+
instrumentationKey?: undefined;
|
|
11
|
+
} | {
|
|
12
|
+
instrumentationKey: string;
|
|
13
|
+
connectionString?: undefined;
|
|
14
|
+
}) & {
|
|
15
|
+
useSHA1?: boolean | undefined;
|
|
16
|
+
}) | undefined;
|
|
17
|
+
}) => void;
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@equinor/mad-insights",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Application Insights implementation for the mad team",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"react native",
|
|
8
|
+
"tracking",
|
|
9
|
+
"logging",
|
|
10
|
+
"typescript"
|
|
11
|
+
],
|
|
12
|
+
"author": {
|
|
13
|
+
"name": "Equinor Mobile App Delivery team (MAD)"
|
|
14
|
+
},
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/equinor/mad/issues"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/equinor/mad/",
|
|
20
|
+
"main": "./dist/index.js",
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"react": ">=16.8",
|
|
24
|
+
"react-dom": ">=16.8",
|
|
25
|
+
"react-native": ">=0.68",
|
|
26
|
+
"react-native-device-info": "^10.8.0"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@microsoft/applicationinsights-react-js": "^17.0.0",
|
|
30
|
+
"@microsoft/applicationinsights-react-native": "^4.0.0",
|
|
31
|
+
"@microsoft/applicationinsights-web": "^3.0.2",
|
|
32
|
+
"crypto-js": "^4.1.1",
|
|
33
|
+
"history": "^5.3.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/crypto-js": "^4.1.2",
|
|
37
|
+
"@equinor/eslint-config-mad": "0.2.1"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"CODE_OF_CONDUCT.md",
|
|
41
|
+
"LICENSE",
|
|
42
|
+
"CHANGELOG.md",
|
|
43
|
+
"dist"
|
|
44
|
+
],
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "tsup-node",
|
|
47
|
+
"test": "node --experimental-vm-modules ../../node_modules/jest/bin/jest.js",
|
|
48
|
+
"dev": "tsup-node --watch --clean=false",
|
|
49
|
+
"lint": "eslint src/"
|
|
50
|
+
}
|
|
51
|
+
}
|