⚠️ This package is on the path to deprecation. Please use @microsoft/react-native-clarity instead for new projects or future updates.
react-native-clarity
A React Native plugin that allows integrating Clarity with your application.
Notes
- React Native versions earlier than
0.64
are not supported. - Platform Support: Android and iOS.
- iOS support was experimental before version
4.0.1
and required enabling the enableIOS_experimental
feature flag. - The Clarity package depends on native code to run. Therefore, you've to ship a new build after integrating the package.
- Expo Go does not support plugins requiring native code to execute. Hence, Clarity SDK doesn't operate if you're running through Expo GO. To run the SDK locally, use the EAS Build system.
Installation
Step 1: Add package dependency
To add the package, run the following command.
npm install react-native-clarity
Step 2: Initialize Clarity
Initialize Clarity by adding the following.
import * as Clarity from 'react-native-clarity';
Clarity.initialize('<project-id>', {
});
Step 3: Testing
After integrating Clarity with your application, ensure these steps for testing on a device or a simulator:
- Your device or simulator should be connected to the internet.
- The OS version on your device or simulator should be within the supported range specified at Platform/Framework Support Matrix.
- For initial setup, consider setting the log level to Verbose. This provides detailed logs that can help identify any initialization issues.
Allow approximately 2 hours for complete sessions to appear in your Clarity project on the Clarity website. However, in-progress sessions can still be viewed in real time. See Clarity Recordings in Real Time.
Known issues
- For
react-native-svg
Users: A known issue in react-native-svg
version 13.x affects user interaction and playback when used with Clarity. Upgrade to react-native-svg
version 14 or later for proper behavior and compatibility.
Usage
import * as Clarity from 'react-native-clarity';
const clarityConfig = {
logLevel: Clarity.LogLevel.Verbose
};
Clarity.initialize('<project-id>', clarityConfig);
Clarity.pause();
Clarity.resume();
Clarity.isPaused().then((paused) => {...});
Clarity.startNewSession(sessionId => {
Clarity.setCustomUserId("<CustomUserId>");
Clarity.setCustomSessionId("<CustomSessionId>");
Clarity.setCustomTag("key", "value");
});
Clarity.sendCustomEvent('Initialized');
Clarity.setOnSessionStartedCallback(sessionId => { });
Clarity.setCurrentScreenName('Home');
Clarity.getCurrentSessionId().then((id) => {...});
Clarity.getCurrentSessionUrl().then((url) => {...});
Using Clarity With React Navigation
import * as Clarity from 'react-native-clarity';
import { NavigationContainer useNavigationContainerRef } from '@react-navigation/native';
const HomeScreen = ({...}) => {
const navigationRef = useNavigationContainerRef();
const routeNameRef = React.useRef();
return (
<NavigationContainer
ref={navigationRef}
onReady={() => {
routeNameRef.current = navigationRef.getCurrentRoute().name;
const clarityConfig = {
logLevel: Clarity.LogLevel.Verbose
};
Clarity.initialize('<project-id>', clarityConfig);
Clarity.setCurrentScreenName(routeNameRef.current);
}}
onStateChange={() => {
const previousRouteName = routeNameRef.current;
const currentRouteName = navigationRef.getCurrentRoute().name;
if (previousRouteName !== currentRouteName) {
routeNameRef.current = currentRouteName;
Clarity.setCurrentScreenName(currentRouteName);
}
}}
>
{/* ... */}
</NavigationContainer>
);
};
Initialization arguments
function initialize(projectId: string, config?: ClarityConfig)
export interface ClarityConfig {
userId?: string;
logLevel?: LogLevel;
}
License
MIT