-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathposthog.ts
More file actions
59 lines (49 loc) · 2.38 KB
/
Copy pathposthog.ts
File metadata and controls
59 lines (49 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import PostHog from 'posthog-react-native'
import Config from 'react-native-config'
// Environment variables are embedded at build time via react-native-config
// Ensure .env file exists with POSTHOG_PROJECT_TOKEN and POSTHOG_HOST
const projectToken = Config.POSTHOG_PROJECT_TOKEN
const host = Config.POSTHOG_HOST || 'https://us.i.posthog.com'
const isPostHogConfigured = projectToken && projectToken !== 'phc_your_project_token_here'
if (!isPostHogConfigured) {
console.warn(
'PostHog project token not configured. Analytics will be disabled. ' +
'Set POSTHOG_PROJECT_TOKEN in your .env file to enable analytics.'
)
}
/**
* PostHog client instance for bare React Native
*
* Configuration loaded from .env via react-native-config (embedded at build time).
* Required peer dependencies: @react-native-async-storage/async-storage,
* react-native-device-info, react-native-localize
*
* @see https://posthog.com/docs/libraries/react-native
*/
export const posthog = new PostHog(projectToken || 'placeholder_key', {
// PostHog API host (usually 'https://us.i.posthog.com' or 'https://eu.i.posthog.com')
host,
// Disable PostHog if project token is not configured
disabled: !isPostHogConfigured,
// Capture app lifecycle events:
// - Application Installed, Application Updated
// - Application Opened, Application Became Active, Application Backgrounded
captureAppLifecycleEvents: true,
// Enable debug mode in development for verbose logging
debug: __DEV__,
// Batching: queue events and flush periodically to optimize battery usage
flushAt: 20, // Number of events to queue before sending
flushInterval: 10000, // Interval in ms between periodic flushes
maxBatchSize: 100, // Maximum events per batch
maxQueueSize: 1000, // Maximum queued events (oldest dropped when full)
// Feature flags
preloadFeatureFlags: true, // Load flags on initialization
sendFeatureFlagEvent: true, // Track getFeatureFlag calls for experiments
featureFlagsRequestTimeoutMs: 10000, // Timeout for flag requests (prevents blocking)
// Network settings
requestTimeout: 10000, // General request timeout in ms
fetchRetryCount: 3, // Number of retry attempts for failed requests
fetchRetryDelay: 3000, // Delay between retries in ms
})
// Export helper to check if PostHog is enabled
export const isPostHogEnabled = isPostHogConfigured