Basic Engagement Example
A simple example showing how to create and trigger a basic engagement.
Overview
This example demonstrates:
- Creating a simple engagement
- Triggering it with an event
- Displaying a welcome nudge
Engagement Configuration
{
"ctaId": "welcome-nudge",
"rule": {
"priority": 1,
"frequency": {
"session": {"limit": 1},
"lifespan": {"limit": 1}
},
"stateTransition": {
"APP_LAUNCH": {
"0": [{"transitionTo": "1"}]
}
},
"stateToAction": {
"1": "show-welcome"
},
"actions": [
{
"actionId": "show-welcome",
"type": "NUDGE_UI",
"template": {
"type": "Bottomsheet",
"props": {
"title": "Welcome!",
"message": "Thanks for downloading our app",
"primaryButton": {
"text": "Get Started",
"action": {
"type": "DEEPLINK",
"url": "app://home"
}
},
"secondaryButton": {
"text": "Maybe Later",
"action": {
"type": "DISMISS"
}
}
}
}
}
],
"resetStates": ["1"],
"resetCTAonFirstLaunch": true
}
}
App Integration
import React, {useEffect, useRef} from 'react'
import {
NavigationContainer,
NavigationContainerRef,
} from '@react-navigation/native'
import {createNativeStackNavigator} from '@react-navigation/native-stack'
import {SafeAreaProvider} from 'react-native-safe-area-context'
import {GestureHandlerRootView} from 'react-native-gesture-handler'
import {Platform} from 'react-native'
import {
Nudge,
RAVEN_ROUTE_NAME,
setNavigationRef,
ravenClient,
useNavigationTracker,
fetchCTA,
trackAppEvent,
type RavenClientOptions,
} from '@dreamhorizonorg/raven-client'
const Stack = createNativeStackNavigator()
function HomeScreen() {
return (
<View>
<Text>Welcome to the app!</Text>
</View>
)
}
export default function App() {
const navigationRef = useRef<NavigationContainerRef>(null)
useNavigationTracker(navigationRef)
useEffect(() => {
// Initialize SDK
ravenClient.init({
listeners: {
appEvent: (eventName, props) => {
console.log('Analytics:', eventName, props)
},
getAccessToken: () => ({
token: 'your-token',
tokenType: 'Bearer',
}),
},
config: {
baseUrl: 'https://api.example.com',
userId: 'user-123',
appVersion: '1.0.0',
platform: Platform.OS,
packageName: 'com.example.app',
},
} as RavenClientOptions)
// Fetch CTAs
fetchCTA().catch(console.error)
// Process app launch event
trackAppEvent({
eventName: 'APP_LAUNCH',
})
}, [])
return (
<GestureHandlerRootView style={{flex: 1}}>
<SafeAreaProvider>
<NavigationContainer
ref={(ref) => {
navigationRef.current = ref
setNavigationRef(ref)
}}>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen
name={RAVEN_ROUTE_NAME}
component={Nudge}
options={{
headerShown: false,
presentation: 'transparentModal',
animation: 'fade',
}}
/>
</Stack.Navigator>
</NavigationContainer>
</SafeAreaProvider>
</GestureHandlerRootView>
)
}
Flow
- App launches
- SDK initializes
- CTAs are fetched
APP_LAUNCHevent is processed- State machine transitions from "0" to "1"
- Welcome nudge is displayed
- User interacts with nudge
- State machine resets
Next Steps
- Multi-Step Nudge - More complex example
- State Machine DSL Examples - More configuration examples