Skip to content

Commit

Permalink
feat: add fid to analytics events
Browse files Browse the repository at this point in the history
  • Loading branch information
nounspaceryan committed Jul 2, 2024
1 parent 1157550 commit 7633b7f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/common/data/stores/app/accounts/farcasterStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const farcasterStore = (
);
if (!isUndefined(data.value)) {
get().account.addFidToCurrentIdentity(data.value!.fid);
analytics.track(AnalyticsEvent.LINK_FID);
analytics.track(AnalyticsEvent.LINK_FID, { fid });
}
},
});
14 changes: 14 additions & 0 deletions src/common/lib/hooks/useCurrentFid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useAppStore } from "@/common/data/stores/app";
import { NounspaceDeveloperManagedSignerData } from "@/authenticators/farcaster/signers/NounspaceManagedSignerAuthenticator";

export const useCurrentFid = (): number | null => {
const data: NounspaceDeveloperManagedSignerData | undefined = useAppStore(
(state) => {
return state.account.authenticatorConfig["farcaster:nounspace"]?.data;
},
);
const fid = data?.accountFid;
return !fid || fid === 1 ? null : fid;
};

export default useCurrentFid;
16 changes: 9 additions & 7 deletions src/common/providers/AnalyticsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { ReactNode, useEffect } from "react";
import { useRouter } from "next/router";
import { AnalyticsBrowser } from "@segment/analytics-next";
import { useCurrentSpaceIdentityPublicKey } from "@/common/lib/hooks/useCurrentSpaceIdentityPublicKey";
import { useCurrentFid } from "@/common/lib/hooks/useCurrentFid";

export enum AnalyticsEvent {
SIGN_UP = "Sign Up",
Expand All @@ -16,7 +17,7 @@ export enum AnalyticsEvent {

type AnalyticsEventProperties = {
[AnalyticsEvent.SIGN_UP]: Record<string, never>;
[AnalyticsEvent.LINK_FID]: Record<string, never>;
[AnalyticsEvent.LINK_FID]: { fid: number };
[AnalyticsEvent.SAVE_SPACE_THEME]: Record<string, never>;
[AnalyticsEvent.SAVE_HOMEBASE_THEME]: Record<string, never>;
[AnalyticsEvent.ADD_FIDGET]: { fidgetType: string };
Expand All @@ -33,16 +34,17 @@ export const analytics = {
) => {
segment.track(eventName, properties);
},
identify: (id: string) => {
segment.identify(id);
identify: (id: string, properties: any) => {
segment.identify(id, properties);
},
};

export const AnalyticsProvider: React.FC<{ children: ReactNode }> = ({
children,
}) => {
const router = useRouter();
const userId = useCurrentSpaceIdentityPublicKey();
const fid = useCurrentFid();
const identityPublicKey = useCurrentSpaceIdentityPublicKey();
const writeKey = process.env.NEXT_PUBLIC_SEGMENT_WRITE_KEY;

useEffect(() => {
Expand All @@ -54,10 +56,10 @@ export const AnalyticsProvider: React.FC<{ children: ReactNode }> = ({
}, [writeKey]);

useEffect(() => {
if (userId) {
analytics.identify(userId);
if (identityPublicKey) {
analytics.identify(identityPublicKey, { fid });
}
}, [userId]);
}, [identityPublicKey, fid]);

useEffect(() => {
segment.page();
Expand Down

0 comments on commit 7633b7f

Please sign in to comment.