pulsemetrics-sdk 0.0.0-stage → 1.0.2

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,161 @@
1
- # Temporary Holding Version
1
+ # pulsemetrics
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
+ A simple and lightweight client SDK to track Daily Active Users (DAU) in modern web applications (React, Next.js, Node.js, Vue, etc.).
4
+
5
+ ---
6
+
7
+ ## Getting Started
8
+
9
+ ### Step 1: Get your API Key
10
+ 1. Create a project on the dashboard.
11
+ 2. Go to **Project Settings**.
12
+ 3. Copy your **API Key**.
13
+
14
+ ### Step 2: Install the Package
15
+
16
+ ```bash
17
+ npm install pulsemetrics-sdk
18
+ ```
19
+
20
+ Or using bun, yarn, or pnpm:
21
+
22
+ ```bash
23
+ bun add pulsemetrics-sdk
24
+ # or
25
+ yarn add pulsemetrics-sdk
26
+ # or
27
+ pnpm add pulsemetrics-sdk
28
+ ```
29
+
30
+ ---
31
+
32
+ ## Basic Setup
33
+
34
+ Create a centralized tracker instance (e.g., `lib/dau.js` or `utils/dau.ts`):
35
+
36
+ ```javascript
37
+ import Pulsemetrics from "pulsemetrics-sdk";
38
+
39
+ export const dau = new Pulsemetrics("YOUR_API_KEY");
40
+ ```
41
+
42
+ ---
43
+
44
+ ## Recommended Integration Patterns (React / Next.js)
45
+
46
+ Daily active users can be captured effectively at multiple stages of the user lifecycle:
47
+
48
+ ### Pattern 1: Right After Successful Login
49
+ Call `dau.init(userId)` immediately when the user logs in via email/password or OAuth:
50
+
51
+ ```javascript
52
+ import { dau } from "@/lib/dau";
53
+
54
+ async function handleLogin(credentials) {
55
+ const response = await api.post("/auth/login", credentials);
56
+ const user = response.data.user;
57
+
58
+ // Track DAU on login
59
+ await dau.init(user.id);
60
+ }
61
+ ```
62
+
63
+ ---
64
+
65
+ ### Pattern 2: On Page Refresh / Session Restore (`/api/me`)
66
+ In SPAs and Next.js apps, users refresh pages while remaining logged in. Modern apps usually call an endpoint like `/api/me`, `/api/auth/session`, or "Who Am I" to restore session state. Trigger DAU tracking right after this call:
67
+
68
+ ```javascript
69
+ import { useEffect } from "react";
70
+ import { dau } from "@/lib/dau";
71
+
72
+ export function useSessionRestore() {
73
+ useEffect(() => {
74
+ async function fetchCurrentUser() {
75
+ try {
76
+ const response = await fetch("/api/me");
77
+ if (response.ok) {
78
+ const userData = await response.json();
79
+ // Track DAU every time page is refreshed or session is verified
80
+ await dau.init(userData.id);
81
+ }
82
+ } catch (error) {
83
+ console.error("Session restore error:", error);
84
+ }
85
+ }
86
+
87
+ fetchCurrentUser();
88
+ }, []);
89
+ }
90
+ ```
91
+
92
+ ---
93
+
94
+ ### Pattern 3: Auth Interceptor & Token Refresh
95
+ When an access token expires and an HTTP interceptor (like Axios or custom fetch) refreshes the token to regain access, trigger DAU tracking:
96
+
97
+ ```javascript
98
+ import axios from "axios";
99
+ import { dau } from "@/lib/dau";
100
+
101
+ axios.interceptors.response.use(
102
+ (response) => response,
103
+ async (error) => {
104
+ if (error.response?.status === 401) {
105
+ // 1. Refresh token
106
+ const newSession = await refreshAccessToken();
107
+
108
+ // 2. Track DAU upon successful re-authentication
109
+ if (newSession?.user?.id) {
110
+ await dau.init(newSession.user.id);
111
+ }
112
+
113
+ // 3. Retry original request
114
+ return axios(error.config);
115
+ }
116
+ return Promise.reject(error);
117
+ }
118
+ );
119
+ ```
120
+
121
+ ---
122
+
123
+ ### Pattern 4: Centralized Inside Auth Context
124
+ If you use an `AuthProvider` in React or Next.js, place `dau.init(user.id)` inside your global auth state listener:
125
+
126
+ ```javascript
127
+ import React, { createContext, useEffect, useState } from "react";
128
+ import { dau } from "@/lib/dau";
129
+
130
+ export const AuthContext = createContext(null);
131
+
132
+ export function AuthProvider({ children }) {
133
+ const [user, setUser] = useState(null);
134
+
135
+ useEffect(() => {
136
+ if (user?.id) {
137
+ dau.init(user.id).catch(console.error);
138
+ }
139
+ }, [user?.id]);
140
+
141
+ return (
142
+ <AuthContext.Provider value={{ user, setUser }}>
143
+ {children}
144
+ </AuthContext.Provider>
145
+ );
146
+ }
147
+ ```
148
+
149
+ ---
150
+
151
+ ## API Reference
152
+
153
+ ### `new Pulsemetrics(apiKey: string)`
154
+ - **apiKey**: Required. The API key obtained from your Project Settings.
155
+
156
+ ### `tracker.init(userId: string)`
157
+ - **userId**: Required. The unique identifier of the user (e.g. database ID, UUID).
158
+ - Returns a Promise resolving to the API response.
159
+
160
+ ## License
161
+ MIT
@@ -0,0 +1,20 @@
1
+ export interface PulsemetricsResponse {
2
+ success?: boolean;
3
+ message?: string;
4
+ [key: string]: any;
5
+ }
6
+ export declare class Pulsemetrics {
7
+ private apiKey;
8
+ private apiUrl;
9
+ /**
10
+ * Creates an instance of Pulsemetrics.
11
+ * @param apiKey The API key passed in headers for authentication.
12
+ */
13
+ constructor(apiKey: string);
14
+ /**
15
+ * Initializes and sends a tracking record for a user.
16
+ * @param userId The unique identifier of the user.
17
+ */
18
+ init(userId: string): Promise<PulsemetricsResponse>;
19
+ }
20
+ export default Pulsemetrics;
package/dist/index.js ADDED
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Pulsemetrics = void 0;
4
+ class Pulsemetrics {
5
+ /**
6
+ * Creates an instance of Pulsemetrics.
7
+ * @param apiKey The API key passed in headers for authentication.
8
+ */
9
+ constructor(apiKey) {
10
+ this.apiUrl = "https://api.pulsemetrics.in/api/v1/dau/track";
11
+ if (!apiKey || typeof apiKey !== "string" || apiKey.trim() === "") {
12
+ throw new Error("Pulsemetrics: API key is required and must be a valid string.");
13
+ }
14
+ this.apiKey = apiKey.trim();
15
+ }
16
+ /**
17
+ * Initializes and sends a tracking record for a user.
18
+ * @param userId The unique identifier of the user.
19
+ */
20
+ async init(userId) {
21
+ if (!userId || typeof userId !== "string" || userId.trim() === "") {
22
+ throw new Error("Pulsemetrics: User ID is required and must be a valid string.");
23
+ }
24
+ const response = await fetch(this.apiUrl, {
25
+ method: "POST",
26
+ headers: {
27
+ "Content-Type": "application/json",
28
+ "x-api-key": this.apiKey,
29
+ },
30
+ body: JSON.stringify({ user: userId.trim() }),
31
+ });
32
+ if (!response.ok) {
33
+ const errorBody = await response.text().catch(() => "");
34
+ throw new Error(`Pulsemetrics: Request failed with status ${response.status} (${response.statusText}). ${errorBody}`);
35
+ }
36
+ return (await response.json());
37
+ }
38
+ }
39
+ exports.Pulsemetrics = Pulsemetrics;
40
+ exports.default = Pulsemetrics;
41
+ if (typeof module !== "undefined" && module.exports) {
42
+ module.exports = Pulsemetrics;
43
+ module.exports.default = Pulsemetrics;
44
+ module.exports.Pulsemetrics = Pulsemetrics;
45
+ }
package/package.json CHANGED
@@ -1,6 +1,27 @@
1
1
  {
2
2
  "name": "pulsemetrics-sdk",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.2",
4
+ "description": "Client SDK for tracking Daily Active Users (DAU)",
5
+ "homepage": "https://pulsemetrics.in",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "README.md"
11
+ ],
12
+ "scripts": {
13
+ "build": "tsc"
14
+ },
15
+ "keywords": [
16
+ "pulsemetrics",
17
+ "dau",
18
+ "analytics",
19
+ "tracker",
20
+ "sdk"
21
+ ],
22
+ "author": "Galerin",
23
+ "license": "MIT",
24
+ "devDependencies": {
25
+ "typescript": "^5.0.0"
26
+ }
27
+ }