humanbehavior-js 0.2.9 → 0.3.1

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.
@@ -324,9 +324,11 @@ interface ServerSideUserData {
324
324
  * Identify user from server-side (NextAuth, Firebase, etc.)
325
325
  * Use this in your auth events to track users immediately on sign-in
326
326
  */
327
- declare function identifyUser(userData: ServerSideUserData, apiKey: string): Promise<{
327
+ declare function identifyUser(userId: string, // Separate userId parameter (should be stable unique identifier)
328
+ userData: ServerSideUserData, apiKey: string): Promise<{
328
329
  success: boolean;
329
330
  error?: string;
331
+ actualUserId?: string;
330
332
  }>;
331
333
 
332
334
  declare enum LogLevel {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "humanbehavior-js",
3
- "version": "0.2.9",
3
+ "version": "0.3.1",
4
4
  "description": "SDK for HumanBehavior session and event recording",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/index.js",
package/src/server.ts CHANGED
@@ -15,9 +15,10 @@ export interface ServerSideUserData {
15
15
  * Use this in your auth events to track users immediately on sign-in
16
16
  */
17
17
  export async function identifyUser(
18
+ userId: string, // Separate userId parameter (should be stable unique identifier)
18
19
  userData: ServerSideUserData,
19
20
  apiKey: string
20
- ): Promise<{ success: boolean; error?: string }> {
21
+ ): Promise<{ success: boolean; error?: string; actualUserId?: string }> {
21
22
  try {
22
23
  const response = await fetch('https://ingest.humanbehavior.co/api/ingestion/user', {
23
24
  method: 'POST',
@@ -26,8 +27,9 @@ export async function identifyUser(
26
27
  'Authorization': `Bearer ${apiKey}`
27
28
  },
28
29
  body: JSON.stringify({
29
- userId: userData.email,
30
- userAttributes: userData,
30
+ userId: userId, // Use stable unique identifier
31
+ userAttributes: userData, // Email and other data go here
32
+ sessionId: 'server-side-identification', // Dummy sessionId for server-side requests
31
33
  posthogName: userData.email || userData.name || null // Update posthogName with email
32
34
  })
33
35
  });
@@ -40,6 +42,17 @@ export async function identifyUser(
40
42
  };
41
43
  }
42
44
 
45
+ const result = await response.json();
46
+
47
+ // If server found existing user, return the actual userId
48
+ if (result.actualUserId && result.actualUserId !== userId) {
49
+ console.log(`🔄 Server found existing user: ${result.actualUserId} (sent: ${userId})`);
50
+ return {
51
+ success: true,
52
+ actualUserId: result.actualUserId
53
+ };
54
+ }
55
+
43
56
  return { success: true };
44
57
  } catch (error) {
45
58
  return {