@funnelsgrove/analytics 0.1.24 → 0.1.26

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.
@@ -7,7 +7,7 @@ const DEFAULT_OPTIONS = {
7
7
  batchSize: 20,
8
8
  flushIntervalMs: 1500,
9
9
  };
10
- const POSTHOG_BATCH_ENDPOINT = '/batch/';
10
+ const POSTHOG_EVENT_ENDPOINT = '/i/v0/e/';
11
11
  const DEFAULT_FUNNEL_DOMAIN_SIGNAL = 'funnelsgrove';
12
12
  const normalizeEnvironment = (value) => {
13
13
  if (typeof value !== 'string') {
@@ -96,6 +96,13 @@ const toDurationMs = (startedAt, endedAt) => {
96
96
  }
97
97
  return endedMs - startedMs;
98
98
  };
99
+ const toPostHogOffset = (occurredAt) => {
100
+ const occurredMs = new Date(occurredAt).getTime();
101
+ if (!Number.isFinite(occurredMs)) {
102
+ return 0;
103
+ }
104
+ return Math.max(0, Date.now() - occurredMs);
105
+ };
99
106
  const buildEventId = () => {
100
107
  var _a;
101
108
  if (canUseDom() && typeof ((_a = window.crypto) === null || _a === void 0 ? void 0 : _a.randomUUID) === 'function') {
@@ -549,17 +556,14 @@ export class PublicAnalyticsSdkService {
549
556
  const eventEnvironment = event.environment || analyticsEnvironment;
550
557
  return {
551
558
  event: toPostHogEventName(event.eventType),
552
- properties: buildDefinedRecord(Object.assign(Object.assign({ distinct_id: event.userId || this.ensureUserId(), $groups: {
559
+ properties: buildDefinedRecord(Object.assign(Object.assign({ token: POSTHOG_PROJECT_API_KEY, distinct_id: event.userId || this.ensureUserId(), $groups: {
553
560
  project: PROJECT_ID,
554
561
  }, $insert_id: event.eventId, project_id: PROJECT_ID, funnel_id: FUNNEL_ID || undefined, funnel_version_id: FUNNEL_VERSION_ID || undefined, environment: eventEnvironment, test: eventEnvironment === 'test' || undefined, step_id: event.stepId || undefined, step_name: event.stepName || undefined, step_type: event.stepType || undefined, started_at: event.startedAt || undefined, ended_at: event.endedAt || undefined, duration_ms: (_a = event.durationMs) !== null && _a !== void 0 ? _a : undefined, engagement_threshold_ms: (_b = event.engagementThresholdMs) !== null && _b !== void 0 ? _b : undefined, event_source: 'client' }, buildFeatureFlagProperties(event.featureFlags)), { selected: Object.keys(event.selected).length > 0 ? event.selected : undefined, payload: Object.keys(event.payload).length > 0 ? event.payload : undefined, context: Object.keys(event.context).length > 0 ? event.context : undefined, metadata: Object.keys(event.metadata).length > 0 ? event.metadata : undefined })),
555
- timestamp: event.occurredAt,
562
+ offset: toPostHogOffset(event.occurredAt),
556
563
  };
557
564
  });
558
- const payload = JSON.stringify({
559
- api_key: POSTHOG_PROJECT_API_KEY,
560
- batch,
561
- });
562
- const endpoint = `${POSTHOG_API_HOST}${POSTHOG_BATCH_ENDPOINT}`;
565
+ const payload = JSON.stringify(batch);
566
+ const endpoint = `${POSTHOG_API_HOST}${POSTHOG_EVENT_ENDPOINT}`;
563
567
  if (canUseDom() && typeof navigator.sendBeacon === 'function') {
564
568
  const accepted = navigator.sendBeacon(endpoint, new Blob([payload], { type: 'application/json' }));
565
569
  if (accepted) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/analytics",
3
- "version": "0.1.24",
3
+ "version": "0.1.26",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "main": "./dist/index.js",