React Native
- React Native
- Expo
Below are the various processes involved in setting up Spotcheck in React Native.
Below are the various processes involved in setting up Spotcheck in React Native Expo.
Installation
- React Native
- Expo
To install the SurveySparrowSdk for React Native, make sure you are in the root folder of your React Native project in the terminal and run the following command.
# NPM Installation
npm install surveysparrow-react-native-sdk
npm install react-native-webview # 13.13.2 or newer (13.x)
npm install react-native-safe-area-context # 5.6.2 or newer (5.x)
npm install react-native-svg # 15.15.1 or newer (15.x), for SpotChecks buttons
npm install react-native-device-info # 14.0.4 or newer (14.x)
# YARN Installation
yarn add surveysparrow-react-native-sdk
yarn add react-native-webview # 13.13.2 or newer (13.x)
yarn add react-native-safe-area-context # 5.6.2 or newer (5.x)
yarn add react-native-svg # 15.15.1 or newer (15.x), for SpotChecks buttons
yarn add react-native-device-info # 14.0.4 or newer (14.x)
Then run cd ios && pod install. The SDK follows your app's React Native minimum iOS version and Android minSdkVersion (24 or higher recommended).
Optional: install @sentry/react-native if you want SDK errors reported to your Sentry project.
To install the SurveySparrowSdk for Expo, make sure you are in the root folder of your Expo project in the terminal and run the following command.
# Expo (recommended, picks versions matching your SDK)
npx expo install surveysparrow-expo-spotchecks react-native-webview react-native-safe-area-context react-native-svg axios
# NPM Installation
npm install surveysparrow-expo-spotchecks
npm install react-native-webview # 13.0.0 or newer
npm install react-native-safe-area-context # 4.0.0 or newer
npm install react-native-svg # 15.0.0 or newer, for SpotChecks buttons
npm install axios # 0.21.0 or newer
# YARN Installation
yarn add surveysparrow-expo-spotchecks
yarn add react-native-webview # 13.0.0 or newer
yarn add react-native-safe-area-context # 4.0.0 or newer
yarn add react-native-svg # 15.0.0 or newer, for SpotChecks buttons
yarn add axios # 0.21.0 or newer
The package needs a development build (npx expo run:ios / npx expo run:android or EAS); it does not run in Expo Go. Android minSdkVersion 24 or higher.
Optional: react-native-device-info (8.7.0 or newer) for richer device details (tablet detection, app version and build, and the device model in SDK error reports; without it Android still reports the model), expo-blur for the backdrop blur setting on iOS, @sentry/react-native to report SDK errors to your Sentry project, expo-secure-store (13.0.0 or newer) if you already use it for the visitor id.
Permissions
- React Native
- Expo
Android — AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Optional — photo capture
<uses-permission android:name="android.permission.CAMERA" />
<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
Optional — voice transcription
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
iOS — Info.plist
Optional — photo capture
<key>NSCameraUsageDescription</key>
<string>Your description</string>
Optional — voice transcription
<key>NSMicrophoneUsageDescription</key>
<string>Your description</string>
<key>NSSpeechRecognitionUsageDescription</key>
<string>Your description</string>
Optional — photo library
<key>NSPhotoLibraryUsageDescription</key>
<string>Your description</string>
app.json / app.config.js
{
"expo": {
"ios": {
"infoPlist": {
"NSCameraUsageDescription": "Used when surveys include photo capture",
"NSMicrophoneUsageDescription": "Used when surveys include voice input",
"NSSpeechRecognitionUsageDescription": "Used when surveys include voice transcription",
"NSPhotoLibraryUsageDescription": "Used when surveys allow choosing photos"
}
},
"android": {
"permissions": [
"CAMERA",
"RECORD_AUDIO",
"MODIFY_AUDIO_SETTINGS",
"INTERNET",
"ACCESS_NETWORK_STATE"
]
}
}
}
If the camera does not open after prebuild on Android 11+, merge a manifest snippet that includes the IMAGE_CAPTURE intent <queries> block (config plugin or custom manifest fragment for your Expo SDK).
Initialization
Declare and initialize the spotcheck with the help of the initializeSpotChecks() function.
Anonymous users
If you wish not to keep track of users' data, you can follow the below syntax for initialization.
- React Native
- Expo
import Spotcheck, { initializeSpotChecks } from 'surveysparrow-react-native-sdk';
import { useLayoutEffect } from 'react';
useLayoutEffect(() => {
initializeSpotChecks({
domainName: 'your-domain-name',
targetToken: 'your-target-token',
// Should Not Pass userDetails as const
userDetails: {},
variables: {},
customProperties: {},
});
},[]);
import Spotcheck, { initializeSpotChecks } from 'surveysparrow-expo-spotchecks';
import { useLayoutEffect } from 'react';
useLayoutEffect(() => {
initializeSpotChecks({
domainName: 'your-domain-name',
targetToken: 'your-target-token',
// Should Not Pass userDetails as const
userDetails: {},
variables: {},
customProperties: {},
});
},[]);
Known Users
If you wish to keep track of users' data and perform conditional Survey triggers, you can follow the below syntax for initialization.
- React Native
- Expo
import Spotcheck, { initializeSpotChecks } from 'surveysparrow-react-native-sdk';
import { useLayoutEffect } from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
//Initialize once at the root of the App
useLayoutEffect(() => {
initializeSpotChecks({
domainName: 'your-domain-name',
targetToken: 'your-target-token',
userDetails: {
email:"<user_email>",
mobile:"<user_mobile>",
uuid: '<uuid_value>' // Optional
},
variables: {
sparrowLang: "ta" // Eg: ta, en,
<api_identifier_of_variable>:"<variable_value>",
},
customProperties: {
<custom_property_name>: '<custom_property_value>'
},
});
},[]);
import Spotcheck, { initializeSpotChecks } from 'surveysparrow-expo-spotchecks';
import { useLayoutEffect } from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
//Initialize once at the root of the App
useLayoutEffect(() => {
initializeSpotChecks({
domainName: 'your-domain-name',
targetToken: 'your-target-token',
userDetails: {
email:"<user_email>",
mobile:"<user_mobile>",
uuid: '<uuid_value>' // Optional
},
variables: {
sparrowLang: "ta" // Eg: ta, en,
<api_identifier_of_variable>:"<variable_value>",
},
customProperties: {
<custom_property_name>: '<custom_property_value>'
},
});
},[]);
Add Spotcheck inside the navigation container only once in the App component
<SafeAreaProvider> // Spotcheck component needed to be enclose within SafeAreaProvider
<NavigationContainer>
<Stack.Navigator>
<your-app-code>
</Stack.Navigator>
<Spotcheck/>
</NavigationContainer>
</SafeAreaProvider>
Screen Track
It provides the ability to keep track of screens the users are visiting and to enable the survey trigger on that screen. Also user can able to pass the dynamic variables in the screen tracking function.
Syntax:
- React Native
- Expo
import { trackScreen } from 'surveysparrow-react-native-sdk';
import { useEffect } from 'react';
useEffect(() => {
trackScreen('<your-screen-name>', {
userDetails: {
email: '<user_email>',
mobile: '<user_mobile>',
uuid: '<uuid_value>' // Optional
} // optional,
variables: {
sparrowLang: 'ta', // Language code (e.g., 'ta', 'en')
<api_identifier_of_variable>: '<variable_value>',
} // optional,
customProperties: {
<custom_property_name>: '<custom_property_value>'
} // optional
});
}, []);
import { trackScreen } from 'surveysparrow-expo-spotchecks';
import { useEffect } from 'react';
useEffect(() => {
trackScreen('<your-screen-name>', {
userDetails: {
email: '<user_email>',
mobile: '<user_mobile>',
uuid: '<uuid_value>' // Optional
} // optional,
variables: {
sparrowLang: 'ta', // Language code (e.g., 'ta', 'en')
<api_identifier_of_variable>: '<variable_value>',
} // optional,
customProperties: {
<custom_property_name>: '<custom_property_value>'
} // optional
});
}, []);
Example:
If a survey needs to be triggered on the payment page, the name of the ScreenName should be specified in the TrackScreen function. Also we can pass the dynamic variables as shown below.
- React Native
- Expo
import { trackScreen } from 'surveysparrow-react-native-sdk';
import { useEffect } from 'react';
useEffect(() => {
trackScreen("paymentScreen",
{
userDetails: {
email: 'kalaiselvan.k@surveysparrow.com',
mobile: '9449988099',
}, // optional
variables: {
sparrowLang: 'ta', // Language code (e.g., 'ta', 'en')
}, //optional
customProperties: {
customerCount: 10
}, // optional
});
}, []);
import { trackScreen } from 'surveysparrow-expo-spotchecks';
import { useEffect } from 'react';
useEffect(() => {
trackScreen("paymentScreen",
{
userDetails: {
email: 'kalaiselvan.k@surveysparrow.com',
mobile: '9449988099',
}, // optional
variables: {
sparrowLang: 'ta', // Language code (e.g., 'ta', 'en')
}, //optional
customProperties: {
customerCount: 10
}, // optional
});
}, []);
Event Track
It provides the ability to keep track of events which when triggered, will enable the survey to be popped.
Syntax:
- React Native
- Expo
import { trackEvent } from 'surveysparrow-react-native-sdk';
trackEvent('<your-screen-name>', {
"<your-events-or-functions>": {}
});
import { trackEvent } from 'surveysparrow-expo-spotchecks';
trackEvent('<your-screen-name>', {
"<your-events-or-functions>": {}
});
Example:
If a survey needs to be triggered when the user completes a payment, then the TrackEvent function should be called with the respective ScreenName and optional custom properties.
- React Native
- Expo
import { trackEvent } from 'surveysparrow-react-native-sdk';
trackEvent('paymentScreen', {
'paymentComplete': {
email:'kalaiselvan.k@surveysparrow.com',
paymentStatus:true
}
});
import { trackEvent } from 'surveysparrow-expo-spotchecks';
trackEvent('paymentScreen', {
'paymentComplete': {
email:'kalaiselvan.k@surveysparrow.com',
paymentStatus:true
}
});
Callbacks (Optional)
Callbacks are used to listen to events that are part of the SpotChecks. All callback functions are optional — you can implement only the ones you need. The available callbacks include:
-
Survey Response - triggers when the user submits a response.
-
Survey Loaded - triggers when the SpotChecks is loaded on the user's page.
-
Survey Close - triggers when the SpotChecks is closed by the user.
-
Partial Submission - triggers when the PartialSubmission is enabled and received.
Syntax:
- React Native
- Expo
import { initializeSpotChecks } from 'surveysparrow-react-native-sdk';
import { useLayoutEffect } from 'react';
// Step 1: Add callbacks to the SpotChecks Initialization
useLayoutEffect(() => {
initializeSpotChecks({
//...
listener: listener
});
}, []);
// Step 2: Listen to the SpotChecks events
const listener = {
onSurveyLoaded: async (response: Record<string, any>) => {},
onSurveyResponse: async (response: Record<string, any>) => {},
onCloseButtonTap: async () => {},
onPartialSubmission: async (response: Record<string, any>) => {}
}
import { initializeSpotChecks } from 'surveysparrow-expo-spotchecks';
import { useLayoutEffect } from 'react';
// Step 1: Add callbacks to the SpotChecks Initialization
useLayoutEffect(() => {
initializeSpotChecks({
//...
listener: listener
});
}, []);
// Step 2: Listen to the SpotChecks events
const listener = {
onSurveyLoaded: async (response: Record<string, any>) => {},
onSurveyResponse: async (response: Record<string, any>) => {},
onCloseButtonTap: async () => {},
onPartialSubmission: async (response: Record<string, any>) => {}
}