posthog-node 4.0.0-beta.3 → 4.0.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/CHANGELOG.md CHANGED
@@ -1,3 +1,33 @@
1
+
2
+ # 4.0.1 - 2024-04-25
3
+
4
+ 1. Prevent double JSON parsing of feature flag payloads, which would convert the payload [1] into 1.
5
+
6
+ # 4.0.0 - 2024-03-18
7
+
8
+ ## Added
9
+
10
+ 1. Adds a `disabled` option and the ability to change it later via `posthog.disabled = true`. Useful for disabling PostHog tracking for example in a testing environment without having complex conditional checking
11
+ 2. Adds a new `featureFlagsRequestTimeoutMs` timeout parameter for feature flags which defaults to 3 seconds, updated from the default 10s for all other API calls.
12
+ 3. `shutdown` takes a `shutdownTimeoutMs` param with a default of 30000 (30s). This is the time to wait for flushing events before shutting down the client. If the timeout is reached, the client will be shut down regardless of pending events.
13
+ 4. Flushes will now try to flush up to `maxBatchSize` (default 100) in one go
14
+ 5. Queued events are limited up to `maxQueueSize` (default 1000) and the oldest events are dropped when the limit is reached
15
+ 6. Sets `User-Agent` headers with SDK name and version for RN
16
+
17
+ ## Removed
18
+
19
+ 1. `flushAsync` and `shutdownAsync` are removed with `flush` and `shutdown` now being the async methods.
20
+
21
+ ## Changed
22
+
23
+ 1. `flush` and `shutdown` now being async methods.
24
+
25
+ ## Fixed
26
+
27
+ 1. Fixed an issue where `shutdown` would potentially exit early if a flush was already in progress
28
+ 2. Fixes some typos in types
29
+
30
+
1
31
  # 4.0.0-beta.3 - 2024-03-13
2
32
 
3
33
  1. Sets `User-Agent` headers with SDK name and version for RN
package/lib/index.cjs.js CHANGED
@@ -4,7 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var rusha = require('rusha');
6
6
 
7
- var version = "4.0.0-beta.3";
7
+ var version = "4.0.1";
8
8
 
9
9
  var PostHogPersistedProperty;
10
10
  (function (PostHogPersistedProperty) {
@@ -963,6 +963,7 @@ class PostHogCoreStateless {
963
963
  this.host = removeTrailingSlash(options?.host || 'https://app.posthog.com');
964
964
  this.flushAt = options?.flushAt ? Math.max(options?.flushAt, 1) : 20;
965
965
  this.maxBatchSize = Math.max(this.flushAt, options?.maxBatchSize ?? 100);
966
+ this.maxQueueSize = Math.max(this.flushAt, options?.maxQueueSize ?? 1000);
966
967
  this.flushInterval = options?.flushInterval ?? 10000;
967
968
  this.captureMode = options?.captureMode || 'form';
968
969
  // If enable is explicitly set to false we override the optout
@@ -1156,14 +1157,11 @@ class PostHogCoreStateless {
1156
1157
  if (response === undefined) {
1157
1158
  return null;
1158
1159
  }
1159
- return this._parsePayload(response);
1160
+ return response;
1160
1161
  }
1161
1162
  async getFeatureFlagPayloadsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip) {
1162
1163
  await this._initPromise;
1163
1164
  const payloads = (await this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)).payloads;
1164
- if (payloads) {
1165
- return Object.fromEntries(Object.entries(payloads).map(([k, v]) => [k, this._parsePayload(v)]));
1166
- }
1167
1165
  return payloads;
1168
1166
  }
1169
1167
  _parsePayload(response) {
@@ -1187,9 +1185,13 @@ class PostHogCoreStateless {
1187
1185
  const decideResponse = await this.getDecide(distinctId, groups, personProperties, groupProperties, extraPayload);
1188
1186
  const flags = decideResponse?.featureFlags;
1189
1187
  const payloads = decideResponse?.featureFlagPayloads;
1188
+ let parsedPayloads = payloads;
1189
+ if (payloads) {
1190
+ parsedPayloads = Object.fromEntries(Object.entries(payloads).map(([k, v]) => [k, this._parsePayload(v)]));
1191
+ }
1190
1192
  return {
1191
1193
  flags,
1192
- payloads,
1194
+ payloads: parsedPayloads,
1193
1195
  };
1194
1196
  }
1195
1197
  /***
@@ -1221,6 +1223,10 @@ class PostHogCoreStateless {
1221
1223
  delete message.distinctId;
1222
1224
  }
1223
1225
  const queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
1226
+ if (queue.length >= this.maxQueueSize) {
1227
+ queue.shift();
1228
+ console.info('Queue is full, the oldest event is dropped.');
1229
+ }
1224
1230
  queue.push({ message });
1225
1231
  this.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
1226
1232
  this._events.emit(type, message);
@@ -1538,11 +1544,15 @@ class FeatureFlagsPoller {
1538
1544
  } // Undefined means a loading or missing data issue. Null means evaluation happened and there was no match
1539
1545
 
1540
1546
 
1541
- if (response === undefined) {
1547
+ if (response === undefined || response === null) {
1542
1548
  return null;
1543
1549
  }
1544
1550
 
1545
- return response;
1551
+ try {
1552
+ return JSON.parse(response);
1553
+ } catch {
1554
+ return response;
1555
+ }
1546
1556
  }
1547
1557
 
1548
1558
  async getAllFlagsAndPayloads(distinctId, groups = {}, personProperties = {}, groupProperties = {}) {
@@ -1676,7 +1686,7 @@ class FeatureFlagsPoller {
1676
1686
  let matches = false;
1677
1687
 
1678
1688
  if (propertyType === 'cohort') {
1679
- matches = matchCohort(prop, properties, this.cohorts);
1689
+ matches = matchCohort(prop, properties, this.cohorts, this.debugMode);
1680
1690
  } else {
1681
1691
  matches = matchProperty(prop, properties);
1682
1692
  }
@@ -1932,7 +1942,7 @@ function matchProperty(property, propertyValues) {
1932
1942
  }
1933
1943
  }
1934
1944
 
1935
- function matchCohort(property, propertyValues, cohortProperties) {
1945
+ function matchCohort(property, propertyValues, cohortProperties, debugMode = false) {
1936
1946
  const cohortId = String(property.value);
1937
1947
 
1938
1948
  if (!(cohortId in cohortProperties)) {
@@ -1940,10 +1950,10 @@ function matchCohort(property, propertyValues, cohortProperties) {
1940
1950
  }
1941
1951
 
1942
1952
  const propertyGroup = cohortProperties[cohortId];
1943
- return matchPropertyGroup(propertyGroup, propertyValues, cohortProperties);
1953
+ return matchPropertyGroup(propertyGroup, propertyValues, cohortProperties, debugMode);
1944
1954
  }
1945
1955
 
1946
- function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties) {
1956
+ function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties, debugMode = false) {
1947
1957
  if (!propertyGroup) {
1948
1958
  return true;
1949
1959
  }
@@ -1962,7 +1972,7 @@ function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties) {
1962
1972
  // a nested property group
1963
1973
  for (const prop of properties) {
1964
1974
  try {
1965
- const matches = matchPropertyGroup(prop, propertyValues, cohortProperties);
1975
+ const matches = matchPropertyGroup(prop, propertyValues, cohortProperties, debugMode);
1966
1976
 
1967
1977
  if (propertyGroupType === 'AND') {
1968
1978
  if (!matches) {
@@ -1976,7 +1986,10 @@ function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties) {
1976
1986
  }
1977
1987
  } catch (err) {
1978
1988
  if (err instanceof InconclusiveMatchError) {
1979
- console.debug(`Failed to compute property ${prop} locally: ${err}`);
1989
+ if (debugMode) {
1990
+ console.debug(`Failed to compute property ${prop} locally: ${err}`);
1991
+ }
1992
+
1980
1993
  errorMatchingLocally = true;
1981
1994
  } else {
1982
1995
  throw err;
@@ -1996,7 +2009,7 @@ function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties) {
1996
2009
  let matches;
1997
2010
 
1998
2011
  if (prop.type === 'cohort') {
1999
- matches = matchCohort(prop, propertyValues, cohortProperties);
2012
+ matches = matchCohort(prop, propertyValues, cohortProperties, debugMode);
2000
2013
  } else {
2001
2014
  matches = matchProperty(prop, propertyValues);
2002
2015
  }
@@ -2024,7 +2037,10 @@ function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties) {
2024
2037
  }
2025
2038
  } catch (err) {
2026
2039
  if (err instanceof InconclusiveMatchError) {
2027
- console.debug(`Failed to compute property ${prop} locally: ${err}`);
2040
+ if (debugMode) {
2041
+ console.debug(`Failed to compute property ${prop} locally: ${err}`);
2042
+ }
2043
+
2028
2044
  errorMatchingLocally = true;
2029
2045
  } else {
2030
2046
  throw err;
@@ -2373,11 +2389,7 @@ class PostHog extends PostHogCoreStateless {
2373
2389
  response = await super.getFeatureFlagPayloadStateless(key, distinctId, groups, personProperties, groupProperties, disableGeoip);
2374
2390
  }
2375
2391
 
2376
- try {
2377
- return JSON.parse(response);
2378
- } catch {
2379
- return response;
2380
- }
2392
+ return response;
2381
2393
  }
2382
2394
 
2383
2395
  async isFeatureEnabled(key, distinctId, options) {