Skip to content

Instantly share code, notes, and snippets.

@benjackwhite
Last active May 24, 2022 09:41
Show Gist options
  • Save benjackwhite/41652b911283b5f973b3be52d3023e6e to your computer and use it in GitHub Desktop.
Save benjackwhite/41652b911283b5f973b3be52d3023e6e to your computer and use it in GitHub Desktop.
posthog-react-native-super-properties
import { Linking } from "react-native"
import PostHog from 'posthog-react-native'
import AsyncStorage from '@react-native-async-storage/async-storage';
import { v4 as uuidv4 } from 'uuid';
const getSessionId = async () => {
// You could get something remotely from an API or just store a unique ID
let sessionId = await AsyncStorage.getItem('my-session-id')
if (!sessionId) {
sessionId = uuidv4()
await AsyncStorage.setItem('my-session-id', sessionId)
}
return sessionId
}
let sessionId = getSessionId();
await PostHog.setup('my-site-id', {})
// Your wrapped functions to include the sessionId in every event
export const capture = async (event: string, properties = {}) => {
return await PostHog.capture(event, {
...properties,
session_id: sessionId,
});
}
export const screen = async (name: string, properties = {}) => {
return await PostHog.screen(name, {
...properties,
session_id: sessionId,
});
};
export const openLink = (url: string) => {
// Naieve appending of query param
url = url.includes("?") ? `${url}&session_id=${sessionId}`: `${url}?session_id=${sessionId}`
Linking.openURL(url);
// Now on your website you can check for the `session_id` param and add it to your tracking the same way as here.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment