From d23ce9548df51f0b2f3207a64fb822eb47f45cbf Mon Sep 17 00:00:00 2001 From: Vincenzo Scamporlino Date: Fri, 21 Apr 2023 14:53:46 +0200 Subject: [PATCH] core-plugin-api: useAnalytics clean up Signed-off-by: Vincenzo Scamporlino --- .../core-plugin-api/src/analytics/Tracker.ts | 59 ------------------- .../src/analytics/useAnalytics.tsx | 44 ++++++++------ 2 files changed, 27 insertions(+), 76 deletions(-) delete mode 100644 packages/core-plugin-api/src/analytics/Tracker.ts diff --git a/packages/core-plugin-api/src/analytics/Tracker.ts b/packages/core-plugin-api/src/analytics/Tracker.ts deleted file mode 100644 index 51e99bff14..0000000000 --- a/packages/core-plugin-api/src/analytics/Tracker.ts +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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 5a047e5a63..29017c167f 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 { useRef } from 'react'; -import { Tracker } from './Tracker'; +import { useMemo } from 'react'; function useAnalyticsApi(): AnalyticsApi { try { @@ -38,22 +38,32 @@ 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(); - function getTracker(): Tracker { - if (trackerRef.current === null) { - trackerRef.current = new Tracker(analyticsApi); - } - return trackerRef.current; - } - - const tracker = getTracker(); - tracker.setContext(context); - - return tracker; + 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]); }