The ca-sdk is a lightweight frontend analytics library designed to collect user session, page views, and event interaction data in real time via Socket.IO. Built for CravingsInc platforms, this SDK enables deep insights into user behavior across web applications.
- Frontend SDK: TypeScript + React Context + Custom Hooks
- Backend: Node.js + Express + TypeScript
- Real-time: Socket.IO
- Database: PostgreSQL
Tracks the lifetime of a user's visit to the app.
- Auto-generates a session ID using
uuid - Detects:
- Device Type: Mobile, Tablet, Desktop
- Operating System: Windows, macOS, Android, iOS, etc
- Browser: Chrome, Safari, Firefox, Edge, etc
- Captures activity like:
- Clicks, Scrolls, Keyboard input
- Idle or expired session due to timeout/unmount
- Calculates:
- Time until login
- Last active timestamp
- Save intervals ( Every 3 seconds )
{
id: string;
user: { id: string | null; wasLoggedIn: boolean; timeUntilLoggedIn: number };
createdAt: Date;
lastActiveAt: Date;
lastSaveAt: Date;
deviceInfo: { type: string; os: string; browser: string };
location?: {
country: string; // 'US' | 'CA' | etc
city: string; // 'New York' | 'Toronto' | etc
region: string; // 'NY' | 'ON' | etc
coordinates?: {
lat: number; // latitude
lng: number; // longitude
}
}
expired: { expired: boolean; reason?: 'unmount' | 'timeout' | 'manual' | null };
}Tracks each time a user navigates to a page route
- URL visited and referring page
- Time started and ended
- Total Time spent
- Scroll depth % reached
- All interactions during that view
{
id: string; // unique page session id
sessionId: string; // session id
url: string; // page url
referrer: {
utm_source: string | null;
utm_medium: string | null;
utm_campaign?: string | null;
utm_term?: string | null;
utm_content?: string | null;
raw_url?: string;
};
timeStarted: Date; // timestamp when the page was loaded
timeEnded: Date; // timestamp when the page was unloaded
timeSpent: number; // time spent on the page in milliseconds
scrollDepth: number; // scroll depth in percentage
interactionCount: number; // number of interactions on the page
interactions: EventsInteraction[]; // array of interactions
}Captures specific frontend events:
- Supported Types:
click,scroll,input,hover, etc. - Captures timestamp, target element, and optional event data.
{
id: string; // unique event id
sessionId: string; // session id
type: string; // event type (e.g. 'click', 'scroll', 'input', etc)
target: string; // target element (e.g. 'button#submit', 'input#email', etc)
time: Date; // timestamp when the event occurred
data?: any; // additional data (e.g. { value: 'test' })
}import { CASDK } from '@CravingsInc/ca-sdk';
<BrowserRouter>
<CASDK>
<App />
</CASDK>
</BrowserRouter>SessionProvider: Manages session lifecycle, expiration, and refreshPageTrackerProvider: Tracks route changes and view lifecycle
useSession(): Access session data and methods (restartSession,refreshActivity, etc ).usePage(): Access current page view state and interactionsuseComponentEvent(): Hook into pre-component interactions
import React from 'react';
function App() {
return (
<div style={{ padding: '2rem' }}>
<h1 data-label="hello-world" data-data={`{ "type": "button", "platform": "EVENTRIX" }`}>Hello from CASDK Example</h1>
</div>
);
}
export default App;const { pageView, scrollDepth, interactionEvents, triggerEvent } = usePage();
useEffect(() => {
triggerEvent({ type: 'click', target: '#signup-button' });
}, []);Sessions are stored in localStorage under the key __cravings_ca_sdk_session. When a user closes the browser, the ssion is marked as expired with a reason. If the session expires, it can be restarted on the next visit.
- Anonymized by default: If no user ID is passed, the SDK tracks anonymous session.
- Extendable: You can customize what events are captured using the
triggerEvent()method.
- SDK initializes in browser
SessionManagercreates or restores sessionPageTrackerstarts a new view on route change- Events and views are sent to server via Socket.IO
- Server persists into PostgreSQL
- Heatmap & scroll visualization
- Offline buffering of events
- Backend dashboard for live session tracking
- More Customization:
- RefreshSession Handlers
- SessionServerHandler
- PageServerHandler
To run the SDK locally:
pnpm install
pnpm devTo build:
pnpm build@CravingsInc/ca-sdk-server(WIP): Server-side event ingestion and persistance
We welcome improvement and integrations! Please submit PRs or open issues for bugs/requests.
Chidozie Nnaji & CravingsInc Team
Build't for internal use, open for external developers
MIT