From f9e4c279e2eb03e5b55a3a4f814e81748eefdaa6 Mon Sep 17 00:00:00 2001 From: Vincenzo Scamporlino Date: Wed, 26 Apr 2023 22:16:43 +0200 Subject: [PATCH] Revert "core-plugin-api: useAnalytics clean up" This reverts commit d7d3e471e1c480cd9ad171840833c551a8af268f. Signed-off-by: Vincenzo Scamporlino --- .changeset/mean-seas-tan.md | 5 -- .../core-plugin-api/src/analytics/Tracker.ts | 59 +++++++++++++++++++ .../src/analytics/useAnalytics.tsx | 44 ++++++-------- 3 files changed, 76 insertions(+), 32 deletions(-) delete mode 100644 .changeset/mean-seas-tan.md create mode 100644 packages/core-plugin-api/src/analytics/Tracker.ts diff --git a/.changeset/mean-seas-tan.md b/.changeset/mean-seas-tan.md deleted file mode 100644 index 9d99016f45..0000000000 --- a/.changeset/mean-seas-tan.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@backstage/core-plugin-api': patch ---- - -Minor improvements to `useAnalytics` hook diff --git a/packages/core-plugin-api/src/analytics/Tracker.ts b/packages/core-plugin-api/src/analytics/Tracker.ts new file mode 100644 index 0000000000..51e99bff14 --- /dev/null +++ b/packages/core-plugin-api/src/analytics/Tracker.ts @@ -0,0 +1,59 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { + AnalyticsApi, + AnalyticsEventAttributes, + AnalyticsTracker, +} from '../apis'; +import { AnalyticsContextValue } from './'; + +export class Tracker implements AnalyticsTracker { + constructor( + private readonly analyticsApi: AnalyticsApi, + private context: AnalyticsContextValue = { + routeRef: 'unknown', + pluginId: 'root', + extension: 'App', + }, + ) {} + + setContext(context: AnalyticsContextValue) { + this.context = context; + } + + captureEvent( + action: string, + subject: string, + { + value, + attributes, + }: { value?: number; attributes?: AnalyticsEventAttributes } = {}, + ) { + try { + this.analyticsApi.captureEvent({ + action, + subject, + value, + attributes, + context: this.context, + }); + } catch (e) { + // eslint-disable-next-line no-console + console.warn('Error during analytics event capture. %o', e); + } + } +} diff --git a/packages/core-plugin-api/src/analytics/useAnalytics.tsx b/packages/core-plugin-api/src/analytics/useAnalytics.tsx index 29017c167f..5a047e5a63 100644 --- a/packages/core-plugin-api/src/analytics/useAnalytics.tsx +++ b/packages/core-plugin-api/src/analytics/useAnalytics.tsx @@ -20,9 +20,9 @@ import { AnalyticsTracker, AnalyticsApi, useApi, - AnalyticsEventAttributes, } from '../apis'; -import { useMemo } from 'react'; +import { useRef } from 'react'; +import { Tracker } from './Tracker'; function useAnalyticsApi(): AnalyticsApi { try { @@ -38,32 +38,22 @@ function useAnalyticsApi(): AnalyticsApi { * @public */ export function useAnalytics(): AnalyticsTracker { + const trackerRef = useRef(null); const context = useAnalyticsContext(); + // Our goal is to make this API truly optional for any/all consuming code + // (including tests). This hook runs last to ensure hook order is, as much as + // possible, maintained. const analyticsApi = useAnalyticsApi(); - return useMemo(() => { - return { - captureEvent( - action: string, - subject: string, - { - value, - attributes, - }: { value?: number; attributes?: AnalyticsEventAttributes } = {}, - ) { - try { - analyticsApi.captureEvent({ - action, - subject, - value, - attributes, - context, - }); - } catch (e) { - // eslint-disable-next-line no-console - console.warn('Error during analytics event capture. %o', e); - } - }, - }; - }, [analyticsApi, context]); + function getTracker(): Tracker { + if (trackerRef.current === null) { + trackerRef.current = new Tracker(analyticsApi); + } + return trackerRef.current; + } + + const tracker = getTracker(); + tracker.setContext(context); + + return tracker; }