feat: whyrating - initial project from turbostarter boilerplate
This commit is contained in:
47
packages/analytics/mobile/src/providers/mixpanel/index.tsx
Normal file
47
packages/analytics/mobile/src/providers/mixpanel/index.tsx
Normal file
@@ -0,0 +1,47 @@
|
||||
import { Mixpanel } from "mixpanel-react-native";
|
||||
import { useEffect } from "react";
|
||||
|
||||
import { useTrackingPermissions } from "../../hooks";
|
||||
|
||||
import { env } from "./env";
|
||||
|
||||
import type { AnalyticsProviderClientStrategy } from "@turbostarter/analytics";
|
||||
|
||||
const optOutTracking = true;
|
||||
const trackAutomaticEvents = false;
|
||||
const mixpanel = new Mixpanel(
|
||||
env.EXPO_PUBLIC_MIXPANEL_TOKEN,
|
||||
trackAutomaticEvents,
|
||||
optOutTracking,
|
||||
);
|
||||
|
||||
void mixpanel.init();
|
||||
|
||||
export const { Provider, track, identify, reset } = {
|
||||
Provider: ({ children }) => {
|
||||
const granted = useTrackingPermissions();
|
||||
|
||||
useEffect(() => {
|
||||
void (async () => {
|
||||
const optedOut = await mixpanel.hasOptedOutTracking();
|
||||
if (granted && optedOut) {
|
||||
void mixpanel.optInTracking();
|
||||
}
|
||||
})();
|
||||
}, [granted]);
|
||||
|
||||
return <>{children}</>;
|
||||
},
|
||||
track: (name, params) => {
|
||||
mixpanel.track(name, params);
|
||||
},
|
||||
identify: (userId, traits) => {
|
||||
void mixpanel.identify(userId);
|
||||
if (traits) {
|
||||
void mixpanel.getPeople().set(traits);
|
||||
}
|
||||
},
|
||||
reset: () => {
|
||||
mixpanel.reset();
|
||||
},
|
||||
} satisfies AnalyticsProviderClientStrategy;
|
||||
Reference in New Issue
Block a user