@juanedev/analytics-react
v0.1.5
Published
React SDK for `@juanedev/analytics-core`. It initializes sessions, tracks pageviews automatically on route changes, and exposes a simple `useAnalytics()` hook for custom events.
Readme
@juane/analytics-react
React SDK for @juanedev/analytics-core. It initializes sessions, tracks pageviews automatically on route changes, and exposes a simple useAnalytics() hook for custom events.
- ESM + CJS output
- Built on
@juanedev/analytics-core - Fixed endpoints: AP∫
https://track.juane.dev, WebSocketwss://track.juane.dev
Installation
npm install @juane/analytics-react
# or
yarn add @juane/analytics-react
# or
pnpm add @juane/analytics-reactPeer deps:
- react ^18
- react-dom ^18
Quick start
import { AnalyticsProvider, useAnalytics } from '@juanedev/analytics-react';
function App() {
return (
<AnalyticsProvider websiteId="mi-web-123">
<MyRoutes />
</AnalyticsProvider>
);
}
function Button() {
const { trackEvent } = useAnalytics();
return (
<button onClick={() => trackEvent('button_click', { color: 'red' })}>
Click me
</button>
);
}API
Props:
websiteId: string(required)
Behavior:
- Creates or retrieves
userIdand createssessionIdusing@juanedev/analytics-core. - Sends an initial
pageviewon mount. - Listens to browser history changes (pushState/replaceState/popstate) and sends
pageviewautomatically. - Opens WebSocket to
wss://track.juane.devwith heartbeats and emitssession:endon unmount/unload.
useAnalytics()
Returns:
{ trackEvent(eventName: string, properties?: Record<string, any>): Promise<any> }- Sends an event to
https://track.juane.dev/track/eventusing@juanedev/analytics-corepayload helpers.
- Sends an event to
Notes
- For SPAs,
pageviewis auto-sent on navigation. If you use a router that doesn’t trigger history events, manually calltrackEvent/trackPageviewvia the core. - Runs only on the client; do not call inside SSR-only code paths.
Build
npm run buildoutputsdist/index.esm.js,dist/index.cjs.js, anddist/index.d.ts.
