@backstage-community/plugin-analytics-module-newrelic-browser 0.8.0 → 0.10.0
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
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @backstage-community/plugin-analytics-module-newrelic-browser
|
|
2
2
|
|
|
3
|
+
## 0.10.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 7eb926b: Adding a sessionReplay config block to the plugin options, allowing users to enable/disable session replay and set options like sampling rate, error sampling rate, masking selectors, and block selectors.
|
|
8
|
+
|
|
9
|
+
## 0.9.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- 3d6351f: Backstage version bump to v1.42.4
|
|
14
|
+
|
|
3
15
|
## 0.8.0
|
|
4
16
|
|
|
5
17
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -68,8 +68,58 @@ app:
|
|
|
68
68
|
...
|
|
69
69
|
distributedTracing: true
|
|
70
70
|
cookiesEnabled: true
|
|
71
|
+
sessionReplay:
|
|
72
|
+
enabled: true
|
|
73
|
+
samplingRate: 90
|
|
74
|
+
errorSamplingRate: 90
|
|
75
|
+
# Optional:
|
|
76
|
+
# maskTextSelector: 'input[type="password"]'
|
|
77
|
+
# maskAllInputs: true
|
|
78
|
+
# blockSelector: '.private'
|
|
71
79
|
```
|
|
72
80
|
|
|
81
|
+
See [`config.d.ts`](./config.d.ts) for all available options.
|
|
82
|
+
|
|
83
|
+
## Session Replay
|
|
84
|
+
|
|
85
|
+
Session Replay can be enabled via the `sessionReplay` config block. This allows you to record and replay user sessions for debugging and UX analysis.
|
|
86
|
+
|
|
87
|
+
### Troubleshooting: Manual Session Replay
|
|
88
|
+
|
|
89
|
+
If you notice that sessions are **not being recorded automatically**, you can force a manual session replay start by injecting the following React component into your app:
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import { useEffect } from 'react';
|
|
93
|
+
|
|
94
|
+
const UseStartReplay = () => {
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
const interval = setInterval(() => {
|
|
97
|
+
if ((window?.newrelic as any)?.recordReplay) {
|
|
98
|
+
(window.newrelic as any).recordReplay();
|
|
99
|
+
clearInterval(interval);
|
|
100
|
+
}
|
|
101
|
+
}, 100);
|
|
102
|
+
return () => clearInterval(interval);
|
|
103
|
+
}, []);
|
|
104
|
+
return null;
|
|
105
|
+
};
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Then, add it to your app’s router:
|
|
109
|
+
|
|
110
|
+
```tsx
|
|
111
|
+
<AppRouter>
|
|
112
|
+
<UseStartReplay />
|
|
113
|
+
<Root>{routes}</Root>
|
|
114
|
+
</AppRouter>
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
This will ensure session recording starts as soon as the New Relic agent is ready.
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
For more details, see the [New Relic Browser documentation](https://docs.newrelic.com/docs/browser/).
|
|
122
|
+
|
|
73
123
|
### User IDs
|
|
74
124
|
|
|
75
125
|
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.
|
package/config.d.ts
CHANGED
|
@@ -53,6 +53,41 @@ export interface Config {
|
|
|
53
53
|
* @visibility frontend
|
|
54
54
|
*/
|
|
55
55
|
cookiesEnabled: boolean;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Session Replay configuration
|
|
59
|
+
* @deepVisibility frontend
|
|
60
|
+
*/
|
|
61
|
+
sessionReplay?: {
|
|
62
|
+
/**
|
|
63
|
+
* Whether to enable Session Replay, defaults to false
|
|
64
|
+
*/
|
|
65
|
+
enabled: boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Sampling rate for session replay, defaults to 0
|
|
68
|
+
*/
|
|
69
|
+
samplingRate?: number;
|
|
70
|
+
/**
|
|
71
|
+
* Error sampling rate for session replay, defaults to 100
|
|
72
|
+
*/
|
|
73
|
+
errorSamplingRate?: number;
|
|
74
|
+
/**
|
|
75
|
+
* Whether to mask all inputs, defaults to false
|
|
76
|
+
*/
|
|
77
|
+
maskTextSelector?: string;
|
|
78
|
+
/**
|
|
79
|
+
* Whether to mask all inputs, defaults to true
|
|
80
|
+
*/
|
|
81
|
+
maskAllInputs?: boolean;
|
|
82
|
+
/**
|
|
83
|
+
* Whether to collect fonts, defaults to true
|
|
84
|
+
*/
|
|
85
|
+
collectFonts?: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Block selector for session replay, defaults to true
|
|
88
|
+
*/
|
|
89
|
+
blockSelector?: string;
|
|
90
|
+
};
|
|
56
91
|
};
|
|
57
92
|
};
|
|
58
93
|
};
|
|
@@ -13,6 +13,36 @@ class NewRelicBrowser {
|
|
|
13
13
|
},
|
|
14
14
|
ajax: {
|
|
15
15
|
deny_list: [options.endpoint]
|
|
16
|
+
},
|
|
17
|
+
session_replay: {
|
|
18
|
+
enabled: options.sessionReplay?.enabled || false,
|
|
19
|
+
block_selector: options.sessionReplay?.blockSelector || "",
|
|
20
|
+
mask_text_selector: options.sessionReplay?.maskTextSelector || "",
|
|
21
|
+
sampling_rate: options.sessionReplay?.samplingRate || 0,
|
|
22
|
+
error_sampling_rate: options.sessionReplay?.errorSamplingRate || 0,
|
|
23
|
+
mask_all_inputs: options.sessionReplay?.maskAllInputs || false,
|
|
24
|
+
collect_fonts: true,
|
|
25
|
+
inline_images: false,
|
|
26
|
+
inline_stylesheet: true,
|
|
27
|
+
fix_stylesheets: true,
|
|
28
|
+
preload: true,
|
|
29
|
+
mask_input_options: {
|
|
30
|
+
color: true,
|
|
31
|
+
date: true,
|
|
32
|
+
datetime_local: true,
|
|
33
|
+
email: true,
|
|
34
|
+
month: true,
|
|
35
|
+
number: true,
|
|
36
|
+
range: true,
|
|
37
|
+
search: true,
|
|
38
|
+
tel: true,
|
|
39
|
+
text: true,
|
|
40
|
+
time: true,
|
|
41
|
+
url: true,
|
|
42
|
+
week: true,
|
|
43
|
+
select: true,
|
|
44
|
+
textarea: true
|
|
45
|
+
}
|
|
16
46
|
}
|
|
17
47
|
},
|
|
18
48
|
info: {
|
|
@@ -53,7 +83,11 @@ class NewRelicBrowser {
|
|
|
53
83
|
applicationId: newRelicBrowserConfig.getString("applicationId"),
|
|
54
84
|
licenseKey: newRelicBrowserConfig.getString("licenseKey"),
|
|
55
85
|
distributedTracingEnabled: newRelicBrowserConfig.getOptionalBoolean("distributedTracingEnabled") ?? false,
|
|
56
|
-
cookiesEnabled: newRelicBrowserConfig.getOptionalBoolean("cookiesEnabled") ?? false
|
|
86
|
+
cookiesEnabled: newRelicBrowserConfig.getOptionalBoolean("cookiesEnabled") ?? false,
|
|
87
|
+
sessionReplay: {
|
|
88
|
+
...newRelicBrowserConfig.getOptionalConfig("sessionReplay")?.get()
|
|
89
|
+
}
|
|
90
|
+
// Ensure we get the session replay config as plain object
|
|
57
91
|
};
|
|
58
92
|
return new NewRelicBrowser(
|
|
59
93
|
browserOptions,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NewRelicBrowser.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"],"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B,CAAc,WAAA,EAAA,GAAG,CAAE,CAAA,CAAA,GAAI,WAAW,GAAG,CAAA;AAAA,SACvD,CAAA;AAAA;AAGH,MAAK,IAAA,CAAA,KAAA,CAAM,aAAc,CAAA,MAAA,EAAQ,gBAAgB,CAAA;AAAA;AACnD;AACF;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;AAAA,KAChC;AACA,IAAA,MAAM,YAAY,KAAM,CAAA,IAAA,CAAK,IAAI,UAAA,CAAW,MAAM,CAAC,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;AAAA;AAEtE;;;;"}
|
|
1
|
+
{"version":3,"file":"NewRelicBrowser.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';\n\ntype NewRelicBrowserOptions = {\n endpoint: string;\n accountId: string;\n applicationId: string;\n licenseKey: string;\n distributedTracingEnabled: boolean;\n cookiesEnabled: boolean;\n sessionReplay?: {\n enabled?: boolean;\n samplingRate?: number;\n errorSamplingRate?: number;\n maskTextSelector?: string;\n maskAllInputs?: boolean;\n blockSelector?: string;\n };\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: BrowserAgent;\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 session_replay: {\n enabled: options.sessionReplay?.enabled || false,\n block_selector: options.sessionReplay?.blockSelector || '',\n mask_text_selector: options.sessionReplay?.maskTextSelector || '',\n sampling_rate: options.sessionReplay?.samplingRate || 0,\n error_sampling_rate: options.sessionReplay?.errorSamplingRate || 0,\n mask_all_inputs: options.sessionReplay?.maskAllInputs || false,\n collect_fonts: true,\n inline_images: false,\n inline_stylesheet: true,\n fix_stylesheets: true,\n preload: true,\n mask_input_options: {\n color: true,\n date: true,\n datetime_local: true,\n email: true,\n month: true,\n number: true,\n range: true,\n search: true,\n tel: true,\n text: true,\n time: true,\n url: true,\n week: true,\n select: true,\n textarea: true,\n },\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);\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 sessionReplay: {\n ...newRelicBrowserConfig.getOptionalConfig('sessionReplay')?.get(),\n }, // Ensure we get the session replay config as plain object\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":";;AAgDO,MAAM,eAAwD,CAAA;AAAA,EAClD,KAAA;AAAA,EAET,WAAA,CACN,OACA,EAAA,WAAA,EACA,eACA,EAAA;AAEA,IAAA,MAAM,YAAe,GAAA;AAAA,MACnB,IAAM,EAAA;AAAA,QACJ,mBAAqB,EAAA;AAAA,UACnB,SAAS,OAAQ,CAAA;AAAA,SACnB;AAAA,QACA,OAAS,EAAA;AAAA,UACP,iBAAiB,OAAQ,CAAA;AAAA,SAC3B;AAAA,QACA,IAAM,EAAA;AAAA,UACJ,SAAA,EAAW,CAAC,OAAA,CAAQ,QAAQ;AAAA,SAC9B;AAAA,QACA,cAAgB,EAAA;AAAA,UACd,OAAA,EAAS,OAAQ,CAAA,aAAA,EAAe,OAAW,IAAA,KAAA;AAAA,UAC3C,cAAA,EAAgB,OAAQ,CAAA,aAAA,EAAe,aAAiB,IAAA,EAAA;AAAA,UACxD,kBAAA,EAAoB,OAAQ,CAAA,aAAA,EAAe,gBAAoB,IAAA,EAAA;AAAA,UAC/D,aAAA,EAAe,OAAQ,CAAA,aAAA,EAAe,YAAgB,IAAA,CAAA;AAAA,UACtD,mBAAA,EAAqB,OAAQ,CAAA,aAAA,EAAe,iBAAqB,IAAA,CAAA;AAAA,U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4
4
|
"backstage": {
|
|
5
5
|
"role": "frontend-plugin-module",
|
|
6
6
|
"pluginId": "app",
|
|
@@ -34,15 +34,15 @@
|
|
|
34
34
|
"test": "backstage-cli package test"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@backstage/config": "^1.3.
|
|
38
|
-
"@backstage/core-components": "^0.17.
|
|
39
|
-
"@backstage/core-plugin-api": "^1.10.
|
|
40
|
-
"@backstage/frontend-plugin-api": "^0.
|
|
41
|
-
"@newrelic/browser-agent": "^1.
|
|
37
|
+
"@backstage/config": "^1.3.3",
|
|
38
|
+
"@backstage/core-components": "^0.17.5",
|
|
39
|
+
"@backstage/core-plugin-api": "^1.10.9",
|
|
40
|
+
"@backstage/frontend-plugin-api": "^0.11.0",
|
|
41
|
+
"@newrelic/browser-agent": "^1.295.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@backstage/cli": "^0.
|
|
45
|
-
"@backstage/dev-utils": "^1.1.
|
|
44
|
+
"@backstage/cli": "^0.34.1",
|
|
45
|
+
"@backstage/dev-utils": "^1.1.13",
|
|
46
46
|
"@testing-library/jest-dom": "^6.0.0",
|
|
47
47
|
"@testing-library/react": "^15.0.0",
|
|
48
48
|
"@types/react": "^18.2.58",
|