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.
package/dist/cjs/index.js CHANGED
@@ -5948,7 +5948,8 @@ if (isBrowser) {
5948
5948
  * Identify user from server-side (NextAuth, Firebase, etc.)
5949
5949
  * Use this in your auth events to track users immediately on sign-in
5950
5950
  */
5951
- function identifyUser(userData, apiKey) {
5951
+ function identifyUser(userId, // Separate userId parameter (should be stable unique identifier)
5952
+ userData, apiKey) {
5952
5953
  return __awaiter$1(this, void 0, void 0, function* () {
5953
5954
  try {
5954
5955
  const response = yield fetch('https://ingest.humanbehavior.co/api/ingestion/user', {
@@ -5958,8 +5959,9 @@ function identifyUser(userData, apiKey) {
5958
5959
  'Authorization': `Bearer ${apiKey}`
5959
5960
  },
5960
5961
  body: JSON.stringify({
5961
- userId: userData.email,
5962
- userAttributes: userData,
5962
+ userId: userId, // Use stable unique identifier
5963
+ userAttributes: userData, // Email and other data go here
5964
+ sessionId: 'server-side-identification', // Dummy sessionId for server-side requests
5963
5965
  posthogName: userData.email || userData.name || null // Update posthogName with email
5964
5966
  })
5965
5967
  });
@@ -5970,6 +5972,15 @@ function identifyUser(userData, apiKey) {
5970
5972
  error: `Failed to identify user: ${response.status} ${response.statusText} - ${errorText}`
5971
5973
  };
5972
5974
  }
5975
+ const result = yield response.json();
5976
+ // If server found existing user, return the actual userId
5977
+ if (result.actualUserId && result.actualUserId !== userId) {
5978
+ console.log(`🔄 Server found existing user: ${result.actualUserId} (sent: ${userId})`);
5979
+ return {
5980
+ success: true,
5981
+ actualUserId: result.actualUserId
5982
+ };
5983
+ }
5973
5984
  return { success: true };
5974
5985
  }
5975
5986
  catch (error) {