Files
claudemesh/packages/analytics/mobile/src/providers/posthog/index.tsx
Alejandro Gutiérrez d3163a5bff feat(db): mesh data model — meshes, members, invites, audit log
- pgSchema "mesh" with 4 tables isolating the peer mesh domain
- Enums: visibility, transport, tier, role
- audit_log is metadata-only (E2E encryption enforced at broker/client)
- Cascade on mesh delete, soft-delete via archivedAt/revokedAt

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 21:19:32 +01:00

74 lines
1.5 KiB
TypeScript

import PostHog, { PostHogProvider } from "posthog-react-native";
import { useEffect } from "react";
import { useTrackingPermissions } from "../../hooks";
import { env } from "./env";
import type { AnalyticsProviderClientStrategy } from "@turbostarter/analytics";
let client: PostHog | null = null;
const getClient = () => {
if (client) {
return client;
}
client = new PostHog(env.EXPO_PUBLIC_POSTHOG_KEY, {
host: env.EXPO_PUBLIC_POSTHOG_HOST,
defaultOptIn: false,
});
return client;
};
const Wrapper = ({ children }: { children: React.ReactNode }) => {
const client = getClient();
return (
<PostHogProvider client={client} autocapture>
{children}
</PostHogProvider>
);
};
const Setup = () => {
const client = getClient();
const granted = useTrackingPermissions();
useEffect(() => {
if (granted) {
void client.optIn();
} else {
void client.optOut();
}
}, [granted, client]);
return null;
};
const ProviderComponent = ({ children }: { children: React.ReactNode }) => {
return (
<Wrapper>
<Setup />
{children}
</Wrapper>
);
};
export const { Provider, track, identify, reset } = {
Provider: ProviderComponent,
track: (name, params) => {
const client = getClient();
client.capture(name, params);
},
identify: (userId, traits) => {
const client = getClient();
client.identify(userId, traits);
},
reset: () => {
const client = getClient();
client.reset();
},
} satisfies AnalyticsProviderClientStrategy;