Introduction
This document describes how to integrate the cux.io SDK with a mobile application built using React Native. The goal of this integration is to collect analytics data on user behavior, track events, identify users, and automatically monitor in-app navigation.
This guide assumes you have an existing React Native project and are using the react-navigation
library to handle navigation.
SDK Installation
To get started, add the CUX package to your project using either the npm
or yarn
package manager.
# Using npm npm install not-published-package-yet --save # Using yarn yarn add not-published-package-yet
(Note: The package name not-published-package-yet
is used for the purpose of this guide).
SDK Initialization
After installing the package, you must initialize CUX in your application's main entry point (usually App.js
). The initialization should only run once when the app starts. The best way to achieve this is by placing it inside a useEffect
hook with an empty dependency array.
File: App.js
import React, { useEffect } from 'react'; import { CUX } from 'cux-react-native'; import AppNavigator from './navigation/AppNavigator'; // Import your main navigation component const App = () => { useEffect(() => { // Initialize CUX with your unique API Key and options CUX.initialize({ token: 'YOUR_UNIQUE_API_KEY', enableCrashReporting: true, // Optional: enable crash reporting }); }, []); return <AppNavigator />; }; export default App;
Automatic Screen Tracking with react-navigation
To avoid manually tracking every screen, our SDK provides a custom hook, useCUXNavigation, that automatically handles screen tracking when used with react-navigation.
Import the useCUXNavigation
hook from the cux-react-native
package and apply it to your NavigationContainer
.
File: src/navigation/AppNavigator.js
import React from 'react'; import { NavigationContainer } from '@react-navigation/native'; import { createStackNavigator } from '@react-navigation/stack'; // 1. Import the useCUXNavigation hook from the CUX package import { useCUXNavigation } from 'cux-react-native'; // Import your screen components import HomeScreen from '../screens/HomeScreen'; import DetailsScreen from '../screens/DetailsScreen'; import ProfileScreen from '../screens/ProfileScreen'; const Stack = createStackNavigator(); const AppNavigator = () => { // 2. Call the hook to get the required props for the container const cuxNavProps = useCUXNavigation(); return ( // 3. Spread the props onto NavigationContainer <NavigationContainer {...cuxNavProps}> <Stack.Navigator initialRouteName="Home"> <Stack.Screen name="Home" component={HomeScreen} /> <Stack.Screen name="Details" component={DetailsScreen} /> <Stack.Screen name="Profile" component={ProfileScreen} /> </Stack.Navigator> </NavigationContainer> ); }; export default AppNavigator;
With this implementation, CUX will automatically record every screen a user visits, without requiring any additional code in your individual screen components. The hook returns a ref
, onReady
, and onStateChange
handler, which are all passed to the NavigationContainer
.
Autocapture of Tap Events
To simplify the collection of interaction data, the CUX SDK provides a way to automatically capture tap events on specific elements. This feature automatically records the element's identifier and the coordinates of the tap.
nstead of a hook, the SDK offers a wrapper component, <CUX.Touchable>
. This component functions just like a standard Pressable
component but adds automatic event tracking.
When a user taps a <CUX.Touchable>
component, the SDK will automatically fire an event with the following payload:
Event Name:
cux_tap
Coordinates: The
x
andy
position of the tap relative to the screen.Element ID: The
testID
ornativeID
of the tapped element.
The component is designed to be a drop-in replacement for TouchableOpacity
or Pressable
and will not interfere with your existing onPress
handlers.
To enable autocapture, simply replace your existing touchable component with <CUX.Touchable>
and ensure you have assigned a testID
.
import { Text, StyleSheet } from 'react-native'; // 1. Import the CUX.Touchable component from the SDK import { CUX } from 'cux-react-native'; const MyButton = ({ onPress }) => ( // 2. Replace TouchableOpacity with CUX.Touchable <CUX.Touchable style={styles.button} onPress={onPress} // Your original onPress handler still works perfectly testID="checkout-button" // The ID is now automatically captured > <Text style={styles.text}>Proceed to Checkout</Text> </CUX.Touchable> ); const styles = StyleSheet.create({ button: { padding: 10, backgroundColor: '#007bff' }, text: { color: 'white' } });
This approach provides powerful, low-effort event collection for the most common user interactions in your app.
Data Masking and Privacy
To ensure user privacy and compliance with data protection regulations (like GDPR), the CUX SDK provides powerful data masking features. Screenshots taken by the SDK can have sensitive information automatically or manually obscured before they are sent to our servers.
Automatic Masking of Input Fields
All text input fields are considered sensitive by default. To enable automatic masking for them, the SDK provides a privacy-enhanced component that should be used in place of the standard React Native TextInput
.
This component behaves identically to the standard TextInput
but ensures its content is always masked in any screenshots.
Simply replace the TextInput
import from react-native
with the <CUX.TextInput>
component.
import { SafeAreaView, StyleSheet } from 'react-native'; // 1. Import the CUX object import { CUX } from 'cux-react-native'; const LoginScreen = () => { return ( <SafeAreaView> {/* 2. Use CUX.TextInput instead of the standard TextInput */} <CUX.TextInput style={styles.input} placeholder="Enter your email" keyboardType="email-address" /> <CUX.TextInput style={styles.input} placeholder="Enter your password" secureTextEntry /> </SafeAreaView> ); }; const styles = StyleSheet.create({ input: { height: 40, borderColor: 'gray', borderWidth: 1, margin: 10, padding: 10 } });
Masking Custom Elements
You may have other sensitive information in your app that is not an input field, such as user balances, names, or profile pictures. The SDK provides a <CUX.Mask>
wrapper component to easily hide any element from screenshots.
You can wrap any component or group of components with <CUX.Mask>
. During a screenshot, the <CUX.Mask>
component will render a solid placeholder block that has the exact same dimensions as the content it wraps, effectively hiding it. Your application's UI remains unchanged for the user.
Imagine you have a View
that displays a user's account balance. To mask it, simply wrap the entire view in <CUX.Mask>
.
import { View, Text, StyleSheet } from 'react-native'; import { CUX } from 'cux-react-native'; const AccountBalance = ({ balance }) => { return ( // Wrap the sensitive area with CUX.Mask <CUX.Mask> <View style={styles.balanceContainer}> <Text style={styles.label}>Your Balance</Text> <Text style={styles.balanceText}>${balance}</Text> </View> </CUX.Mask> ); }; const styles = StyleSheet.create({ balanceContainer: { padding: 20, backgroundColor: '#f0f0f0', alignItems: 'center' }, label: { fontSize: 16, color: '#666' }, balanceText: { fontSize: 24, fontWeight: 'bold', color: 'black' } });
In any screenshot captured by CUX, the entire balanceContainer
view will be replaced by a solid gray rectangle, ensuring the user's financial data is never exposed.
By combining these two methods, you gain full control over data privacy in your session recordings.
Identifying Users (required for cross-platform)
To associate sessions and events with a specific user, use the CUX.identifyUser
method. This is best called right after a user logs in.
userId
: A unique identifier for the user in your system.properties
: An optional object with additional user attributes.
import { CUX } from 'cux-react-native'; const handleLogin = (user) => { // Your login logic... // After a successful login, identify the user in CUX CUX.identifyUser(user.id, { email: user.email, name: user.fullName, plan: 'premium', // Example custom attribute }); };
Tracking Custom Events
To track custom user interactions (e.g., button clicks, adding an item to a cart), use the CUX.trackEvent
method.
import { CUX } from 'cux-react-native'; const ProductDetails = ({ product }) => { const handleAddToCart = () => { // Your add-to-cart logic... // Send an event to CUX with custom properties CUX.trackEvent('addToCart', { productId: product.id, productName: product.name, category: product.category, price: product.price, }); }; return ( <Button title="Add to Cart" onPress={handleAddToCart} /> ); };
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article