humanbehavior-js 0.1.6 → 0.1.8

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.
@@ -275,7 +275,7 @@ declare class HumanBehaviorAPI {
275
275
  sendEventsChunked(events: any[], sessionId: string): Promise<any[]>;
276
276
  sendUserData(userId: string, userData: Record<string, any>, sessionId: string): Promise<any>;
277
277
  sendUserAuth(userId: string, userData: Record<string, any>, sessionId: string, authFields: string[]): Promise<any>;
278
- sendBeaconEvents(events: any[], sessionId: string): void;
278
+ sendBeaconEvents(events: any[], sessionId: string): boolean;
279
279
  sendCustomEvent(sessionId: string, eventName: string, eventProperties?: Record<string, any>): Promise<any>;
280
280
  sendCustomEventBatch(sessionId: string, events: Array<{
281
281
  eventName: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "humanbehavior-js",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "SDK for HumanBehavior session and event recording",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/index.js",
package/src/api.ts CHANGED
@@ -220,16 +220,24 @@ export class HumanBehaviorAPI {
220
220
  }
221
221
 
222
222
  public sendBeaconEvents(events: any[], sessionId: string) {
223
- const data = new URLSearchParams()
224
- data.append('events', encodeURIComponent(JSON.stringify(events)))
225
- data.append('sessionId', encodeURIComponent(sessionId))
226
- data.append('timestamp', encodeURIComponent(Date.now().toString()))
227
- data.append('apiKey', encodeURIComponent(this.apiKey))
223
+ // Create JSON payload that matches the server's expected format
224
+ const payload = {
225
+ sessionId: sessionId,
226
+ events: events,
227
+ endUserId: null // Beacon doesn't have user context
228
+ };
229
+
230
+ // Convert to Blob for sendBeacon
231
+ const blob = new Blob([JSON.stringify(payload)], {
232
+ type: 'application/json'
233
+ });
228
234
 
229
235
  const success = navigator.sendBeacon(
230
236
  `${this.baseUrl}/api/ingestion/events`,
231
- data
237
+ blob
232
238
  );
239
+
240
+ return success;
233
241
  }
234
242
 
235
243
  async sendCustomEvent(sessionId: string, eventName: string, eventProperties?: Record<string, any>) {
@@ -113,9 +113,11 @@ export const HumanBehaviorProvider = ({ apiKey, client, children, options }: Hum
113
113
  setHumanBehavior(tracker);
114
114
 
115
115
  // Wait for initialization to complete
116
- tracker.initializationPromise?.then(() => {
117
- setIsInitialized(true);
118
-
116
+ tracker.initializationPromise?.then(async () => {
117
+ // Start the tracker to enable event flushing and recording
118
+ await tracker.start();
119
+ setIsInitialized(true);
120
+
119
121
  // Process any queued events
120
122
  const currentQueue = eventQueueRef.current;
121
123
  if (currentQueue.length > 0) {