Skip to content

Commit

Permalink
Add identify and page view tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
pilhuhn committed Nov 27, 2024
1 parent dce41f3 commit 4916d71
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ const initProps: InitProps = {
};

const tracking = getTrackingProviders(initProps);
tracking.identify('user-123');
tracking.trackPageView();

const actionEventName = 'MessageAction';
const initialMessages: MessageProps[] = [
Expand Down
9 changes: 7 additions & 2 deletions packages/module/src/tracking/console_tracking_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand All @@ -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);
Expand Down
32 changes: 22 additions & 10 deletions packages/module/src/tracking/segment_tracking_provider.ts
Original file line number Diff line number Diff line change
@@ -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(
{
Expand All @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion packages/module/src/tracking/trackingProviderProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/module/src/tracking/tracking_api.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 4916d71

Please sign in to comment.