Step 1: Installation
Step 2: Environment Variables
Create or add to a.env.production
or .env.development
file in your project root based on your staging type:
Security Best Practice: The
GATSBY_
prefix is required for Gatsby to expose the environment variable to the client-side code.Step 3: Basic Setup
Add the Tracker to Your App
Create or update yourgatsby-browser.js
file in the root of your project:
That’s it! The SDK now automatically tracks user interactions, navigation, and console events. Everything past here is optional.
Optional: Advanced Features
Track Custom Events
You can track custom events from any React component:User Identification
Identify users after authentication:Data Redaction
Configure data redaction for sensitive information in yourgatsby-browser.js
:
Gatsby-Specific Considerations
Client-Side Only Execution
The HumanBehavior SDK runs on the client side using Gatsby’sonClientEntry
API, which ensures it only loads after the browser environment is ready.
Environment Variables
Gatsby requires environment variables to be prefixed withGATSBY_
to be available in client-side code.
Performance
The HumanBehavior SDK is lightweight and won’t impact your Gatsby site’s performance. It only loads the tracking code when needed.SSR Compatibility
The SDK is fully compatible with Gatsby’s SSR (Server-Side Rendering) and SSG (Static Site Generation) modes.Build Process
The SDK works seamlessly with Gatsby’s build process and doesn’t interfere with static generation or deployment.Alternative: Using gatsby-ssr.js
If you prefer to initialize the tracker in the SSR phase, you can also usegatsby-ssr.js
:
Troubleshooting
Common Issues
“API key not found”- Ensure your environment variable is prefixed with
GATSBY_
- Check that the
.env
file is in your project root - Restart your development server after adding environment variables
- Verify the API key is correct
- Check browser console for error messages
- Ensure
gatsby-browser.js
is in your project root
- Check your network tab for API calls to HumanBehavior
- Verify the API key has the correct permissions
- Check browser console for any error messages
- Ensure the SDK is only imported in client-side files
- Check that environment variables are properly configured
- Verify Gatsby version compatibility
Next Steps
Once you’ve completed the setup:- Test the integration by visiting your site and checking the browser console
- Verify events are being sent to your HumanBehavior dashboard
- Customize tracking by adding custom events where needed
- Monitor performance to ensure the SDK isn’t impacting your site
- Deploy your Gatsby site with the tracking enabled