Review feedback.

Signed-off-by: Eric Peterson <ericpeterson@spotify.com>
This commit is contained in:
Eric Peterson
2021-09-05 21:19:33 +02:00
parent c582819a01
commit edd5293d68
18 changed files with 78 additions and 56 deletions
@@ -1,8 +1,8 @@
---
'@backstage/plugin-analytics-provider-ga': patch
'@backstage/plugin-analytics-module-ga': patch
---
Initial Google Analytics API provider for the Backstage Analytics API, which:
Initial Google Analytics implementation for the Backstage Analytics API, which:
- Handles pageview and custom event instrumentation idiomatically.
- Enables custom dimension and metric collection via app config.
@@ -41,39 +41,46 @@ const getExtensionDomain = (
pathname: string,
routes: RouteObjects,
): RoutableAnalyticsDomain | {} => {
const cleanPath = pathname.replace(/\/+$/, '');
const matches = matchRoutes(routes, { pathname });
const RouteElement = matches
?.filter(match => {
const pathsMatch = match.pathname.replace(/\/+$/, '') === cleanPath;
const hasRoutableElement = !!(match.route.element as React.ReactElement)
?.props?.element;
return pathsMatch && hasRoutableElement;
})
.pop()?.route?.element;
const RoutableElement = (RouteElement as React.ReactElement)?.props?.element;
try {
const cleanPath = pathname.replace(/\/+$/, '');
const matches = matchRoutes(routes, { pathname });
const RouteElement = matches
?.filter(match => {
const pathsMatch = match.pathname.replace(/\/+$/, '') === cleanPath;
const hasRoutableElement = !!(match.route.element as React.ReactElement)
?.props?.element;
return pathsMatch && hasRoutableElement;
})
.pop()?.route?.element;
const RoutableElement = (RouteElement as React.ReactElement)?.props
?.element;
if (RoutableElement) {
const plugin: BackstagePlugin | undefined = getComponentData(
RoutableElement,
'core.plugin',
);
const mountPoint: { id?: string } | undefined = getComponentData(
RoutableElement,
'core.mountPoint',
);
if (plugin && mountPoint) {
return {
pluginId: plugin.getId(),
componentName: 'App',
routeRef: mountPoint?.id || '',
};
if (RoutableElement) {
const plugin: BackstagePlugin | undefined = getComponentData(
RoutableElement,
'core.plugin',
);
const mountPoint: { id?: string } | undefined = getComponentData(
RoutableElement,
'core.mountPoint',
);
if (plugin && mountPoint) {
return {
pluginId: plugin.getId(),
componentName: 'App',
routeRef: mountPoint?.id || '',
};
}
}
}
// Try again, one path-level shallower.
const nextLevelPath = cleanPath.split('/').slice(0, -1).join('/');
return nextLevelPath !== '' ? getExtensionDomain(nextLevelPath, routes) : {};
// Try again, one path-level shallower.
const nextLevelPath = cleanPath.split('/').slice(0, -1).join('/');
return nextLevelPath !== ''
? getExtensionDomain(nextLevelPath, routes)
: {};
} catch {
return {};
}
};
/**
@@ -1,4 +1,4 @@
# Analytics Provider: Google Analytics
# Analytics Module: Google Analytics
This plugin provides an opinionated implementation of the Backstage Analytics
API for Google Analytics. Once installed and configured, analytics events will
@@ -8,9 +8,10 @@ This plugin contains no (and will never contain any) other functionality.
## Installation
1. Install this plugin in your Backstage instance: `yarn add @backstage/plugin-analytics-provider-ga`
1. Install this plugin in your Backstage app:
`cd packages/app && yarn add @backstage/plugin-analytics-module-ga`
2. Register the plugin with your App. In most instances of Backstage, this is
as simple as adding `export { analyticsProviderGA } from '@backstage/plugin-analytics-provider-ga';`
as simple as adding `export { analyticsModuleGA } from '@backstage/plugin-analytics-module-ga';`
to your `packages/app/src/plugins.ts` file.
3. Configure the plugin (see below).
+14
View File
@@ -0,0 +1,14 @@
## API Report File for "@backstage/plugin-analytics-module-ga"
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts
import { BackstagePlugin } from '@backstage/core-plugin-api';
// Warning: (ae-missing-release-tag) "analyticsModuleGA" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
export const analyticsModuleGA: BackstagePlugin<{}, {}>;
// (No @packageDocumentation comment for this package)
```
@@ -16,7 +16,10 @@
export interface Config {
app: {
analytics: {
// TODO: Only marked as optional because backstage-cli config:check in the
// context of the monorepo is too strict. Ideally, this would be marked as
// required.
analytics?: {
provider: 'ga';
ga: {
/**
@@ -15,11 +15,11 @@
*/
import React from 'react';
import { createDevApp } from '@backstage/dev-utils';
import { analyticsProviderGA } from '../src/plugin';
import { analyticsModuleGA } from '../src/plugin';
import { Playground } from './Playground';
createDevApp()
.registerPlugin(analyticsProviderGA)
.registerPlugin(analyticsModuleGA)
.addPage({
path: '/ga',
title: 'GA Playground',
@@ -1,5 +1,5 @@
{
"name": "@backstage/plugin-analytics-provider-ga",
"name": "@backstage/plugin-analytics-module-ga",
"version": "0.1.1",
"main": "src/index.ts",
"types": "src/index.ts",
@@ -21,10 +21,10 @@
},
"dependencies": {
"@backstage/config": "^0.1.5",
"@backstage/core-components": "^0.1.6",
"@backstage/core-plugin-api": "^0.1.3",
"@backstage/theme": "^0.2.8",
"@material-ui/core": "^4.11.0",
"@backstage/core-components": "^0.2.0",
"@backstage/core-plugin-api": "^0.1.4",
"@backstage/theme": "^0.2.9",
"@material-ui/core": "^4.12.2",
"@material-ui/icons": "^4.9.1",
"@material-ui/lab": "4.0.0-alpha.45",
"react": "^16.13.1",
@@ -33,10 +33,10 @@
"react-use": "^17.2.4"
},
"devDependencies": {
"@backstage/cli": "^0.7.4",
"@backstage/core-app-api": "^0.1.4",
"@backstage/dev-utils": "^0.2.2",
"@backstage/test-utils": "^0.1.14",
"@backstage/cli": "^0.7.6",
"@backstage/core-app-api": "^0.1.6",
"@backstage/dev-utils": "^0.2.4",
"@backstage/test-utils": "^0.1.16",
"@testing-library/jest-dom": "^5.10.1",
"@testing-library/react": "^11.2.5",
"@testing-library/user-event": "^13.1.8",
@@ -13,4 +13,4 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { analyticsProviderGA } from './plugin';
export { analyticsModuleGA } from './plugin';
@@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { analyticsProviderGA } from './plugin';
import { analyticsModuleGA } from './plugin';
describe('google-analytics', () => {
it('should export plugin', () => {
expect(analyticsProviderGA).toBeDefined();
expect(analyticsModuleGA).toBeDefined();
});
});
@@ -21,7 +21,7 @@ import {
} from '@backstage/core-plugin-api';
import { GoogleAnalytics } from './apis/implementations/AnalyticsApi';
export const analyticsProviderGA = createPlugin({
export const analyticsModuleGA = createPlugin({
id: 'analytics-provider-ga',
apis: [
createApiFactory({
@@ -1,3 +0,0 @@
app:
analytics:
provider: ga
@@ -75,8 +75,8 @@ describe('SearchPage', () => {
const expectedFilters = { [expectedFilterField]: expectedFilterValue };
const expectedPageCursor = 'SOMEPAGE';
// e.g. ?query=petstore&pageCursor=SOMEPAGE&filters[lifecycle][]=experimental&filters[kind]=Component
(useLocation as jest.Mock).mockReturnValueOnce({
// e.g. ?query=petstore&pageCursor=1&filters[lifecycle][]=experimental&filters[kind]=Component
(useLocation as jest.Mock).mockReturnValue({
search: `?query=${expectedTerm}&types[]=${expectedTypes[0]}&filters[${expectedFilterField}]=${expectedFilterValue}&pageCursor=${expectedPageCursor}`,
});