From 4916d719228c50ccf01f8ae5ae09157353105a27 Mon Sep 17 00:00:00 2001 From: "Heiko W. Rupp" Date: Wed, 27 Nov 2024 09:49:14 +0100 Subject: [PATCH] Add identify and page view tracking --- .../chatbot/examples/demos/Chatbot.tsx | 2 ++ .../src/tracking/console_tracking_provider.ts | 9 ++++-- .../src/tracking/segment_tracking_provider.ts | 32 +++++++++++++------ .../src/tracking/trackingProviderProxy.ts | 8 ++++- packages/module/src/tracking/tracking_api.ts | 4 ++- 5 files changed, 41 insertions(+), 14 deletions(-) diff --git a/packages/module/patternfly-docs/content/extensions/chatbot/examples/demos/Chatbot.tsx b/packages/module/patternfly-docs/content/extensions/chatbot/examples/demos/Chatbot.tsx index 60459a89..39b3b2b4 100644 --- a/packages/module/patternfly-docs/content/extensions/chatbot/examples/demos/Chatbot.tsx +++ b/packages/module/patternfly-docs/content/extensions/chatbot/examples/demos/Chatbot.tsx @@ -103,6 +103,8 @@ const initProps: InitProps = { }; const tracking = getTrackingProviders(initProps); +tracking.identify('user-123'); +tracking.trackPageView(); const actionEventName = 'MessageAction'; const initialMessages: MessageProps[] = [ diff --git a/packages/module/src/tracking/console_tracking_provider.ts b/packages/module/src/tracking/console_tracking_provider.ts index 0a92a9c7..77a6866a 100644 --- a/packages/module/src/tracking/console_tracking_provider.ts +++ b/packages/module/src/tracking/console_tracking_provider.ts @@ -2,9 +2,9 @@ import { TrackingSpi } from './tracking_spi'; import { TrackingApi } from './tracking_api'; export class ConsoleTrackingProvider implements TrackingSpi, TrackingApi { - trackPageView(url: any) { + trackPageView(url: string | undefined) { // eslint-disable-next-line no-console - console.log('ConsoleProvider', url); + console.log('ConsoleProvider pageView', url); } // eslint-disable-next-line @typescript-eslint/no-empty-function registerProvider(): void {} @@ -14,6 +14,11 @@ export class ConsoleTrackingProvider implements TrackingSpi, TrackingApi { console.log('ConsoleProvider initialize'); } + identify(userID: string): void { + // eslint-disable-next-line no-console + console.log('ConsoleProvider identify', userID); + } + trackSingleItem(item: string, options: string | undefined): void { // eslint-disable-next-line no-console console.log('ConsoleProvider: ' + item, options); diff --git a/packages/module/src/tracking/segment_tracking_provider.ts b/packages/module/src/tracking/segment_tracking_provider.ts index 60367e1d..5ee38f65 100644 --- a/packages/module/src/tracking/segment_tracking_provider.ts +++ b/packages/module/src/tracking/segment_tracking_provider.ts @@ -1,25 +1,17 @@ import { AnalyticsBrowser } from '@segment/analytics-next'; import { TrackingApi } from './tracking_api'; -import { InitProps, TrackingSpi } from "./tracking_spi"; - +import { InitProps, TrackingSpi } from './tracking_spi'; export class SegmentTrackingProvider implements TrackingSpi, TrackingApi { private analytics: AnalyticsBrowser | undefined; - trackPageView(url: any): void { - // eslint-disable-next-line no-console - console.log('SegmentProvider url', url); - if (this.analytics) { - this.analytics.page(url); - } - } // eslint-disable-next-line @typescript-eslint/no-empty-function registerProvider(): void {} initialize(props: InitProps): void { // eslint-disable-next-line no-console console.log('SegmentProvider initialize'); - const segmentKey = 'qylQB4US91okwS4xtHIxtnka9FFHcC7g'; // TODO add your key here + const segmentKey = props.segmentKey as string; this.analytics = AnalyticsBrowser.load( { @@ -38,6 +30,26 @@ export class SegmentTrackingProvider implements TrackingSpi, TrackingApi { ); } + identify(userID: string): void { + // eslint-disable-next-line no-console + console.log('SegmentProvider userID: ' + userID); + if (this.analytics) { + this.analytics.identify(userID); + } + } + + trackPageView(url: string | undefined): void { + // eslint-disable-next-line no-console + console.log('SegmentProvider url', url); + if (this.analytics) { + if (url) { + this.analytics.page(url); + } else { + this.analytics.page(); // Uses window.url + } + } + } + trackSingleItem(item: string, options: string | undefined): void { // eslint-disable-next-line no-console console.log('SegmentProvider: trackSingleItem' + item, options); diff --git a/packages/module/src/tracking/trackingProviderProxy.ts b/packages/module/src/tracking/trackingProviderProxy.ts index c0eb741a..d9cfaba4 100644 --- a/packages/module/src/tracking/trackingProviderProxy.ts +++ b/packages/module/src/tracking/trackingProviderProxy.ts @@ -6,13 +6,19 @@ class TrackingProviderProxy implements TrackingApi { this.providers = providers; } + identify(userID: string): void { + for (const provider of this.providers) { + provider.identify(userID); + } + } + trackSingleItem(eventName: string, options: string | undefined): void { for (const provider of this.providers) { provider.trackSingleItem(eventName, options); } } - trackPageView(url: any) { + trackPageView(url: string | undefined): void { for (const provider of this.providers) { provider.trackPageView(url); } diff --git a/packages/module/src/tracking/tracking_api.ts b/packages/module/src/tracking/tracking_api.ts index 5b498bb5..e5aa9d4b 100644 --- a/packages/module/src/tracking/tracking_api.ts +++ b/packages/module/src/tracking/tracking_api.ts @@ -1,5 +1,7 @@ export interface TrackingApi { - trackPageView: (url: string) => void; + identify: (userID: string) => void; + + trackPageView: (url: string | undefined) => void; trackSingleItem: (eventName: string, options: string | undefined) => void; }