Before you begin, ensure you have the following:
The easiest way to get started is by installing our package from npm. Open your terminal in your project's root directory and run the following command:
npm install @your-company/analytics-sdk
Once installed, you can initialize the SDK in your application's entry point (e.g., `App.js` or `index.js`). It's best to do this once when your application first loads.
**Important:** Replace `"YOUR_API_KEY"` with your actual API key.
import AnalyticsSDK from '@your-company/analytics-sdk';
// Initialize the SDK with your API key
const sdk = new AnalyticsSDK({
apiKey: "YOUR_API_KEY"
});
// Make the sdk instance globally available or pass it via context
export default sdk;
With the SDK initialized, you can start tracking key user actions. The `track` method is the primary function for sending events. It accepts two arguments: the event name (a string) and an optional payload of properties (an object).
import sdk from './sdk';
import { useEffect } from 'react';
function MyPage() {
useEffect(() => {
sdk.track("Page Viewed", {
path: window.location.pathname,
title: document.title
});
}, []);
return (
// Your page content
);
}
import sdk from './sdk';
function HeroSection() {
const handleHeroButtonClick = () => {
sdk.track("Hero: Main CTA Clicked", {
button_text: "Get Started Now",
location: "Hero Section"
});
window.location.href = "/get-started";
};
return (
<button onClick={handleHeroButtonClick}>
Get Started Now
</button>
);
}
After you've integrated the SDK and deployed your changes, you should be able to see the events appear in your dashboard.