Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: convert analytics context from string to enum #1551

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions apps/web/contexts/Analytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import logEvent, {
ActionType,
AnalyticsEventImportance,
CCAEventData,
AnalyticsContext,
} from 'libs/base-ui/utils/logEvent';
import { ReactNode, createContext, useCallback, useContext, useMemo } from 'react';

Expand All @@ -12,15 +13,15 @@ export type AnalyticsContextProps = {
fullContext: string;
};

export const AnalyticsContext = createContext<AnalyticsContextProps>({
export const AnalyticsReactContext = createContext<AnalyticsContextProps>({
logEventWithContext: function () {
return undefined;
},
fullContext: '',
});

export function useAnalytics() {
const context = useContext(AnalyticsContext);
const context = useContext(AnalyticsReactContext);
if (context === undefined) {
throw new Error('useAnalytics must be used within a AnalyticsProvider');
}
Expand All @@ -29,7 +30,7 @@ export function useAnalytics() {

type AnalyticsProviderProps = {
children?: ReactNode;
context: string; // TODO: This could be an enum in CCAEventData
context: AnalyticsContext;
};

export default function AnalyticsProvider({ children, context }: AnalyticsProviderProps) {
Expand All @@ -41,10 +42,10 @@ export default function AnalyticsProvider({ children, context }: AnalyticsProvid
const sanitizedEventName = eventName.toLocaleLowerCase();
if (typeof window === 'undefined') return;
logEvent(
sanitizedEventName, // TODO: Do we want context here?
sanitizedEventName,
{
action: action,
context: fullContext,
context: fullContext as AnalyticsContext,
page_path: window.location.pathname,
...eventData,
},
Expand All @@ -58,5 +59,5 @@ export default function AnalyticsProvider({ children, context }: AnalyticsProvid
return { logEventWithContext, context, fullContext };
}, [context, fullContext, logEventWithContext]);

return <AnalyticsContext.Provider value={values}>{children}</AnalyticsContext.Provider>;
return <AnalyticsReactContext.Provider value={values}>{children}</AnalyticsReactContext.Provider>;
}
23 changes: 21 additions & 2 deletions libs/base-ui/utils/logEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ enum AnalyticsEventImportance {
high = 'high',
}

enum AnalyticsContext {
// Navigation/UI contexts
Navbar = 'navbar',
Build = 'build',
Explore = 'explore',
Community = 'community',
About = 'about',
Socials = 'socials',

// Feature-specific contexts
BasenamesClaimFrame = 'basenames_claim_frame',
SliceSo = 'slice.so',
HypersubXyz = 'hypersub.xyz',
HighlightXyz = 'highlight.xyz',
EventsXyz = 'events.xyz',
PaycasterCo = 'paycaster.co',
SocialDex = 'social-dex',
}

type CCAEventData = {
// Standard Attributes
action?: ActionType;
Expand All @@ -65,7 +84,7 @@ type CCAEventData = {
message_id?: number;
response_helpful?: boolean;
address?: string;
context?: string;
context?: AnalyticsContext;
userId?: string;
error?: string;
wallet_type?: string;
Expand Down Expand Up @@ -124,5 +143,5 @@ export function identify(event: CCAEventData) {
}
}

export { ActionType, AnalyticsEventImportance, ComponentType };
export { ActionType, AnalyticsEventImportance, ComponentType, AnalyticsContext };
export type { AnalyticsEventData, LogEvent, CCAEventData };