Skip to content

React SDK

import { Aside } from ‘@astrojs/starlight/components’;

The React SDK provides hooks and components for easy integration.

Terminal window
npm install @analyze.rocks/react
import { AnalyzeProvider } from '@analyze.rocks/react';
function App() {
return (
<AnalyzeProvider
apiKey="YOUR_API_KEY"
host="https://ingest.analyze.rocks"
>
<YourApp />
</AnalyzeProvider>
);
}
OptionTypeDefaultDescription
apiKeystringrequiredYour public API key
hoststringhttps://ingest.analyze.rocksIngest endpoint
autoCapturebooleantrueAuto-track pageviews and clicks
debugbooleanfalseEnable debug logging
cookiePolicystringkeepkeep, anonymize, none
ipPolicystringkeepkeep, anonymize, none
import { useAnalyze } from '@analyze.rocks/react';
function SignupButton() {
const { track, identify, set, reset } = useAnalyze();
const handleClick = () => {
track('signup_clicked', {
button_location: 'header',
plan: 'pro'
});
};
return (
<button onClick={handleClick}>
Sign Up
</button>
);
}
import { useIdentify } from '@analyze.rocks/react';
import { useEffect } from 'react';
function UserProfile({ user }) {
const identify = useIdentify();
useEffect(() => {
if (user) {
identify(user.id, {
email: user.email,
name: user.name,
plan: user.plan,
company: user.company
});
}
}, [user, identify]);
return <Profile user={user} />;
}
import { usePageView } from '@analyze.rocks/react';
function ProductPage({ product }) {
// Track pageview with custom properties
usePageView({
product_id: product.id,
product_category: product.category
});
return <ProductDetails product={product} />;
}
import { useAnalyze } from '@analyze.rocks/react';
function AddToCartButton({ product }) {
const { track } = useAnalyze();
const handleAddToCart = () => {
track('$cart_add', {
product_id: product.id,
product_name: product.name,
price: product.price,
quantity: 1,
currency: 'EUR'
});
// Add to cart logic
};
return <button onClick={handleAddToCart}>Add to Cart</button>;
}

Full TypeScript definitions are included:

import { useAnalyze, TrackProperties } from '@analyze.rocks/react';
interface SignupEventProps extends TrackProperties {
plan: 'free' | 'pro' | 'enterprise';
source: string;
}
function SignupForm() {
const { track } = useAnalyze();
const handleSubmit = (plan: string) => {
track<SignupEventProps>('signup_completed', {
plan: plan as 'free' | 'pro' | 'enterprise',
source: 'landing_page'
});
};
return <form onSubmit={() => handleSubmit('pro')}>...</form>;
}