Initialization
This guide covers how to initialize Raven Client with proper configuration.
Basic Initialization
Initialize the SDK in your root component's useEffect:
import {useEffect} from 'react'
import {
ravenClient,
fetchCTA,
type RavenClientOptions,
} from '@dreamhorizonorg/raven-client'
import {Platform} from 'react-native'
function App() {
useEffect(() => {
ravenClient.init({
listeners: {
appEvent: (eventName, props) => {
// Handle analytics events
console.log('Analytics event:', eventName, props)
},
getAccessToken: () => ({
token: 'your-access-token',
tokenType: 'Bearer',
}),
},
config: {
baseUrl: 'https://your-api-base-url.com',
userId: 'user-123',
appVersion: '1.0.0',
platform: Platform.OS,
packageName: 'com.yourcompany.yourapp',
tenantId: 'your-tenant-id', // Optional: Multi-tenant identifier
},
} as RavenClientOptions)
// Fetch CTAs after initialization
fetchCTA().catch(console.error)
}, [])
// ... rest of your app
}
Configuration Options
RavenClientConfig
interface RavenClientConfig {
baseUrl: string // API base URL
userId: string | number // Current user ID
appVersion: string // App version
codepushVersion?: string // CodePush version (optional)
platform: string // 'ios' or 'android'
packageName: string // App package/bundle ID
tenantId?: string // Tenant ID for multi-tenant apps (optional)
}
RavenClientListeners
interface RavenClientListeners {
appEvent: (eventName: string, props?: unknown) => void
getAccessToken: () => AccessToken
}
Listener Implementations
appEvent Listener
Called when the SDK needs to send analytics events. Integrate with your analytics service:
listeners: {
appEvent: (eventName, props) => {
// Send to your analytics service
yourAnalyticsService.track(eventName, props);
// Or use Firebase Analytics
analytics().logEvent(eventName, props);
// Or use Mixpanel
mixpanel.track(eventName, props);
},
}
getAccessToken Listener
Returns the current authentication token for API requests:
listeners: {
getAccessToken: () => {
// Get token from your auth system
const token = await getAuthToken();
return {
token: token,
tokenType: 'Bearer',
};
},
}
Fetching CTAs
After initialization, fetch CTAs from your backend:
useEffect(() => {
ravenClient.init({
/* ... */
})
// Fetch CTAs
fetchCTA()
.then(() => {
console.log('CTAs fetched successfully')
})
.catch((error) => {
console.error('Failed to fetch CTAs:', error)
})
}, [])
Loading CTAs from Storage
If you want to load CTAs from local storage on app start:
import {getCtaFromStorageToMemory} from '@dreamhorizonorg/raven-client'
useEffect(() => {
// Load CTAs from storage first
getCtaFromStorageToMemory()
// Then initialize and fetch fresh CTAs
ravenClient.init({
/* ... */
})
fetchCTA()
}, [])
Dynamic Configuration
You can update configuration at runtime:
// Update user ID when user logs in
ravenClient.config.userId = newUserId
// Fetch CTAs again with new user context
fetchCTA()
Error Handling
Handle initialization errors gracefully:
useEffect(() => {
try {
ravenClient.init({
// ... configuration
})
fetchCTA().catch((error) => {
console.error('Failed to fetch CTAs:', error)
// Fallback: Load from storage
getCtaFromStorageToMemory()
})
} catch (error) {
console.error('Failed to initialize SDK:', error)
}
}, [])
Best Practices
- Initialize Early: Initialize the SDK as early as possible in your app lifecycle
- Handle Errors: Always handle errors when fetching CTAs
- Update User Context: Update
userIdwhen users log in/out - Token Refresh: Ensure
getAccessTokenreturns valid tokens - Network Handling: Handle network failures gracefully
Next Steps
- Event Processing - Set up event handling
- State Machine DSL - Learn about state machines