@backstage-community/plugin-analytics-module-newrelic-browser 0.1.5

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 ADDED
@@ -0,0 +1,366 @@
1
+ # @backstage-community/plugin-analytics-module-newrelic-browser
2
+
3
+ ## 0.1.5
4
+
5
+ ### Patch Changes
6
+
7
+ - 193a2a3: Migrated from the [backstage/backstage](https://github.com/backstage/backstage) monorepo.
8
+
9
+ ## 0.1.4
10
+
11
+ ### Patch Changes
12
+
13
+ - abfbcfc: Updated dependency `@testing-library/react` to `^15.0.0`.
14
+ - Updated dependencies
15
+ - @backstage/core-components@0.14.4
16
+ - @backstage/core-plugin-api@1.9.2
17
+ - @backstage/frontend-plugin-api@0.6.4
18
+ - @backstage/config@1.2.0
19
+
20
+ ## 0.1.4-next.1
21
+
22
+ ### Patch Changes
23
+
24
+ - Updated dependencies
25
+ - @backstage/frontend-plugin-api@0.6.4-next.1
26
+ - @backstage/config@1.2.0
27
+ - @backstage/core-components@0.14.4-next.0
28
+ - @backstage/core-plugin-api@1.9.1
29
+
30
+ ## 0.1.4-next.0
31
+
32
+ ### Patch Changes
33
+
34
+ - Updated dependencies
35
+ - @backstage/core-components@0.14.4-next.0
36
+ - @backstage/config@1.2.0
37
+ - @backstage/core-plugin-api@1.9.1
38
+ - @backstage/frontend-plugin-api@0.6.4-next.0
39
+
40
+ ## 0.1.3
41
+
42
+ ### Patch Changes
43
+
44
+ - Updated dependencies
45
+ - @backstage/core-components@0.14.3
46
+ - @backstage/frontend-plugin-api@0.6.3
47
+ - @backstage/core-plugin-api@1.9.1
48
+ - @backstage/config@1.2.0
49
+
50
+ ## 0.1.2
51
+
52
+ ### Patch Changes
53
+
54
+ - Updated dependencies
55
+ - @backstage/core-components@0.14.2
56
+ - @backstage/frontend-plugin-api@0.6.2
57
+ - @backstage/core-plugin-api@1.9.1
58
+ - @backstage/config@1.2.0
59
+
60
+ ## 0.1.1
61
+
62
+ ### Patch Changes
63
+
64
+ - Updated dependencies
65
+ - @backstage/config@1.2.0
66
+ - @backstage/core-components@0.14.1
67
+ - @backstage/core-plugin-api@1.9.1
68
+ - @backstage/frontend-plugin-api@0.6.1
69
+
70
+ ## 0.1.1-next.2
71
+
72
+ ### Patch Changes
73
+
74
+ - Updated dependencies
75
+ - @backstage/core-components@0.14.1-next.2
76
+ - @backstage/frontend-plugin-api@0.6.1-next.2
77
+ - @backstage/config@1.2.0-next.1
78
+ - @backstage/core-plugin-api@1.9.1-next.1
79
+
80
+ ## 0.1.1-next.1
81
+
82
+ ### Patch Changes
83
+
84
+ - Updated dependencies
85
+ - @backstage/config@1.2.0-next.1
86
+ - @backstage/core-components@0.14.1-next.1
87
+ - @backstage/core-plugin-api@1.9.1-next.1
88
+ - @backstage/frontend-plugin-api@0.6.1-next.1
89
+
90
+ ## 0.1.1-next.0
91
+
92
+ ### Patch Changes
93
+
94
+ - Updated dependencies
95
+ - @backstage/core-components@0.14.1-next.0
96
+ - @backstage/config@1.1.2-next.0
97
+ - @backstage/core-plugin-api@1.9.1-next.0
98
+ - @backstage/frontend-plugin-api@0.6.1-next.0
99
+
100
+ ## 0.1.0
101
+
102
+ ### Minor Changes
103
+
104
+ - e586f79: Add support to the new analytics api.
105
+
106
+ ### Patch Changes
107
+
108
+ - 9aac2b0: Use `--cwd` as the first `yarn` argument
109
+ - 8472188: Added or fixed the `repository` field in `package.json`.
110
+ - Updated dependencies
111
+ - @backstage/frontend-plugin-api@0.6.0
112
+ - @backstage/core-components@0.14.0
113
+ - @backstage/core-plugin-api@1.9.0
114
+ - @backstage/config@1.1.1
115
+
116
+ ## 0.1.0-next.2
117
+
118
+ ### Patch Changes
119
+
120
+ - 8472188: Added or fixed the `repository` field in `package.json`.
121
+ - Updated dependencies
122
+ - @backstage/core-components@0.14.0-next.2
123
+ - @backstage/config@1.1.1
124
+ - @backstage/core-plugin-api@1.9.0-next.1
125
+ - @backstage/frontend-plugin-api@0.6.0-next.3
126
+
127
+ ## 0.1.0-next.1
128
+
129
+ ### Patch Changes
130
+
131
+ - 9aac2b0: Use `--cwd` as the first `yarn` argument
132
+ - Updated dependencies
133
+ - @backstage/core-components@0.14.0-next.1
134
+ - @backstage/core-plugin-api@1.9.0-next.1
135
+ - @backstage/frontend-plugin-api@0.6.0-next.2
136
+ - @backstage/config@1.1.1
137
+
138
+ ## 0.1.0-next.0
139
+
140
+ ### Minor Changes
141
+
142
+ - e586f79: Add support to the new analytics api.
143
+
144
+ ### Patch Changes
145
+
146
+ - Updated dependencies
147
+ - @backstage/frontend-plugin-api@0.6.0-next.1
148
+ - @backstage/core-components@0.14.0-next.0
149
+ - @backstage/core-plugin-api@1.8.3-next.0
150
+ - @backstage/config@1.1.1
151
+
152
+ ## 0.0.6
153
+
154
+ ### Patch Changes
155
+
156
+ - 4016f21: Remove some unused dependencies
157
+ - Updated dependencies
158
+ - @backstage/core-components@0.13.10
159
+ - @backstage/core-plugin-api@1.8.2
160
+ - @backstage/config@1.1.1
161
+
162
+ ## 0.0.6-next.1
163
+
164
+ ### Patch Changes
165
+
166
+ - Updated dependencies
167
+ - @backstage/core-plugin-api@1.8.2-next.0
168
+ - @backstage/core-components@0.13.10-next.1
169
+ - @backstage/config@1.1.1
170
+
171
+ ## 0.0.6-next.0
172
+
173
+ ### Patch Changes
174
+
175
+ - 4016f21: Remove some unused dependencies
176
+ - Updated dependencies
177
+ - @backstage/core-components@0.13.10-next.0
178
+ - @backstage/config@1.1.1
179
+ - @backstage/core-plugin-api@1.8.1
180
+
181
+ ## 0.0.5
182
+
183
+ ### Patch Changes
184
+
185
+ - Updated dependencies
186
+ - @backstage/core-plugin-api@1.8.1
187
+ - @backstage/core-components@0.13.9
188
+ - @backstage/config@1.1.1
189
+
190
+ ## 0.0.5-next.3
191
+
192
+ ### Patch Changes
193
+
194
+ - Updated dependencies
195
+ - @backstage/core-components@0.13.9-next.3
196
+ - @backstage/config@1.1.1
197
+ - @backstage/core-plugin-api@1.8.1-next.1
198
+
199
+ ## 0.0.5-next.2
200
+
201
+ ### Patch Changes
202
+
203
+ - Updated dependencies
204
+ - @backstage/config@1.1.1
205
+ - @backstage/core-components@0.13.9-next.2
206
+ - @backstage/core-plugin-api@1.8.1-next.1
207
+
208
+ ## 0.0.5-next.1
209
+
210
+ ### Patch Changes
211
+
212
+ - Updated dependencies
213
+ - @backstage/core-components@0.13.9-next.1
214
+ - @backstage/core-plugin-api@1.8.1-next.1
215
+ - @backstage/config@1.1.1
216
+
217
+ ## 0.0.5-next.0
218
+
219
+ ### Patch Changes
220
+
221
+ - Updated dependencies
222
+ - @backstage/core-plugin-api@1.8.1-next.0
223
+ - @backstage/core-components@0.13.9-next.0
224
+ - @backstage/config@1.1.1
225
+
226
+ ## 0.0.4
227
+
228
+ ### Patch Changes
229
+
230
+ - 6c2b872153: Add official support for React 18.
231
+ - Updated dependencies
232
+ - @backstage/core-components@0.13.8
233
+ - @backstage/core-plugin-api@1.8.0
234
+ - @backstage/config@1.1.1
235
+
236
+ ## 0.0.4-next.2
237
+
238
+ ### Patch Changes
239
+
240
+ - Updated dependencies
241
+ - @backstage/core-components@0.13.8-next.2
242
+
243
+ ## 0.0.4-next.1
244
+
245
+ ### Patch Changes
246
+
247
+ - Updated dependencies
248
+ - @backstage/core-components@0.13.8-next.1
249
+ - @backstage/config@1.1.1
250
+ - @backstage/core-plugin-api@1.8.0-next.0
251
+
252
+ ## 0.0.4-next.0
253
+
254
+ ### Patch Changes
255
+
256
+ - 6c2b872153: Add official support for React 18.
257
+ - Updated dependencies
258
+ - @backstage/core-components@0.13.7-next.0
259
+ - @backstage/core-plugin-api@1.8.0-next.0
260
+ - @backstage/config@1.1.1
261
+
262
+ ## 0.0.3
263
+
264
+ ### Patch Changes
265
+
266
+ - 9a1fce352e: Updated dependency `@testing-library/jest-dom` to `^6.0.0`.
267
+ - Updated dependencies
268
+ - @backstage/core-plugin-api@1.7.0
269
+ - @backstage/core-components@0.13.6
270
+ - @backstage/config@1.1.1
271
+
272
+ ## 0.0.3-next.2
273
+
274
+ ### Patch Changes
275
+
276
+ - Updated dependencies
277
+ - @backstage/core-components@0.13.6-next.2
278
+ - @backstage/core-plugin-api@1.7.0-next.1
279
+ - @backstage/config@1.1.1-next.0
280
+
281
+ ## 0.0.3-next.1
282
+
283
+ ### Patch Changes
284
+
285
+ - Updated dependencies
286
+ - @backstage/core-components@0.13.6-next.1
287
+ - @backstage/core-plugin-api@1.7.0-next.0
288
+ - @backstage/config@1.1.0
289
+
290
+ ## 0.0.3-next.0
291
+
292
+ ### Patch Changes
293
+
294
+ - Updated dependencies
295
+ - @backstage/core-plugin-api@1.7.0-next.0
296
+ - @backstage/core-components@0.13.6-next.0
297
+ - @backstage/config@1.1.0
298
+
299
+ ## 0.0.2
300
+
301
+ ### Patch Changes
302
+
303
+ - 406b786a2a2c: Mark package as being free of side effects, allowing more optimized Webpack builds.
304
+ - 8cec7664e146: Removed `@types/node` dependency
305
+ - Updated dependencies
306
+ - @backstage/core-components@0.13.5
307
+ - @backstage/config@1.1.0
308
+ - @backstage/core-plugin-api@1.6.0
309
+
310
+ ## 0.0.2-next.3
311
+
312
+ ### Patch Changes
313
+
314
+ - 406b786a2a2c: Mark package as being free of side effects, allowing more optimized Webpack builds.
315
+ - Updated dependencies
316
+ - @backstage/config@1.1.0-next.2
317
+ - @backstage/core-components@0.13.5-next.3
318
+ - @backstage/core-plugin-api@1.6.0-next.3
319
+
320
+ ## 0.0.2-next.2
321
+
322
+ ### Patch Changes
323
+
324
+ - 8cec7664e146: Removed `@types/node` dependency
325
+ - Updated dependencies
326
+ - @backstage/core-components@0.13.5-next.2
327
+ - @backstage/core-plugin-api@1.6.0-next.2
328
+ - @backstage/config@1.1.0-next.1
329
+
330
+ ## 0.0.2-next.1
331
+
332
+ ### Patch Changes
333
+
334
+ - Updated dependencies
335
+ - @backstage/core-components@0.13.5-next.1
336
+ - @backstage/config@1.1.0-next.0
337
+ - @backstage/core-plugin-api@1.6.0-next.1
338
+
339
+ ## 0.0.2-next.0
340
+
341
+ ### Patch Changes
342
+
343
+ - Updated dependencies
344
+ - @backstage/core-plugin-api@1.6.0-next.0
345
+ - @backstage/core-components@0.13.5-next.0
346
+ - @backstage/config@1.0.8
347
+
348
+ ## 0.0.1
349
+
350
+ ### Patch Changes
351
+
352
+ - ec7357258853: Introduced the New Relic Browser analytics module. Check out the plugins [README.md](https://github.com/backstage/backstage/tree/master/plugins/analytics-module-newrelic-browser) for more details!
353
+ - Updated dependencies
354
+ - @backstage/core-components@0.13.4
355
+ - @backstage/core-plugin-api@1.5.3
356
+ - @backstage/config@1.0.8
357
+
358
+ ## 0.0.1-next.0
359
+
360
+ ### Patch Changes
361
+
362
+ - ec7357258853: Introduced the New Relic Browser analytics module. Check out the plugins [README.md](https://github.com/backstage/backstage/tree/master/plugins/analytics-module-newrelic-browser) for more details!
363
+ - Updated dependencies
364
+ - @backstage/core-components@0.13.4-next.0
365
+ - @backstage/core-plugin-api@1.5.3
366
+ - @backstage/config@1.0.8
package/README.md ADDED
@@ -0,0 +1,135 @@
1
+ # Analytics Module: New Relic Browser
2
+
3
+ This plugin provides an opinionated implementation of the Backstage Analytics API for New Relic Browser. Once installed and configured, analytics events will be sent to New Relic as your users navigate and use your Backstage instance.
4
+
5
+ This plugin contains no other functionality.
6
+
7
+ ## Installation
8
+
9
+ 1. Install the plugin package in your Backstage app:
10
+
11
+ ```sh
12
+ # From your Backstage root directory
13
+ yarn --cwd packages/app add @backstage-community/plugin-analytics-module-newrelic-browser
14
+ ```
15
+
16
+ 2. Wire up the API implementation to your App:
17
+
18
+ ```tsx
19
+ // packages/app/src/apis.ts
20
+ import {
21
+ analyticsApiRef,
22
+ configApiRef,
23
+ identityApiRef,
24
+ } from '@backstage/core-plugin-api';
25
+ import { NewRelicBrowser } from '@backstage-community/plugin-analytics-module-newrelic-browser';
26
+
27
+ export const apis: AnyApiFactory[] = [
28
+ // Instantiate and register the New Relic Browser API Implementation.
29
+ createApiFactory({
30
+ api: analyticsApiRef,
31
+ deps: { configApi: configApiRef, identityApi: identityApiRef },
32
+ factory: ({ configApi, identityApi }) =>
33
+ NewRelicBrowser.fromConfig(configApi, {
34
+ identityApi,
35
+ }),
36
+ }),
37
+ ];
38
+ ```
39
+
40
+ 3. Configure the plugin in your `app-config.yaml`:
41
+
42
+ The following is the minimum configuration required to start sending analytics
43
+ events to New Relic Browser. You find this information when creating a new application
44
+ in New Relic Browser using the Copy/Paste method.
45
+
46
+ ```yaml
47
+ # app-config.yaml
48
+ app:
49
+ analytics:
50
+ newRelic:
51
+ endpoint: 'bam.nr-data.net',
52
+ accountId: '1234567'
53
+ applicationId: '987654321'
54
+ licenseKey: 'NRJS-12a3456bc78de9123f4'
55
+ ```
56
+
57
+ > Note: Depending on New Relic's data center you are using you'll want to change the `endpoint` to `bam.eu01.nr-data.net` for the EU data center. Refer to [this document](https://docs.newrelic.com/docs/new-relic-solutions/get-started/networks/#data-ingest) for available endpoints.
58
+
59
+ ## Configuration
60
+
61
+ By default the distributed tracing and cookies features are disabled. You can enable them by adding the following to your `app-config.yaml`:
62
+
63
+ ```yaml
64
+ # app-config.yaml
65
+ app:
66
+ analytics:
67
+ newRelic:
68
+ ...
69
+ distributedTracing: true
70
+ cookiesEnabled: true
71
+ ```
72
+
73
+ ### User IDs
74
+
75
+ This plugin supports sending user context to New Relic Browser by providing a User ID. This requires instantiating the `NewRelicBrowser` instance with an `identityApi` instance passed to it, but this is optional. If omitted the plugin will not send user context to New Relic Browser.
76
+
77
+ By default the user ID is calculated as a SHA-256 hash of the current user's `userEntityRef` as returned by the `identityApi`. To set a
78
+ different value, provide a `userIdTransform` function alongside `identityApi` when you instantiate `NewRelicBrowser`. This function will be passed the `userEntityRef` as an argument and should resolve to the value you wish to set as the user ID. For example:
79
+
80
+ ```typescript
81
+ import {
82
+ analyticsApiRef,
83
+ configApiRef,
84
+ identityApiRef,
85
+ } from '@backstage/core-plugin-api';
86
+ import { GoogleAnalytics } from '@backstage-community/plugin-analytics-module-newrelic-browser';
87
+
88
+ export const apis: AnyApiFactory[] = [
89
+ createApiFactory({
90
+ api: analyticsApiRef,
91
+ deps: { configApi: configApiRef, identityApi: identityApiRef },
92
+ factory: ({ configApi, identityApi }) =>
93
+ NewRelicBrowser.fromConfig(configApi, {
94
+ identityApi,
95
+ userIdTransform: async (userEntityRef: string): Promise<string> => {
96
+ return customHashingFunction(userEntityRef);
97
+ },
98
+ }),
99
+ }),
100
+ ];
101
+ ```
102
+
103
+ ## Development
104
+
105
+ If you would like to contribute improvements to this plugin, the easiest way to
106
+ make and test changes is to do the following:
107
+
108
+ 1. Clone the main Backstage monorepo `git clone git@github.com:backstage/backstage.git`
109
+ 2. Install all dependencies `yarn install`
110
+ 3. If one does not exist, create an `app-config.local.yaml` file in the root of
111
+ the monorepo and add config for this plugin (see below)
112
+ 4. Enter this plugin's working directory: `cd plugins/analytics-provider-newrelic-browser`
113
+ 5. Start the plugin in isolation: `yarn start`
114
+ 6. Navigate to the playground page at `http://localhost:3000/newrelic`
115
+ 7. Open the web console to see events fire when you navigate or when you
116
+ interact with instrumented components.
117
+
118
+ Code for the isolated version of the plugin can be found inside the [/dev](./dev)
119
+ directory. Changes to the plugin are hot-reloaded.
120
+
121
+ #### Recommended Dev Config
122
+
123
+ Paste this into your `app-config.local.yaml` while developing this plugin:
124
+
125
+ ```yaml
126
+ app:
127
+ analytics:
128
+ newRelic:
129
+ accountId: '1234567'
130
+ applicationId: '987654321'
131
+ licenseKey: 'NRJS-12a3456bc78de9123f4'
132
+ distributedTracingEnabled: true
133
+ cookiesEnabled: true
134
+ useEuEndpoint: false
135
+ ```
package/config.d.ts ADDED
@@ -0,0 +1,59 @@
1
+ /*
2
+ * Copyright 2023 The Backstage Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ export interface Config {
18
+ app: {
19
+ analytics?: {
20
+ newRelic: {
21
+ /**
22
+ * Data ingestion endpoint to use, either bam.eu01.nr-data.net (EU) or bam.nr-data.net (US)
23
+ * @visibility frontend
24
+ */
25
+ endpoint: 'bam.eu01.nr-data.net' | 'bam.nr-data.net';
26
+
27
+ /**
28
+ * New Relic Account ID, e.g. 1234567
29
+ * @visibility frontend
30
+ */
31
+ accountId: string;
32
+
33
+ /**
34
+ * New Relic Application ID, e.g. 987654321
35
+ * @visibility frontend
36
+ */
37
+ applicationId: string;
38
+
39
+ /**
40
+ * New Relic License Key, e.g. NRJS-12a3456bc78de9123f4
41
+ * @visibility frontend
42
+ */
43
+ licenseKey: string;
44
+
45
+ /**
46
+ * Whether to enabled distributed tracing, defaults to false
47
+ * @visibility frontend
48
+ */
49
+ distributedTracingEnabled: boolean;
50
+
51
+ /**
52
+ * Whether to enabled tracing of cookies, defaults to false
53
+ * @visibility frontend
54
+ */
55
+ cookiesEnabled: boolean;
56
+ };
57
+ };
58
+ };
59
+ }
@@ -0,0 +1,24 @@
1
+ import { Config } from '@backstage/config';
2
+ import { AnalyticsApi, IdentityApi, AnalyticsEvent } from '@backstage/core-plugin-api';
3
+ import { AnalyticsApi as AnalyticsApi$1, AnalyticsEvent as AnalyticsEvent$1 } from '@backstage/frontend-plugin-api';
4
+
5
+ /**
6
+ * New Relic Browser API provider for the Backstage Analytics API.
7
+ * @public
8
+ */
9
+ declare class NewRelicBrowser implements AnalyticsApi, AnalyticsApi$1 {
10
+ private readonly agent;
11
+ private constructor();
12
+ static fromConfig(config: Config, options: {
13
+ identityApi?: IdentityApi;
14
+ userIdTransform?: 'sha-256' | ((userEntityRef: string) => Promise<string>);
15
+ }): NewRelicBrowser;
16
+ captureEvent(event: AnalyticsEvent | AnalyticsEvent$1): void;
17
+ /**
18
+ * Simple hash function; relies on web cryptography + the sha-256 algorithm.
19
+ * @param value value to be hashed
20
+ */
21
+ private hash;
22
+ }
23
+
24
+ export { NewRelicBrowser };
@@ -0,0 +1,124 @@
1
+ import { BrowserAgent } from '@newrelic/browser-agent/loaders/browser-agent';
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
5
+ var __publicField = (obj, key, value) => {
6
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
+ return value;
8
+ };
9
+ class NewRelicBrowser {
10
+ constructor(options, identityApi, userIdTransform) {
11
+ __publicField(this, "agent");
12
+ const agentOptions = {
13
+ init: {
14
+ distributed_tracing: {
15
+ enabled: options.distributedTracingEnabled
16
+ },
17
+ privacy: {
18
+ cookies_enabled: options.cookiesEnabled
19
+ },
20
+ ajax: {
21
+ deny_list: [options.endpoint]
22
+ }
23
+ },
24
+ info: {
25
+ beacon: options.endpoint,
26
+ errorBeacon: options.endpoint,
27
+ licenseKey: options.licenseKey,
28
+ applicationID: options.applicationId,
29
+ sa: 1
30
+ },
31
+ loader_config: {
32
+ accountID: options.accountId,
33
+ trustKey: options.accountId,
34
+ agentID: options.applicationId,
35
+ licenseKey: options.licenseKey,
36
+ applicationID: options.applicationId
37
+ }
38
+ };
39
+ this.agent = new BrowserAgent(agentOptions);
40
+ if (identityApi) {
41
+ identityApi.getBackstageIdentity().then((identity) => {
42
+ if (typeof userIdTransform === "function") {
43
+ userIdTransform(identity.userEntityRef).then((userId) => {
44
+ this.agent.setUserId(userId);
45
+ });
46
+ } else {
47
+ this.hash(identity.userEntityRef).then((userId) => {
48
+ this.agent.setUserId(userId);
49
+ });
50
+ }
51
+ });
52
+ }
53
+ }
54
+ static fromConfig(config, options) {
55
+ var _a, _b;
56
+ const newRelicBrowserConfig = config.getConfig("app.analytics.newRelic");
57
+ const browserOptions = {
58
+ endpoint: newRelicBrowserConfig.getString("endpoint"),
59
+ accountId: newRelicBrowserConfig.getString("accountId"),
60
+ applicationId: newRelicBrowserConfig.getString("applicationId"),
61
+ licenseKey: newRelicBrowserConfig.getString("licenseKey"),
62
+ distributedTracingEnabled: (_a = newRelicBrowserConfig.getOptionalBoolean("distributedTracingEnabled")) != null ? _a : false,
63
+ cookiesEnabled: (_b = newRelicBrowserConfig.getOptionalBoolean("cookiesEnabled")) != null ? _b : false
64
+ };
65
+ return new NewRelicBrowser(
66
+ browserOptions,
67
+ options.identityApi,
68
+ options.userIdTransform
69
+ );
70
+ }
71
+ captureEvent(event) {
72
+ const { context, action, subject, value, attributes } = event;
73
+ const extensionId = context.extensionId || context.extension;
74
+ const category = extensionId ? String(extensionId) : "App";
75
+ if (action === "navigate" && category.toLocaleLowerCase("en-US").startsWith("app")) {
76
+ const interaction = this.agent.interaction();
77
+ interaction.setName(subject);
78
+ if (value) {
79
+ interaction.setAttribute("value", value);
80
+ }
81
+ Object.keys(context).forEach((key) => {
82
+ if (context[key]) {
83
+ interaction.setAttribute(`context.${key}`, context[key]);
84
+ }
85
+ });
86
+ if (attributes) {
87
+ Object.keys(attributes).forEach((key) => {
88
+ interaction.setAttribute(`attributes.${key}`, attributes[key]);
89
+ });
90
+ }
91
+ } else {
92
+ const customAttributes = {};
93
+ if (value) {
94
+ customAttributes.value = value;
95
+ }
96
+ Object.keys(context).forEach((key) => {
97
+ if (context[key]) {
98
+ customAttributes[`context.${key}`] = context[key];
99
+ }
100
+ });
101
+ if (attributes) {
102
+ Object.keys(attributes).forEach((key) => {
103
+ customAttributes[`attributes.${key}`] = attributes[key];
104
+ });
105
+ }
106
+ this.agent.addPageAction(action, customAttributes);
107
+ }
108
+ }
109
+ /**
110
+ * Simple hash function; relies on web cryptography + the sha-256 algorithm.
111
+ * @param value value to be hashed
112
+ */
113
+ async hash(value) {
114
+ const digest = await window.crypto.subtle.digest(
115
+ "sha-256",
116
+ new TextEncoder().encode(value)
117
+ );
118
+ const hashArray = Array.from(new Uint8Array(digest));
119
+ return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
120
+ }
121
+ }
122
+
123
+ export { NewRelicBrowser };
124
+ //# sourceMappingURL=index.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.esm.js","sources":["../src/apis/implementations/AnalyticsApi/NewRelicBrowser.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { Config } from '@backstage/config';\nimport {\n AnalyticsApi,\n IdentityApi,\n AnalyticsEvent,\n} from '@backstage/core-plugin-api';\nimport {\n AnalyticsApi as NewAnalyicsApi,\n AnalyticsEvent as NewAnalyticsEvent,\n} from '@backstage/frontend-plugin-api';\nimport { BrowserAgent } from '@newrelic/browser-agent/loaders/browser-agent';\nimport type { setAPI } from '@newrelic/browser-agent/loaders/api/api';\n\ntype NewRelicAPI = ReturnType<typeof setAPI>;\n\ntype NewRelicBrowserOptions = {\n endpoint: string;\n accountId: string;\n applicationId: string;\n licenseKey: string;\n distributedTracingEnabled: boolean;\n cookiesEnabled: boolean;\n};\n\n/**\n * New Relic Browser API provider for the Backstage Analytics API.\n * @public\n */\nexport class NewRelicBrowser implements AnalyticsApi, NewAnalyicsApi {\n private readonly agent: NewRelicAPI;\n\n private constructor(\n options: NewRelicBrowserOptions,\n identityApi?: IdentityApi,\n userIdTransform?: 'sha-256' | ((userEntityRef: string) => Promise<string>),\n ) {\n // Configure the New Relic Browser agent\n const agentOptions = {\n init: {\n distributed_tracing: {\n enabled: options.distributedTracingEnabled,\n },\n privacy: {\n cookies_enabled: options.cookiesEnabled,\n },\n ajax: {\n deny_list: [options.endpoint],\n },\n },\n info: {\n beacon: options.endpoint,\n errorBeacon: options.endpoint,\n licenseKey: options.licenseKey,\n applicationID: options.applicationId,\n sa: 1,\n },\n loader_config: {\n accountID: options.accountId,\n trustKey: options.accountId,\n agentID: options.applicationId,\n licenseKey: options.licenseKey,\n applicationID: options.applicationId,\n },\n };\n\n // Initialize the agent\n this.agent = new BrowserAgent(agentOptions) as unknown as NewRelicAPI;\n\n // Check if identity has been provided\n if (identityApi) {\n identityApi.getBackstageIdentity().then(identity => {\n if (typeof userIdTransform === 'function') {\n userIdTransform(identity.userEntityRef).then(userId => {\n this.agent.setUserId(userId);\n });\n } else {\n this.hash(identity.userEntityRef).then(userId => {\n this.agent.setUserId(userId);\n });\n }\n });\n }\n }\n\n static fromConfig(\n config: Config,\n options: {\n identityApi?: IdentityApi;\n userIdTransform?:\n | 'sha-256'\n | ((userEntityRef: string) => Promise<string>);\n },\n ) {\n const newRelicBrowserConfig = config.getConfig('app.analytics.newRelic');\n const browserOptions: NewRelicBrowserOptions = {\n endpoint: newRelicBrowserConfig.getString('endpoint'),\n accountId: newRelicBrowserConfig.getString('accountId'),\n applicationId: newRelicBrowserConfig.getString('applicationId'),\n licenseKey: newRelicBrowserConfig.getString('licenseKey'),\n distributedTracingEnabled:\n newRelicBrowserConfig.getOptionalBoolean('distributedTracingEnabled') ??\n false,\n cookiesEnabled:\n newRelicBrowserConfig.getOptionalBoolean('cookiesEnabled') ?? false,\n };\n return new NewRelicBrowser(\n browserOptions,\n options.identityApi,\n options.userIdTransform,\n );\n }\n\n captureEvent(event: AnalyticsEvent | NewAnalyticsEvent) {\n const { context, action, subject, value, attributes } = event;\n\n const extensionId = context.extensionId || context.extension;\n const category = extensionId ? String(extensionId) : 'App';\n\n // The legacy default extension was 'App' and the new one is 'app'\n if (\n action === 'navigate' &&\n category.toLocaleLowerCase('en-US').startsWith('app')\n ) {\n const interaction = this.agent.interaction();\n interaction.setName(subject);\n if (value) {\n interaction.setAttribute('value', value);\n }\n Object.keys(context).forEach(key => {\n if (context[key]) {\n interaction.setAttribute(`context.${key}`, context[key]);\n }\n });\n if (attributes) {\n Object.keys(attributes).forEach(key => {\n interaction.setAttribute(`attributes.${key}`, attributes[key]);\n });\n }\n } else {\n const customAttributes: {\n [x: string]: string | number | boolean | undefined;\n } = {};\n if (value) {\n customAttributes.value = value;\n }\n Object.keys(context).forEach(key => {\n if (context[key]) {\n customAttributes[`context.${key}`] = context[key];\n }\n });\n if (attributes) {\n Object.keys(attributes).forEach(key => {\n customAttributes[`attributes.${key}`] = attributes[key];\n });\n }\n\n this.agent.addPageAction(action, customAttributes);\n }\n }\n\n /**\n * Simple hash function; relies on web cryptography + the sha-256 algorithm.\n * @param value value to be hashed\n */\n private async hash(value: string): Promise<string> {\n const digest = await window.crypto.subtle.digest(\n 'sha-256',\n new TextEncoder().encode(value),\n );\n const hashArray = Array.from(new Uint8Array(digest));\n return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');\n }\n}\n"],"names":[],"mappings":";;;;;;;;AA2CO,MAAM,eAAwD,CAAA;AAAA,EAG3D,WAAA,CACN,OACA,EAAA,WAAA,EACA,eACA,EAAA;AANF,IAAiB,aAAA,CAAA,IAAA,EAAA,OAAA,CAAA,CAAA;AAQf,IAAA,MAAM,YAAe,GAAA;AAAA,MACnB,IAAM,EAAA;AAAA,QACJ,mBAAqB,EAAA;AAAA,UACnB,SAAS,OAAQ,CAAA,yBAAA;AAAA,SACnB;AAAA,QACA,OAAS,EAAA;AAAA,UACP,iBAAiB,OAAQ,CAAA,cAAA;AAAA,SAC3B;AAAA,QACA,IAAM,EAAA;AAAA,UACJ,SAAA,EAAW,CAAC,OAAA,CAAQ,QAAQ,CAAA;AAAA,SAC9B;AAAA,OACF;AAAA,MACA,IAAM,EAAA;AAAA,QACJ,QAAQ,OAAQ,CAAA,QAAA;AAAA,QAChB,aAAa,OAAQ,CAA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AAM,aAAc,CAAA,MAAA,EAAQ,gBAAgB,CAAA,CAAA;AAAA,KACnD;AAAA,GACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAc,KAAK,KAAgC,EAAA;AACjD,IAAA,MAAM,MAAS,GAAA,MAAM,MAAO,CAAA,MAAA,CAAO,MAAO,CAAA,MAAA;AAAA,MACxC,SAAA;AAAA,MACA,IAAI,WAAA,EAAc,CAAA,MAAA,CAAO,KAAK,CAAA;AAAA,KAChC,CAAA;AACA,IAAA,MAAM,YAAY,KAAM,CAAA,IAAA,CAAK,IAAI,UAAA,CAAW,MAAM,CAAC,CAAA,CAAA;AACnD,IAAA,OAAO,SAAU,CAAA,GAAA,CAAI,CAAK,CAAA,KAAA,CAAA,CAAE,QAAS,CAAA,EAAE,CAAE,CAAA,QAAA,CAAS,CAAG,EAAA,GAAG,CAAC,CAAA,CAAE,KAAK,EAAE,CAAA,CAAA;AAAA,GACpE;AACF;;;;"}
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@backstage-community/plugin-analytics-module-newrelic-browser",
3
+ "version": "0.1.5",
4
+ "backstage": {
5
+ "role": "frontend-plugin-module"
6
+ },
7
+ "publishConfig": {
8
+ "access": "public",
9
+ "main": "dist/index.esm.js",
10
+ "types": "dist/index.d.ts"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/backstage/community-plugins",
15
+ "directory": "workspaces/analytics/plugins/analytics-module-newrelic-browser"
16
+ },
17
+ "license": "Apache-2.0",
18
+ "sideEffects": false,
19
+ "main": "dist/index.esm.js",
20
+ "types": "dist/index.d.ts",
21
+ "files": [
22
+ "dist",
23
+ "config.d.ts"
24
+ ],
25
+ "scripts": {
26
+ "build": "backstage-cli package build",
27
+ "clean": "backstage-cli package clean",
28
+ "lint": "backstage-cli package lint",
29
+ "prepack": "backstage-cli package prepack",
30
+ "postpack": "backstage-cli package postpack",
31
+ "start": "backstage-cli package start",
32
+ "test": "backstage-cli package test"
33
+ },
34
+ "dependencies": {
35
+ "@backstage/config": "^1.2.0",
36
+ "@backstage/core-components": "^0.14.4",
37
+ "@backstage/core-plugin-api": "^1.9.2",
38
+ "@backstage/frontend-plugin-api": "^0.6.4",
39
+ "@newrelic/browser-agent": "^1.236.0"
40
+ },
41
+ "devDependencies": {
42
+ "@backstage/cli": "^0.26.3",
43
+ "@backstage/dev-utils": "^1.0.31",
44
+ "@testing-library/jest-dom": "^6.0.0",
45
+ "@testing-library/react": "^15.0.0",
46
+ "@types/react": "^18.2.58",
47
+ "canvas": "^2.11.2",
48
+ "react": "^16.13.1 || ^17.0.0 || ^18.0.0",
49
+ "react-dom": "^16.13.1 || ^17.0.0 || ^18.0.0"
50
+ },
51
+ "peerDependencies": {
52
+ "react": "^16.13.1 || ^17.0.0 || ^18.0.0"
53
+ },
54
+ "configSchema": "config.d.ts",
55
+ "module": "./dist/index.esm.js"
56
+ }