trackcel
v0.4.0
Published
Lightweight analytics for React and Next.js — pageviews, engaged time, Web Vitals, events, signups, revenue and AI crawler tracking
Maintainers
Readme
Trackcel - Analytics for React and Next.js
Trackcel is a lightweight analytics SDK for React and Next.js. It tracks pageviews, custom events, clicks, scroll depth, and session duration.
Installation
npm install trackcelGet your Project ID and API Key from the Trackcel dashboard.
React (Vite, Create React App, etc.)
Use the Analytics component from the main trackcel package. Add it once at the root of your app.
// src/main.tsx or src/App.tsx
import { Analytics } from "trackcel";
function App() {
return (
<>
<Analytics
projectId="your-project-id"
apiKey="sk_live_your_api_key"
disableInDevelopment={true}
endpoint="https://trackcel.vercel.app"
debug={false}
/>
{/* rest of your app */}
</>
);
}Or use the hook directly:
import { useAnalytics } from "trackcel";
function App() {
useAnalytics({
projectId: "your-project-id",
apiKey: "sk_live_your_api_key",
disableInDevelopment: true,
});
return <YourRoutes />;
}React Router
Analytics tracks the initial page load and browser back/forward. For client-side route changes, call trackPageView when the route changes:
import { useEffect } from "react";
import { useLocation } from "react-router-dom";
import { init } from "trackcel";
const analytics = init({
projectId: "your-project-id",
apiKey: "sk_live_your_api_key",
disableInDevelopment: true,
});
analytics.attachInteractionListeners();
function RouteTracker() {
const location = useLocation();
useEffect(() => {
analytics.trackPageView();
}, [location.pathname, location.search]);
return null;
}
// In your router setup:
function App() {
return (
<BrowserRouter>
<RouteTracker />
<Routes>{/* ... */}</Routes>
</BrowserRouter>
);
}Next.js App Router
Use NextAnalytics from trackcel/next. It follows App Router navigations via usePathname and useSearchParams.
// app/providers/analytics.tsx
"use client";
import { Suspense } from "react";
import { NextAnalytics } from "trackcel/next";
export function AnalyticsProvider() {
return (
<Suspense fallback={null}>
<NextAnalytics
projectId={process.env.NEXT_PUBLIC_TRACKCEL_PROJECT_ID!}
apiKey={process.env.NEXT_PUBLIC_TRACKCEL_API_KEY!}
disableInDevelopment={true}
endpoint={process.env.NEXT_PUBLIC_TRACKCEL_ENDPOINT}
/>
</Suspense>
);
}// app/layout.tsx
import { AnalyticsProvider } from "./providers/analytics";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
{children}
<AnalyticsProvider />
</body>
</html>
);
}Next.js Pages Router
Use the main Analytics component in _app.tsx:
// pages/_app.tsx
import type { AppProps } from "next/app";
import { Analytics } from "trackcel";
export default function App({ Component, pageProps }: AppProps) {
return (
<>
<Analytics
projectId={process.env.NEXT_PUBLIC_TRACKCEL_PROJECT_ID!}
apiKey={process.env.NEXT_PUBLIC_TRACKCEL_API_KEY!}
disableInDevelopment={true}
endpoint={process.env.NEXT_PUBLIC_TRACKCEL_ENDPOINT}
/>
<Component {...pageProps} />
</>
);
}Custom events
Works in both React and Next.js:
import { init } from "trackcel";
const analytics = init({
projectId: "your-project-id",
apiKey: "sk_live_your_api_key",
});
// Track a button click, signup, purchase, etc.
analytics.trackEvent("signup_click", { plan: "pro" });Or use the shared tracker directly once <Analytics /> / <NextAnalytics /> is mounted:
import { track } from "trackcel";
track("signup_click", { plan: "pro" });Signups and visitor profiles
Link a browser to one of your users so the dashboard can show their journey, mark them as converted, and attribute revenue to the source that brought them in:
import { identify, signup } from "trackcel";
identify(user.id, { email: user.email, name: user.name }); // after login
signup(user.id, { email: user.email }); // after a confirmed signupServer-side: revenue, confirmed signups, AI crawlers
trackcel/server runs on your backend (Node.js or Edge) with the project secret key
(tc_secret_..., from the project's integrations settings). Never ship it to the browser.
import { createTrackcelServer } from "trackcel/server";
const trackcel = createTrackcelServer({ secretKey: process.env.TRACKCEL_SECRET_KEY! });
// Confirmed signup (visitorId comes from getVisitorId() in the browser, e.g. a hidden form field)
await trackcel.identify({ customerId: user.id, visitorId, email: user.email, signedUp: true });
// Payment succeeded / refunded (amounts in major units; transactionId makes retries safe)
await trackcel.revenue({ amount: 19, currency: "USD", transactionId: payment.id, customerId: user.id });
await trackcel.revenue({ type: "refund", amount: 19, currency: "USD", transactionId: refund.id, customerId: user.id });Crawlers (GPTBot, ClaudeBot, PerplexityBot, Googlebot, ...) don't run JavaScript, so log them from middleware to fill the AI activity → Crawlers report:
// middleware.ts
import { NextResponse, type NextFetchEvent, type NextRequest } from "next/server";
import { createTrackcelServer } from "trackcel/server";
const trackcel = createTrackcelServer({ secretKey: process.env.TRACKCEL_SECRET_KEY! });
export function middleware(request: NextRequest, event: NextFetchEvent) {
event.waitUntil(trackcel.crawlers(request));
return NextResponse.next();
}Package exports
| Import | Use case |
|--------|----------|
| trackcel | React, Vite, CRA, Next.js Pages Router (Analytics, track, identify, signup, getVisitorId) |
| trackcel/next | Next.js App Router only (NextAnalytics, plus the same helpers) |
| trackcel/server | Backend only: revenue, confirmed signups, crawler logging |
Configuration
| Option | Type | Description |
|--------|------|-------------|
| projectId | string | Required. From your Trackcel dashboard |
| apiKey | string | Required. From your Trackcel dashboard |
| disableInDevelopment | boolean | Skip tracking on localhost / 127.0.0.1 |
| endpoint | string | Custom API URL (default: https://trackcel.vercel.app) |
| debug | boolean | Log events to the browser console |
| webVitals | boolean | Collect Core Web Vitals (LCP, INP, CLS, FCP, TTFB). Default true |
| trackInteractions | boolean | Record clicks and scroll depth. Default true |
| respectDoNotTrack | boolean | Skip visitors whose browser sends Do Not Track. Default false |
To exclude your own visits, run localStorage.setItem("trackcel_ignore", "true") in the browser console on your site.
Environment variables (Next.js)
| Variable | Description |
|----------|-------------|
| NEXT_PUBLIC_TRACKCEL_PROJECT_ID | Your project ID |
| NEXT_PUBLIC_TRACKCEL_API_KEY | Your API key |
| NEXT_PUBLIC_TRACKCEL_ENDPOINT | Optional custom collect endpoint |
For React / Vite, pass credentials as props (or read from import.meta.env.VITE_* yourself and pass them to Analytics).
Features
- Pageview tracking, SPA navigation (history API) and App Router support
- Visitor IDs and sessions that end after 30 minutes of inactivity
- Engaged time (visible + active, idle time excluded) and time on page
- Core Web Vitals field data (LCP, INP, CLS, FCP, TTFB) for the performance score
- Custom events,
identify()andsignup()for visitor profiles and conversions - Server-side revenue, confirmed signups and AI/search crawler logging (
trackcel/server) - Click heatmap data and scroll depth milestones (25%, 50%, 75%, 100%)
- No CORS preflights (beacons sent as
text/plain), works withoutcrypto.randomUUID - Configurable endpoint for self-hosting, development-mode disable, opt-out switch
Need help?
Visit trackcel.vercel.app or open an issue on GitHub.
