Skip to content

Commit

Permalink
[GAL-4259] include isBetaTester in events (#1902)
Browse files Browse the repository at this point in the history
* include isBetaTester in events

* add function to web too
  • Loading branch information
kaitoo1 authored Sep 12, 2023
1 parent cc91190 commit 7baf636
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
11 changes: 10 additions & 1 deletion apps/mobile/src/contexts/MobileAnalyticsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { PropsWithChildren } from 'react';
import { env } from '~/env/runtime';
import AnalyticsProvider, {
IdentifyFunction,
RegisterSuperPropertiesFunction,
TrackFunction,
} from '~/shared/contexts/AnalyticsContext';

Expand All @@ -29,9 +30,17 @@ const identify: IdentifyFunction = (userId) => {
instance?.identify(userId);
};

const registerSuperProperties: RegisterSuperPropertiesFunction = (eventProps) => {
instance?.registerSuperProperties(eventProps);
};

export function MobileAnalyticsProvider({ children }: PropsWithChildren) {
return (
<AnalyticsProvider track={track} identify={identify}>
<AnalyticsProvider
track={track}
identify={identify}
registerSuperProperties={registerSuperProperties}
>
{children}
</AnalyticsProvider>
);
Expand Down
18 changes: 17 additions & 1 deletion apps/web/src/contexts/analytics/WebAnalyticsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { memo, ReactNode } from 'react';

import AnalyticsProvider, {
IdentifyFunction,
RegisterSuperPropertiesFunction,
TrackFunction,
} from '~/shared/contexts/AnalyticsContext';

Expand Down Expand Up @@ -41,11 +42,26 @@ export const _identify: IdentifyFunction = (userId) => {
}
};

export const _registerSuperProperties: RegisterSuperPropertiesFunction = (eventProps) => {
if (!mixpanelEnabled) return;

try {
mixpanel.register(eventProps);
} catch (error: unknown) {
// mixpanel errors shouldn't disrupt app
captureException(error);
}
};

type Props = { children: ReactNode };

const WebAnalyticsProvider = memo(({ children }: Props) => {
return (
<AnalyticsProvider track={_track} identify={_identify}>
<AnalyticsProvider
track={_track}
identify={_identify}
registerSuperProperties={_registerSuperProperties}
>
{children}
</AnalyticsProvider>
);
Expand Down
31 changes: 24 additions & 7 deletions packages/shared/src/contexts/AnalyticsContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { createContext, memo, ReactNode, useCallback, useContext, useEffect } from 'react';
import {
createContext,
memo,
ReactNode,
useCallback,
useContext,
useEffect,
useState,
} from 'react';
import { useRelayEnvironment } from 'react-relay';
import { fetchQuery, graphql } from 'relay-runtime';

Expand Down Expand Up @@ -39,6 +47,7 @@ const AnalyticsContextQueryNode = graphql`
... on Viewer {
user {
dbid
roles
}
}
}
Expand All @@ -47,15 +56,18 @@ const AnalyticsContextQueryNode = graphql`

export type TrackFunction = (eventName: string, eventProps: EventProps) => void;
export type IdentifyFunction = (userId: string) => void;
export type RegisterSuperPropertiesFunction = (eventProps: EventProps) => void;

type Props = {
children: ReactNode;
track: TrackFunction;
identify: IdentifyFunction;
registerSuperProperties: RegisterSuperPropertiesFunction;
};

const AnalyticsProvider = memo(({ children, identify, track }: Props) => {
const AnalyticsProvider = memo(({ children, identify, track, registerSuperProperties }: Props) => {
const relayEnvironment = useRelayEnvironment();
const [isBetaTester, setIsBetaTester] = useState<boolean>(false);

useEffect(() => {
fetchQuery<AnalyticsContextQuery>(
Expand All @@ -66,22 +78,27 @@ const AnalyticsProvider = memo(({ children, identify, track }: Props) => {
)
.toPromise()
.then((query) => {
const userId = query?.viewer?.user?.dbid;
const user = query?.viewer?.user;
const userId = user?.dbid;

// don't identify unauthenticated users
if (!userId) {
return;
}

identify(userId);

if (user.roles?.includes('BETA_TESTER')) {
setIsBetaTester(true);
registerSuperProperties({ isBetaTester: true });
}
});
}, [identify, relayEnvironment]);
}, [identify, registerSuperProperties, relayEnvironment]);

const handleTrack: HookTrackFunction = useCallback(
(eventName, eventProps = {}) => {
track(eventName, eventProps);
track(eventName, { ...eventProps, isBetaTester });
},
[track]
[isBetaTester, track]
);

return <AnalyticsContext.Provider value={handleTrack}>{children}</AnalyticsContext.Provider>;
Expand Down

1 comment on commit 7baf636

@vercel
Copy link

@vercel vercel bot commented on 7baf636 Sep 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.