Used to track screen views & events of React Native & Expo Apps using Splitbee.
Install @splitbee/react-native
along side the expo related dependencies:
expo add @splitbee/react-native expo-device expo-constants @react-native-async-storage/async-storage
They are required for getting the device data as well as persisting the identifier on the device.
Install following dependencies
Make sure to link all dependencies after instillation.
yarn add react-native-device-info @react-native-async-storage/async-storagenpx pod-install
Please follow the official documentation of those libraries on how to link them correctly.
To initialize the Splitbee SDK run the init
function with your token. This should happen only once at the top of your app.
import splitbee from '@splitbee/react-native';splitbee.init('YOUR_TOKEN'); // Token from your project settings in the Splitbee dashboard
After this step you all functionality is available to you.
import splitbee from '@splitbee/react-native';// This initiliazes Splitbee.jssplitbee.init()// Track a views (See automatic tracking)splitbee.screen("Home")// Track a custom eventsplitbee.track("Sign Up")// Track an event with custom datasplitbee.track("Choose Plan", {plan: "Business"})// Attach data to the current usersplitbee.user.set({})
You might be using react-navigation or react-native-navigation for your app. You can use the Splitbee SDK to track page views automatically. See the examples below.
In this example we are using react-navigation to track screen views automatically.
import splitbee, { useTrackReactNavigation } from '@splitbee/react-native';export default function Navigation() {const [{ onReady, onStateChange }, navigationRef] = useTrackReactNavigation();return (<NavigationContainerref={navigationRef}onReady={() => {onReady();}}onStateChange={() => {onStateChange();}}><RootNavigator /></NavigationContainer>);}
If you already have a ref
set for the NavigationContainer
, you can pass it to useTrackReactNavigation
.
const navigationRef = useRef(null)const [{ onReady, onStateChange }] = useTrackReactNavigation(navigationRef);
To setup automated screen tracking with react-native-navigation, add following code to your mainfile (index.js)
.
Navigation.events().registerComponentDidAppearListener(async ({ componentName, componentType }) => {if (componentType === 'Component') {await splitbee.screen(componentName);}});