Set up HumanBehavior SDK with React applications
Show How to get your API Key
Example screenshot – your actual API key will be different.
npm install humanbehavior-js # or yarn add humanbehavior-js
.env
# For Vite projects VITE_HUMANBEHAVIOR_API_KEY=your-api-key-here # For Create React App REACT_APP_HUMANBEHAVIOR_API_KEY=your-api-key-here
import { HumanBehaviorProvider } from 'humanbehavior-js/react'; function App() { return ( <HumanBehaviorProvider apiKey={import.meta.env.VITE_HUMANBEHAVIOR_API_KEY}> {/* For Vite */} {/* OR */} <HumanBehaviorProvider apiKey={process.env.REACT_APP_HUMANBEHAVIOR_API_KEY}> {/* For Create React App */} <div> <h1>My React App</h1> {/* Your app content */} </div> </HumanBehaviorProvider> ); } export default App;
import { useHumanBehavior } from 'humanbehavior-js/react'; function MyComponent() { const tracker = useHumanBehavior(); const handleClick = () => { tracker.customEvent('button_clicked', { componentName: 'MyComponent', buttonId: 'submit-btn' }); }; return ( <button onClick={handleClick}> Submit </button> ); }
import React from 'react'; import { HumanBehaviorTracker } from 'humanbehavior-js'; function App() { React.useEffect(() => { // Initialize the tracker once globally const tracker = HumanBehaviorTracker.init(process.env.REACT_APP_HUMANBEHAVIOR_API_KEY); }, []); return ( <div> <h1>My React App</h1> {/* Your app content */} </div> ); } export default App;