pulsemetrics-sdk 1.0.2 → 1.1.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 +54 -17
- package/dist/index.d.ts +13 -0
- package/dist/index.js +37 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# pulsemetrics
|
|
1
|
+
# pulsemetrics-sdk
|
|
2
2
|
|
|
3
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
4
|
|
|
@@ -39,14 +39,31 @@ import Pulsemetrics from "pulsemetrics-sdk";
|
|
|
39
39
|
export const dau = new Pulsemetrics("YOUR_API_KEY");
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
+
### Method 1: `init`
|
|
43
|
+
Available for Free to Enterprise plans.
|
|
44
|
+
|
|
45
|
+
```javascript
|
|
46
|
+
await dau.init("logged_in_user_id");
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Method 2: `trackWithName`
|
|
50
|
+
Available for Pro and Enterprise plans.
|
|
51
|
+
|
|
52
|
+
```javascript
|
|
53
|
+
await dau.trackWithName({
|
|
54
|
+
userId: "logged_in_user_id",
|
|
55
|
+
userName: "GUEST", // e.g. userName: string = "GUEST"
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
42
59
|
---
|
|
43
60
|
|
|
44
|
-
##
|
|
61
|
+
## Examples (React / Next.js)
|
|
45
62
|
|
|
46
|
-
Daily active users can be captured effectively at multiple stages of the user lifecycle:
|
|
63
|
+
Daily active users can be captured effectively at multiple stages of the user lifecycle using either `dau.init` (Free to Enterprise plans) or `dau.trackWithName` (Pro and Enterprise plans):
|
|
47
64
|
|
|
48
65
|
### Pattern 1: Right After Successful Login
|
|
49
|
-
Call `dau.init
|
|
66
|
+
Call `dau.init` or `dau.trackWithName` immediately when the user logs in via email/password or OAuth:
|
|
50
67
|
|
|
51
68
|
```javascript
|
|
52
69
|
import { dau } from "@/lib/dau";
|
|
@@ -55,8 +72,14 @@ async function handleLogin(credentials) {
|
|
|
55
72
|
const response = await api.post("/auth/login", credentials);
|
|
56
73
|
const user = response.data.user;
|
|
57
74
|
|
|
58
|
-
//
|
|
75
|
+
// Option 1: Free to Enterprise plans
|
|
59
76
|
await dau.init(user.id);
|
|
77
|
+
|
|
78
|
+
// Option 2: Pro and Enterprise plans
|
|
79
|
+
await dau.trackWithName({
|
|
80
|
+
userId: user.id,
|
|
81
|
+
userName: user.name || "GUEST",
|
|
82
|
+
});
|
|
60
83
|
}
|
|
61
84
|
```
|
|
62
85
|
|
|
@@ -76,8 +99,15 @@ export function useSessionRestore() {
|
|
|
76
99
|
const response = await fetch("/api/me");
|
|
77
100
|
if (response.ok) {
|
|
78
101
|
const userData = await response.json();
|
|
79
|
-
|
|
102
|
+
|
|
103
|
+
// Option 1: Free to Enterprise plans
|
|
80
104
|
await dau.init(userData.id);
|
|
105
|
+
|
|
106
|
+
// Option 2: Pro and Enterprise plans
|
|
107
|
+
await dau.trackWithName({
|
|
108
|
+
userId: userData.id,
|
|
109
|
+
userName: userData.name || "GUEST",
|
|
110
|
+
});
|
|
81
111
|
}
|
|
82
112
|
} catch (error) {
|
|
83
113
|
console.error("Session restore error:", error);
|
|
@@ -107,7 +137,14 @@ axios.interceptors.response.use(
|
|
|
107
137
|
|
|
108
138
|
// 2. Track DAU upon successful re-authentication
|
|
109
139
|
if (newSession?.user?.id) {
|
|
140
|
+
// Option 1: Free to Enterprise plans
|
|
110
141
|
await dau.init(newSession.user.id);
|
|
142
|
+
|
|
143
|
+
// Option 2: Pro and Enterprise plans
|
|
144
|
+
await dau.trackWithName({
|
|
145
|
+
userId: newSession.user.id,
|
|
146
|
+
userName: newSession.user.name || "GUEST",
|
|
147
|
+
});
|
|
111
148
|
}
|
|
112
149
|
|
|
113
150
|
// 3. Retry original request
|
|
@@ -121,7 +158,7 @@ axios.interceptors.response.use(
|
|
|
121
158
|
---
|
|
122
159
|
|
|
123
160
|
### Pattern 4: Centralized Inside Auth Context
|
|
124
|
-
If you use an `AuthProvider` in React or Next.js, place `dau.init
|
|
161
|
+
If you use an `AuthProvider` in React or Next.js, place `dau.init` or `dau.trackWithName` inside your global auth state listener:
|
|
125
162
|
|
|
126
163
|
```javascript
|
|
127
164
|
import React, { createContext, useEffect, useState } from "react";
|
|
@@ -134,9 +171,18 @@ export function AuthProvider({ children }) {
|
|
|
134
171
|
|
|
135
172
|
useEffect(() => {
|
|
136
173
|
if (user?.id) {
|
|
174
|
+
// Option 1: Free to Enterprise plans
|
|
137
175
|
dau.init(user.id).catch(console.error);
|
|
176
|
+
|
|
177
|
+
// Option 2: Pro and Enterprise plans
|
|
178
|
+
dau
|
|
179
|
+
.trackWithName({
|
|
180
|
+
userId: user.id,
|
|
181
|
+
userName: user.name || "GUEST",
|
|
182
|
+
})
|
|
183
|
+
.catch(console.error);
|
|
138
184
|
}
|
|
139
|
-
}, [user?.id]);
|
|
185
|
+
}, [user?.id, user?.name]);
|
|
140
186
|
|
|
141
187
|
return (
|
|
142
188
|
<AuthContext.Provider value={{ user, setUser }}>
|
|
@@ -148,14 +194,5 @@ export function AuthProvider({ children }) {
|
|
|
148
194
|
|
|
149
195
|
---
|
|
150
196
|
|
|
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
197
|
## License
|
|
161
198
|
MIT
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,10 @@ export interface PulsemetricsResponse {
|
|
|
3
3
|
message?: string;
|
|
4
4
|
[key: string]: any;
|
|
5
5
|
}
|
|
6
|
+
export interface ITrackWithUserNameParams {
|
|
7
|
+
userId: string;
|
|
8
|
+
userName: string;
|
|
9
|
+
}
|
|
6
10
|
export declare class Pulsemetrics {
|
|
7
11
|
private apiKey;
|
|
8
12
|
private apiUrl;
|
|
@@ -16,5 +20,14 @@ export declare class Pulsemetrics {
|
|
|
16
20
|
* @param userId The unique identifier of the user.
|
|
17
21
|
*/
|
|
18
22
|
init(userId: string): Promise<PulsemetricsResponse>;
|
|
23
|
+
/**
|
|
24
|
+
* Sends a DAU tracking record with both a user ID and user name.
|
|
25
|
+
*
|
|
26
|
+
* @remarks Available on Pro and Enterprise plans only. Free and Starter plans are not supported.
|
|
27
|
+
* @param params - User tracking payload.
|
|
28
|
+
* @param params.userId - The unique identifier of the user.
|
|
29
|
+
* @param params.userName - The display name of the user.
|
|
30
|
+
*/
|
|
31
|
+
trackWithName({ userId, userName, }: ITrackWithUserNameParams): Promise<PulsemetricsResponse>;
|
|
19
32
|
}
|
|
20
33
|
export default Pulsemetrics;
|
package/dist/index.js
CHANGED
|
@@ -7,7 +7,7 @@ class Pulsemetrics {
|
|
|
7
7
|
* @param apiKey The API key passed in headers for authentication.
|
|
8
8
|
*/
|
|
9
9
|
constructor(apiKey) {
|
|
10
|
-
this.apiUrl = "https://api.pulsemetrics.in/api/v1/dau
|
|
10
|
+
this.apiUrl = "https://api.pulsemetrics.in/api/v1/dau";
|
|
11
11
|
if (!apiKey || typeof apiKey !== "string" || apiKey.trim() === "") {
|
|
12
12
|
throw new Error("Pulsemetrics: API key is required and must be a valid string.");
|
|
13
13
|
}
|
|
@@ -21,7 +21,7 @@ class Pulsemetrics {
|
|
|
21
21
|
if (!userId || typeof userId !== "string" || userId.trim() === "") {
|
|
22
22
|
throw new Error("Pulsemetrics: User ID is required and must be a valid string.");
|
|
23
23
|
}
|
|
24
|
-
const response = await fetch(this.apiUrl
|
|
24
|
+
const response = await fetch(`${this.apiUrl}/track`, {
|
|
25
25
|
method: "POST",
|
|
26
26
|
headers: {
|
|
27
27
|
"Content-Type": "application/json",
|
|
@@ -35,6 +35,41 @@ class Pulsemetrics {
|
|
|
35
35
|
}
|
|
36
36
|
return (await response.json());
|
|
37
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Sends a DAU tracking record with both a user ID and user name.
|
|
40
|
+
*
|
|
41
|
+
* @remarks Available on Pro and Enterprise plans only. Free and Starter plans are not supported.
|
|
42
|
+
* @param params - User tracking payload.
|
|
43
|
+
* @param params.userId - The unique identifier of the user.
|
|
44
|
+
* @param params.userName - The display name of the user.
|
|
45
|
+
*/
|
|
46
|
+
async trackWithName({ userId, userName, }) {
|
|
47
|
+
if (!userId || typeof userId !== "string" || userId.trim() === "") {
|
|
48
|
+
throw new Error("Pulsemetrics: User ID is required and must be a valid string.");
|
|
49
|
+
}
|
|
50
|
+
if (!userName ||
|
|
51
|
+
typeof userName !== "string" ||
|
|
52
|
+
userName.trim() === "") {
|
|
53
|
+
throw new Error("Pulsemetrics: User name is required and must be a valid string.");
|
|
54
|
+
}
|
|
55
|
+
const payload = {
|
|
56
|
+
userId: userId.trim(),
|
|
57
|
+
userName: userName.trim(),
|
|
58
|
+
};
|
|
59
|
+
const response = await fetch(`${this.apiUrl}/track-with-name`, {
|
|
60
|
+
method: "POST",
|
|
61
|
+
headers: {
|
|
62
|
+
"Content-Type": "application/json",
|
|
63
|
+
"x-api-key": this.apiKey,
|
|
64
|
+
},
|
|
65
|
+
body: JSON.stringify(payload),
|
|
66
|
+
});
|
|
67
|
+
if (!response.ok) {
|
|
68
|
+
const errorBody = await response.text().catch(() => "");
|
|
69
|
+
throw new Error(`Pulsemetrics: Request failed with status ${response.status} (${response.statusText}). ${errorBody}`);
|
|
70
|
+
}
|
|
71
|
+
return (await response.json());
|
|
72
|
+
}
|
|
38
73
|
}
|
|
39
74
|
exports.Pulsemetrics = Pulsemetrics;
|
|
40
75
|
exports.default = Pulsemetrics;
|