feat(analytics): add posthog-react-native integration and session management
- Integrated posthog-react-native for enhanced analytics tracking. - Added client and session ID management functionality. - Updated Info.plist and Expo.plist for configuration changes. - Updated package dependencies to include posthog-react-native version 4.1.3. Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -41,6 +41,7 @@
|
||||
<key>CFBundleURLSchemes</key>
|
||||
<array>
|
||||
<string>follow</string>
|
||||
<string>folo</string>
|
||||
<string>is.follow</string>
|
||||
</array>
|
||||
</dict>
|
||||
@@ -52,7 +53,7 @@
|
||||
</dict>
|
||||
</array>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>110</string>
|
||||
<string>1</string>
|
||||
<key>ITSAppUsesNonExemptEncryption</key>
|
||||
<false/>
|
||||
<key>LSApplicationCategoryType</key>
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
<key>EXUpdatesLaunchWaitMs</key>
|
||||
<integer>0</integer>
|
||||
<key>EXUpdatesRuntimeVersion</key>
|
||||
<string>0.1.7</string>
|
||||
<string>0.0.0-dev</string>
|
||||
<key>EXUpdatesURL</key>
|
||||
<string>https://u.expo.dev/a6335b14-fb84-45aa-ba80-6f6ab8926920</string>
|
||||
</dict>
|
||||
|
||||
@@ -94,6 +94,7 @@
|
||||
"nanoid": "5.1.5",
|
||||
"nativewind": "4.1.23",
|
||||
"ofetch": "1.4.1",
|
||||
"posthog-react-native": "4.1.3",
|
||||
"qrcode": "1.5.4",
|
||||
"react": "19.0.0",
|
||||
"react-dom": "19.0.0",
|
||||
|
||||
@@ -1,27 +1,36 @@
|
||||
import { whoami } from "@follow/store/user/getters"
|
||||
import { setFirebaseTracker, setOpenPanelTracker, tracker } from "@follow/tracker"
|
||||
import { getAnalytics } from "@react-native-firebase/analytics"
|
||||
import { setFirebaseTracker, setPostHogTracker, tracker } from "@follow/tracker"
|
||||
import { nativeApplicationVersion, nativeBuildVersion } from "expo-application"
|
||||
import PostHog from "posthog-react-native"
|
||||
|
||||
import { getUserAgent } from "../lib/native/user-agent"
|
||||
import { op } from "../lib/op"
|
||||
import { ga4 } from "../lib/ga4"
|
||||
import { proxyEnv } from "../lib/proxy-env"
|
||||
|
||||
export const initAnalytics = async () => {
|
||||
setOpenPanelTracker(op)
|
||||
setFirebaseTracker(getAnalytics())
|
||||
setFirebaseTracker(ga4)
|
||||
|
||||
const user = whoami()
|
||||
if (user) {
|
||||
tracker.identify(user)
|
||||
}
|
||||
|
||||
op.setGlobalProperties({
|
||||
tracker.manager.appendUserProperties({
|
||||
build: "rn",
|
||||
version: nativeApplicationVersion,
|
||||
buildId: nativeBuildVersion,
|
||||
})
|
||||
|
||||
op.setHeaders({
|
||||
"User-Agent": await getUserAgent(),
|
||||
})
|
||||
if (proxyEnv.POSTHOG_KEY) {
|
||||
setPostHogTracker(new PostHog(proxyEnv.POSTHOG_KEY))
|
||||
}
|
||||
|
||||
// op.setGlobalProperties({
|
||||
// build: "rn",
|
||||
// version: nativeApplicationVersion,
|
||||
// buildId: nativeBuildVersion,
|
||||
// })
|
||||
|
||||
// op.setHeaders({
|
||||
// "User-Agent": await getUserAgent(),
|
||||
// })
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import { getStorageNS } from "@follow/utils/ns"
|
||||
|
||||
import { kv } from "./kv"
|
||||
|
||||
const CLIENT_ID_KEY = getStorageNS("client_id")
|
||||
const SESSION_ID_KEY = getStorageNS("session_id")
|
||||
|
||||
const uuid = () => {
|
||||
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replaceAll(/[xy]/g, (c) => {
|
||||
const r = (Math.random() * 16) | 0,
|
||||
v = c === "x" ? r : (r & 0x3) | 0x8
|
||||
return v.toString(16)
|
||||
})
|
||||
}
|
||||
// In-memory cache for session data
|
||||
const sessionCache: Record<string, string> = {}
|
||||
|
||||
export const getClientId = (): string => {
|
||||
const clientId = kv.getSync(CLIENT_ID_KEY)
|
||||
if (!clientId) {
|
||||
const newClientId = uuid()
|
||||
kv.setSync(CLIENT_ID_KEY, newClientId)
|
||||
return newClientId
|
||||
}
|
||||
return clientId
|
||||
}
|
||||
|
||||
export const getSessionId = (): string => {
|
||||
// First check in-memory cache
|
||||
if (sessionCache[SESSION_ID_KEY]) {
|
||||
return sessionCache[SESSION_ID_KEY]
|
||||
}
|
||||
|
||||
// Check persistent storage
|
||||
const sessionId = kv.getSync(SESSION_ID_KEY)
|
||||
if (!sessionId) {
|
||||
const newSessionId = uuid()
|
||||
// Store in both cache and persistent storage
|
||||
sessionCache[SESSION_ID_KEY] = newSessionId
|
||||
kv.setSync(SESSION_ID_KEY, newSessionId)
|
||||
return newSessionId
|
||||
}
|
||||
|
||||
// Store in cache for future access
|
||||
sessionCache[SESSION_ID_KEY] = sessionId
|
||||
return sessionId
|
||||
}
|
||||
|
||||
export const clearSessionId = (): void => {
|
||||
// Clear from both cache and persistent storage
|
||||
delete sessionCache[SESSION_ID_KEY]
|
||||
kv.delete(SESSION_ID_KEY)
|
||||
}
|
||||
|
||||
export const clearClientId = (): void => {
|
||||
kv.delete(CLIENT_ID_KEY)
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import { apiClient } from "./api-fetch"
|
||||
import { getClientId, getSessionId } from "./client-session"
|
||||
|
||||
class Analytics4 {
|
||||
private clientID: string
|
||||
private sessionID: string
|
||||
private userID: string | null = null
|
||||
private userProperties: Record<string, { value: unknown }> | null = null
|
||||
|
||||
constructor(clientID: string = getClientId(), sessionID = getSessionId()) {
|
||||
this.clientID = clientID
|
||||
this.sessionID = sessionID
|
||||
}
|
||||
|
||||
async setUserId(id: string) {
|
||||
this.userID = id
|
||||
}
|
||||
|
||||
async setUserProperties(upValue?: Record<string, unknown>) {
|
||||
const userProperties = Object.entries(upValue || {}).reduce(
|
||||
(acc, [key, value]) => {
|
||||
acc[key as string] = {
|
||||
value,
|
||||
}
|
||||
return acc
|
||||
},
|
||||
{} as Record<string, { value: unknown }>,
|
||||
)
|
||||
this.userProperties = userProperties
|
||||
}
|
||||
|
||||
async logEvent(eventName: string, params?: Record<string, unknown>): Promise<any> {
|
||||
delete params?.__code
|
||||
delete params?.__eventName
|
||||
|
||||
const payload = {
|
||||
client_id: this.clientID,
|
||||
user_id: this.userID,
|
||||
events: [
|
||||
{
|
||||
name: eventName,
|
||||
params: {
|
||||
session_id: this.sessionID,
|
||||
engagement_time_msec: 1000,
|
||||
...params,
|
||||
},
|
||||
},
|
||||
],
|
||||
user_properties: this.userProperties,
|
||||
}
|
||||
|
||||
return apiClient.data.g.$post({
|
||||
json: payload,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const ga4 = new Analytics4()
|
||||
@@ -46,5 +46,7 @@ export const env = {
|
||||
API_URL: envProfileMap[profile].API_URL,
|
||||
APP_CHECK_DEBUG_TOKEN: process.env.EXPO_PUBLIC_APP_CHECK_DEBUG_TOKEN,
|
||||
OPENPANEL_CLIENT_ID: envProfileMap[profile].OPENPANEL_CLIENT_ID,
|
||||
POSTHOG_KEY: envProfileMap[profile].POSTHOG_KEY,
|
||||
POSTHOG_HOST: envProfileMap[profile].POSTHOG_HOST,
|
||||
OPENPANEL_API_URL: envProfileMap[profile].OPENPANEL_API_URL,
|
||||
}
|
||||
|
||||
@@ -3,8 +3,9 @@
|
||||
"version": "0.0.1",
|
||||
"private": true,
|
||||
"main": "./src/index.ts",
|
||||
"dependencies": {
|
||||
"posthog-js": "1.255.0"
|
||||
"peerDependencies": {
|
||||
"posthog-js": "^1.255.0",
|
||||
"posthog-react-native": "^4.1.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@follow/configs": "workspace:*",
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
import type { PostHog } from "posthog-js"
|
||||
import type PostHogReactNative from "posthog-react-native"
|
||||
|
||||
import type { IdentifyPayload, TrackerAdapter, TrackPayload } from "./base"
|
||||
|
||||
export interface PostHogAdapterConfig {
|
||||
instance: PostHog
|
||||
instance: PostHog | PostHogReactNative
|
||||
enabled?: boolean
|
||||
}
|
||||
|
||||
export class PostHogAdapter implements TrackerAdapter {
|
||||
private posthogInstance: PostHog
|
||||
private posthogInstance: PostHog | PostHogReactNative
|
||||
private enabled: boolean
|
||||
|
||||
constructor(config: PostHogAdapterConfig) {
|
||||
@@ -24,7 +25,7 @@ export class PostHogAdapter implements TrackerAdapter {
|
||||
if (!this.isEnabled()) return
|
||||
|
||||
try {
|
||||
this.posthogInstance.capture(eventName, properties)
|
||||
this.posthogInstance.capture(eventName, properties as any)
|
||||
} catch (error) {
|
||||
console.error(`[PostHog] Failed to track event "${eventName}":`, error)
|
||||
}
|
||||
@@ -35,10 +36,10 @@ export class PostHogAdapter implements TrackerAdapter {
|
||||
|
||||
try {
|
||||
this.posthogInstance.identify(payload.id, {
|
||||
email: payload.email,
|
||||
name: payload.name,
|
||||
avatar: payload.image,
|
||||
handle: payload.handle,
|
||||
email: payload.email!,
|
||||
name: payload.name!,
|
||||
avatar: payload.image!,
|
||||
handle: payload.handle!,
|
||||
})
|
||||
} catch (error) {
|
||||
console.error("[PostHog] Failed to identify user:", error)
|
||||
@@ -49,7 +50,11 @@ export class PostHogAdapter implements TrackerAdapter {
|
||||
if (!this.isEnabled()) return
|
||||
|
||||
try {
|
||||
this.posthogInstance.setPersonProperties(properties)
|
||||
if ("setPersonProperties" in this.posthogInstance) {
|
||||
this.posthogInstance.setPersonProperties(properties)
|
||||
} else {
|
||||
;(this.posthogInstance as PostHogReactNative).setPersonPropertiesForFlags(properties as any)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("[PostHog] Failed to set user properties:", error)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { FirebaseAnalyticsTypes } from "@react-native-firebase/analytics"
|
||||
import type { PostHog } from "posthog-js"
|
||||
import type PostHogReactNative from "posthog-react-native"
|
||||
|
||||
import { FirebaseAdapter, OpenPanelAdapter, PostHogAdapter } from "./adapters"
|
||||
import { ProxyAdapter } from "./adapters/proxy"
|
||||
@@ -50,7 +51,7 @@ class TrackManager extends TrackerManager {
|
||||
this.addAdapter(adapter)
|
||||
}
|
||||
|
||||
setPostHogTracker(posthog: PostHog) {
|
||||
setPostHogTracker(posthog: PostHog | PostHogReactNative) {
|
||||
const adapter = new PostHogAdapter({ instance: posthog })
|
||||
this.addAdapter(adapter)
|
||||
}
|
||||
|
||||
Generated
+56
-1
@@ -971,6 +971,9 @@ importers:
|
||||
ofetch:
|
||||
specifier: 1.4.1
|
||||
version: 1.4.1
|
||||
posthog-react-native:
|
||||
specifier: 4.1.3
|
||||
version: 4.1.3(2121d6ac0841ff6678f1ed39c5b47959)
|
||||
qrcode:
|
||||
specifier: 1.5.4
|
||||
version: 1.5.4
|
||||
@@ -1726,8 +1729,11 @@ importers:
|
||||
packages/internal/tracker:
|
||||
dependencies:
|
||||
posthog-js:
|
||||
specifier: 1.255.0
|
||||
specifier: ^1.255.0
|
||||
version: 1.255.0
|
||||
posthog-react-native:
|
||||
specifier: ^4.1.3
|
||||
version: 4.1.3(2121d6ac0841ff6678f1ed39c5b47959)
|
||||
devDependencies:
|
||||
'@follow/configs':
|
||||
specifier: workspace:*
|
||||
@@ -12671,6 +12677,45 @@ packages:
|
||||
rrweb-snapshot:
|
||||
optional: true
|
||||
|
||||
posthog-react-native@4.1.3:
|
||||
resolution: {integrity: sha512-att/UNiTpxmo2lLaLREYd3wWPPDtrhDjNtgLIRVCM1JBfxU9+OupiNhDBtjwLjKxygnyYWEX3TWdOughRsRdeQ==}
|
||||
peerDependencies:
|
||||
'@react-native-async-storage/async-storage': '>=1.0.0'
|
||||
'@react-navigation/native': '>= 5.0.0'
|
||||
expo-application: '>= 4.0.0'
|
||||
expo-device: '>= 4.0.0'
|
||||
expo-file-system: '>= 13.0.0'
|
||||
expo-localization: '>= 11.0.0'
|
||||
posthog-react-native-session-replay: '>= 1.1.1'
|
||||
react-native-device-info: '>= 10.0.0'
|
||||
react-native-localize: '>= 3.0.0'
|
||||
react-native-navigation: '>= 6.0.0'
|
||||
react-native-safe-area-context: '>= 4.0.0'
|
||||
react-native-svg: '>= 15.0.0'
|
||||
peerDependenciesMeta:
|
||||
'@react-native-async-storage/async-storage':
|
||||
optional: true
|
||||
'@react-navigation/native':
|
||||
optional: true
|
||||
expo-application:
|
||||
optional: true
|
||||
expo-device:
|
||||
optional: true
|
||||
expo-file-system:
|
||||
optional: true
|
||||
expo-localization:
|
||||
optional: true
|
||||
posthog-react-native-session-replay:
|
||||
optional: true
|
||||
react-native-device-info:
|
||||
optional: true
|
||||
react-native-localize:
|
||||
optional: true
|
||||
react-native-navigation:
|
||||
optional: true
|
||||
react-native-safe-area-context:
|
||||
optional: true
|
||||
|
||||
postject@1.0.0-alpha.6:
|
||||
resolution: {integrity: sha512-b9Eb8h2eVqNE8edvKdwqkrY6O7kAwmI8kcnBv1NScolYJbo59XUF0noFq+lxbC1yN20bmC0WBEbDC5H/7ASb0A==}
|
||||
engines: {node: '>=14.0.0'}
|
||||
@@ -30152,6 +30197,16 @@ snapshots:
|
||||
preact: 10.26.9
|
||||
web-vitals: 4.2.4
|
||||
|
||||
posthog-react-native@4.1.3(2121d6ac0841ff6678f1ed39c5b47959):
|
||||
dependencies:
|
||||
react-native-svg: 15.12.0(react-native@0.79.1(@babel/core@7.27.4)(@types/react@19.1.6)(bufferutil@4.0.9)(react@19.0.0))(react@19.0.0)
|
||||
optionalDependencies:
|
||||
expo-application: 6.1.4(expo@53.0.12(@babel/core@7.27.4)(@expo/metro-runtime@5.0.4(react-native@0.79.1(@babel/core@7.27.4)(@types/react@19.1.6)(bufferutil@4.0.9)(react@19.0.0)))(bufferutil@4.0.9)(graphql@16.8.1)(react-native-webview@13.15.0(react-native@0.79.1(@babel/core@7.27.4)(@types/react@19.1.6)(bufferutil@4.0.9)(react@19.0.0))(react@19.0.0))(react-native@0.79.1(@babel/core@7.27.4)(@types/react@19.1.6)(bufferutil@4.0.9)(react@19.0.0))(react@19.0.0))
|
||||
expo-device: 7.1.4(expo@53.0.12(@babel/core@7.27.4)(@expo/metro-runtime@5.0.4(react-native@0.79.1(@babel/core@7.27.4)(@types/react@19.1.6)(bufferutil@4.0.9)(react@19.0.0)))(bufferutil@4.0.9)(graphql@16.8.1)(react-native-webview@13.15.0(react-native@0.79.1(@babel/core@7.27.4)(@types/react@19.1.6)(bufferutil@4.0.9)(react@19.0.0))(react@19.0.0))(react-native@0.79.1(@babel/core@7.27.4)(@types/react@19.1.6)(bufferutil@4.0.9)(react@19.0.0))(react@19.0.0))
|
||||
expo-file-system: 18.1.10(expo@53.0.12(@babel/core@7.27.4)(@expo/metro-runtime@5.0.4(react-native@0.79.1(@babel/core@7.27.4)(@types/react@19.1.6)(bufferutil@4.0.9)(react@19.0.0)))(bufferutil@4.0.9)(graphql@16.8.1)(react-native-webview@13.15.0(react-native@0.79.1(@babel/core@7.27.4)(@types/react@19.1.6)(bufferutil@4.0.9)(react@19.0.0))(react@19.0.0))(react-native@0.79.1(@babel/core@7.27.4)(@types/react@19.1.6)(bufferutil@4.0.9)(react@19.0.0))(react@19.0.0))(react-native@0.79.1(@babel/core@7.27.4)(@types/react@19.1.6)(bufferutil@4.0.9)(react@19.0.0))
|
||||
expo-localization: 16.1.5(expo@53.0.12(@babel/core@7.27.4)(@expo/metro-runtime@5.0.4(react-native@0.79.1(@babel/core@7.27.4)(@types/react@19.1.6)(bufferutil@4.0.9)(react@19.0.0)))(bufferutil@4.0.9)(graphql@16.8.1)(react-native-webview@13.15.0(react-native@0.79.1(@babel/core@7.27.4)(@types/react@19.1.6)(bufferutil@4.0.9)(react@19.0.0))(react@19.0.0))(react-native@0.79.1(@babel/core@7.27.4)(@types/react@19.1.6)(bufferutil@4.0.9)(react@19.0.0))(react@19.0.0))(react@19.0.0)
|
||||
react-native-safe-area-context: 5.4.1(react-native@0.79.1(@babel/core@7.27.4)(@types/react@19.1.6)(bufferutil@4.0.9)(react@19.0.0))(react@19.0.0)
|
||||
|
||||
postject@1.0.0-alpha.6:
|
||||
dependencies:
|
||||
commander: 9.5.0
|
||||
|
||||
Reference in New Issue
Block a user