core-plugin-api: useAnalytics clean up
Signed-off-by: Vincenzo Scamporlino <vincenzos@spotify.com>
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<Tracker | null>(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]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user