pulse-analytics-sdk 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,52 @@
1
- # Temporary Holding Version
1
+ # pulse-analytics-sdk
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Lightweight React & Next.js behavioral telemetry component for **Pulse Analytics** — drop-in `<Analytics />` component similar to `@vercel/analytics`.
4
+
5
+ ## Installation via GitHub
6
+
7
+ ```bash
8
+ pnpm add github:hisham-pp/pulse-analytics-sdk
9
+ # or
10
+ npm install github:hisham-pp/pulse-analytics-sdk
11
+ ```
12
+
13
+ ## Usage (Next.js App Router)
14
+
15
+ ```tsx
16
+ import { Analytics } from "pulse-analytics-sdk";
17
+
18
+ export default function RootLayout({ children }: { children: React.ReactNode }) {
19
+ return (
20
+ <html lang="en">
21
+ <body>
22
+ {children}
23
+ <Analytics
24
+ appId="my-web-app"
25
+ endpoint="https://your-analytics-deployment.vercel.app/api/collect"
26
+ />
27
+ </body>
28
+ </html>
29
+ );
30
+ }
31
+ ```
32
+
33
+ ## Automatic Behavioral Telemetry (v0.2.0)
34
+
35
+ Enabled by default and configurable via boolean props (`trackPageviews`, `trackClicks`, `trackScrollDepth`, `trackEngagement`, `trackFriction`, `trackOutbound`, `trackForms`, `trackErrors`, `trackWebVitals`):
36
+
37
+ - **SPA Route & Query Accuracy**: Defers `document.title` capture across `history.pushState` / `replaceState` / `popstate` and stores `query` + parsed `queryParams` separately from `path`.
38
+ - **Active Engagement & Scroll Depth**: Tracks 25% / 50% / 75% / 100% scroll milestones and distinguishes active engaged time (visible tab + recent interaction) from idle time.
39
+ - **UX Friction Signals**: Detects **rage clicks** (3+ rapid clicks within 1s in the same spot) and **dead clicks** (clicks on non-interactive elements causing no navigation or DOM mutation).
40
+ - **Outbound Links, Downloads & Forms**: Automatically captures external links, file downloads, form submissions, and form abandonments (field names only, never input values).
41
+ - **Client Errors & Core Web Vitals**: Captures uncaught `error` / `unhandledrejection` events and `LCP`, `INP`, `CLS`, `TTFB` per route.
42
+ - **Batched Reliable Transport**: Uses `fetchLater()` with `AbortController` batching and `keepalive` / `sendBeacon` fallback.
43
+
44
+ ## Custom Events, User Identification & Flushing
45
+
46
+ ```ts
47
+ import { flushAnalytics, identifyUser, trackEvent } from "pulse-analytics-sdk";
48
+
49
+ identifyUser("user_123", { email: "user@example.com", plan: "pro" });
50
+ trackEvent("action_completed", { count: 42 });
51
+ flushAnalytics();
52
+ ```
@@ -0,0 +1,48 @@
1
+ type AnalyticsEventType = "pageview" | "click" | "custom" | "leave" | "scroll" | "rage_click" | "dead_click" | "outbound" | "download" | "form_submit" | "form_abandon" | "error" | "web_vital";
2
+ interface AnalyticsProps {
3
+ /** Registered app slug from your Pulse Analytics dashboard */
4
+ appId: string;
5
+ /** Full URL to your Pulse Analytics /api/collect endpoint */
6
+ endpoint: string;
7
+ /** Optional authenticated user ID from your host app */
8
+ userId?: string | null;
9
+ /** Optional authenticated user email */
10
+ userEmail?: string | null;
11
+ /** Optional custom user traits */
12
+ userTraits?: Record<string, unknown>;
13
+ /** Automatically track pageviews & SPA route changes (default: true) */
14
+ trackPageviews?: boolean;
15
+ /** Automatically track clicks with element & coordinate metadata (default: true) */
16
+ trackClicks?: boolean;
17
+ /** Track 25/50/75/100% scroll-depth milestones per pageview (default: true) */
18
+ trackScrollDepth?: boolean;
19
+ /** Track active engaged time vs. idle time (default: true) */
20
+ trackEngagement?: boolean;
21
+ /** Detect rage clicks and dead clicks (default: true) */
22
+ trackFriction?: boolean;
23
+ /** Automatically track outbound links and file downloads (default: true) */
24
+ trackOutbound?: boolean;
25
+ /** Track form submissions and form abandonment without capturing input values (default: true) */
26
+ trackForms?: boolean;
27
+ /** Capture uncaught JS errors and unhandled promise rejections (default: true) */
28
+ trackErrors?: boolean;
29
+ /** Capture Core Web Vitals (LCP, INP, CLS, TTFB) (default: true) */
30
+ trackWebVitals?: boolean;
31
+ /** Debounce window (ms) for batching telemetry via fetchLater (default: 2000) */
32
+ batchWindowMs?: number;
33
+ /** Disable tracking in local dev or when opted out (default: false) */
34
+ disabled?: boolean;
35
+ }
36
+
37
+ /** Manually track a custom event from anywhere in your app */
38
+ declare function trackEvent(eventName: string, props?: Record<string, unknown>): void;
39
+ /** Associate the current visitor with an authenticated user */
40
+ declare function identifyUser(userId: string, traits?: {
41
+ email?: string;
42
+ [key: string]: unknown;
43
+ }): void;
44
+ /** Force-flush any batched events immediately */
45
+ declare function flushAnalytics(): void;
46
+ declare function Analytics({ appId, endpoint, userId, userEmail, userTraits, trackPageviews, trackClicks, trackScrollDepth, trackEngagement, trackFriction, trackOutbound, trackForms, trackErrors, trackWebVitals, batchWindowMs, disabled, }: AnalyticsProps): null;
47
+
48
+ export { Analytics, type AnalyticsEventType, type AnalyticsProps, flushAnalytics, identifyUser, trackEvent };
@@ -0,0 +1,48 @@
1
+ type AnalyticsEventType = "pageview" | "click" | "custom" | "leave" | "scroll" | "rage_click" | "dead_click" | "outbound" | "download" | "form_submit" | "form_abandon" | "error" | "web_vital";
2
+ interface AnalyticsProps {
3
+ /** Registered app slug from your Pulse Analytics dashboard */
4
+ appId: string;
5
+ /** Full URL to your Pulse Analytics /api/collect endpoint */
6
+ endpoint: string;
7
+ /** Optional authenticated user ID from your host app */
8
+ userId?: string | null;
9
+ /** Optional authenticated user email */
10
+ userEmail?: string | null;
11
+ /** Optional custom user traits */
12
+ userTraits?: Record<string, unknown>;
13
+ /** Automatically track pageviews & SPA route changes (default: true) */
14
+ trackPageviews?: boolean;
15
+ /** Automatically track clicks with element & coordinate metadata (default: true) */
16
+ trackClicks?: boolean;
17
+ /** Track 25/50/75/100% scroll-depth milestones per pageview (default: true) */
18
+ trackScrollDepth?: boolean;
19
+ /** Track active engaged time vs. idle time (default: true) */
20
+ trackEngagement?: boolean;
21
+ /** Detect rage clicks and dead clicks (default: true) */
22
+ trackFriction?: boolean;
23
+ /** Automatically track outbound links and file downloads (default: true) */
24
+ trackOutbound?: boolean;
25
+ /** Track form submissions and form abandonment without capturing input values (default: true) */
26
+ trackForms?: boolean;
27
+ /** Capture uncaught JS errors and unhandled promise rejections (default: true) */
28
+ trackErrors?: boolean;
29
+ /** Capture Core Web Vitals (LCP, INP, CLS, TTFB) (default: true) */
30
+ trackWebVitals?: boolean;
31
+ /** Debounce window (ms) for batching telemetry via fetchLater (default: 2000) */
32
+ batchWindowMs?: number;
33
+ /** Disable tracking in local dev or when opted out (default: false) */
34
+ disabled?: boolean;
35
+ }
36
+
37
+ /** Manually track a custom event from anywhere in your app */
38
+ declare function trackEvent(eventName: string, props?: Record<string, unknown>): void;
39
+ /** Associate the current visitor with an authenticated user */
40
+ declare function identifyUser(userId: string, traits?: {
41
+ email?: string;
42
+ [key: string]: unknown;
43
+ }): void;
44
+ /** Force-flush any batched events immediately */
45
+ declare function flushAnalytics(): void;
46
+ declare function Analytics({ appId, endpoint, userId, userEmail, userTraits, trackPageviews, trackClicks, trackScrollDepth, trackEngagement, trackFriction, trackOutbound, trackForms, trackErrors, trackWebVitals, batchWindowMs, disabled, }: AnalyticsProps): null;
47
+
48
+ export { Analytics, type AnalyticsEventType, type AnalyticsProps, flushAnalytics, identifyUser, trackEvent };