posthog-node 4.0.0 → 4.1.0

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/lib/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  declare type PostHogCoreOptions = {
3
- /** PostHog API host, usually 'https://app.posthog.com' or 'https://eu.posthog.com' */
3
+ /** PostHog API host, usually 'https://us.i.posthog.com' or 'https://eu.i.posthog.com' */
4
4
  host?: string;
5
5
  /** The number of events to queue before sending to PostHog (flushing) */
6
6
  flushAt?: number;
@@ -37,7 +37,7 @@ declare type PostHogCoreOptions = {
37
37
  featureFlagsRequestTimeoutMs?: number;
38
38
  /** For Session Analysis how long before we expire a session (defaults to 30 mins) */
39
39
  sessionExpirationTimeSeconds?: number;
40
- /** Whether to post events to PostHog in JSON or compressed format */
40
+ /** Whether to post events to PostHog in JSON or compressed format. Defaults to 'form' */
41
41
  captureMode?: 'json' | 'form';
42
42
  disableGeoip?: boolean;
43
43
  };
package/lib/index.esm.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createHash } from 'rusha';
2
2
 
3
- var version = "4.0.0";
3
+ var version = "4.1.0";
4
4
 
5
5
  var PostHogPersistedProperty;
6
6
  (function (PostHogPersistedProperty) {
@@ -956,7 +956,7 @@ class PostHogCoreStateless {
956
956
  this._isInitialized = false;
957
957
  assert(apiKey, "You must pass your PostHog project's api key.");
958
958
  this.apiKey = apiKey;
959
- this.host = removeTrailingSlash(options?.host || 'https://app.posthog.com');
959
+ this.host = removeTrailingSlash(options?.host || 'https://us.i.posthog.com');
960
960
  this.flushAt = options?.flushAt ? Math.max(options?.flushAt, 1) : 20;
961
961
  this.maxBatchSize = Math.max(this.flushAt, options?.maxBatchSize ?? 100);
962
962
  this.maxQueueSize = Math.max(this.flushAt, options?.maxQueueSize ?? 1000);
@@ -1153,14 +1153,11 @@ class PostHogCoreStateless {
1153
1153
  if (response === undefined) {
1154
1154
  return null;
1155
1155
  }
1156
- return this._parsePayload(response);
1156
+ return response;
1157
1157
  }
1158
1158
  async getFeatureFlagPayloadsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip) {
1159
1159
  await this._initPromise;
1160
1160
  const payloads = (await this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)).payloads;
1161
- if (payloads) {
1162
- return Object.fromEntries(Object.entries(payloads).map(([k, v]) => [k, this._parsePayload(v)]));
1163
- }
1164
1161
  return payloads;
1165
1162
  }
1166
1163
  _parsePayload(response) {
@@ -1184,9 +1181,13 @@ class PostHogCoreStateless {
1184
1181
  const decideResponse = await this.getDecide(distinctId, groups, personProperties, groupProperties, extraPayload);
1185
1182
  const flags = decideResponse?.featureFlags;
1186
1183
  const payloads = decideResponse?.featureFlagPayloads;
1184
+ let parsedPayloads = payloads;
1185
+ if (payloads) {
1186
+ parsedPayloads = Object.fromEntries(Object.entries(payloads).map(([k, v]) => [k, this._parsePayload(v)]));
1187
+ }
1187
1188
  return {
1188
1189
  flags,
1189
- payloads,
1190
+ payloads: parsedPayloads,
1190
1191
  };
1191
1192
  }
1192
1193
  /***
@@ -1539,11 +1540,15 @@ class FeatureFlagsPoller {
1539
1540
  } // Undefined means a loading or missing data issue. Null means evaluation happened and there was no match
1540
1541
 
1541
1542
 
1542
- if (response === undefined) {
1543
+ if (response === undefined || response === null) {
1543
1544
  return null;
1544
1545
  }
1545
1546
 
1546
- return response;
1547
+ try {
1548
+ return JSON.parse(response);
1549
+ } catch {
1550
+ return response;
1551
+ }
1547
1552
  }
1548
1553
 
1549
1554
  async getAllFlagsAndPayloads(distinctId, groups = {}, personProperties = {}, groupProperties = {}) {
@@ -1677,7 +1682,7 @@ class FeatureFlagsPoller {
1677
1682
  let matches = false;
1678
1683
 
1679
1684
  if (propertyType === 'cohort') {
1680
- matches = matchCohort(prop, properties, this.cohorts);
1685
+ matches = matchCohort(prop, properties, this.cohorts, this.debugMode);
1681
1686
  } else {
1682
1687
  matches = matchProperty(prop, properties);
1683
1688
  }
@@ -1933,7 +1938,7 @@ function matchProperty(property, propertyValues) {
1933
1938
  }
1934
1939
  }
1935
1940
 
1936
- function matchCohort(property, propertyValues, cohortProperties) {
1941
+ function matchCohort(property, propertyValues, cohortProperties, debugMode = false) {
1937
1942
  const cohortId = String(property.value);
1938
1943
 
1939
1944
  if (!(cohortId in cohortProperties)) {
@@ -1941,10 +1946,10 @@ function matchCohort(property, propertyValues, cohortProperties) {
1941
1946
  }
1942
1947
 
1943
1948
  const propertyGroup = cohortProperties[cohortId];
1944
- return matchPropertyGroup(propertyGroup, propertyValues, cohortProperties);
1949
+ return matchPropertyGroup(propertyGroup, propertyValues, cohortProperties, debugMode);
1945
1950
  }
1946
1951
 
1947
- function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties) {
1952
+ function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties, debugMode = false) {
1948
1953
  if (!propertyGroup) {
1949
1954
  return true;
1950
1955
  }
@@ -1963,7 +1968,7 @@ function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties) {
1963
1968
  // a nested property group
1964
1969
  for (const prop of properties) {
1965
1970
  try {
1966
- const matches = matchPropertyGroup(prop, propertyValues, cohortProperties);
1971
+ const matches = matchPropertyGroup(prop, propertyValues, cohortProperties, debugMode);
1967
1972
 
1968
1973
  if (propertyGroupType === 'AND') {
1969
1974
  if (!matches) {
@@ -1977,7 +1982,10 @@ function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties) {
1977
1982
  }
1978
1983
  } catch (err) {
1979
1984
  if (err instanceof InconclusiveMatchError) {
1980
- console.debug(`Failed to compute property ${prop} locally: ${err}`);
1985
+ if (debugMode) {
1986
+ console.debug(`Failed to compute property ${prop} locally: ${err}`);
1987
+ }
1988
+
1981
1989
  errorMatchingLocally = true;
1982
1990
  } else {
1983
1991
  throw err;
@@ -1997,7 +2005,7 @@ function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties) {
1997
2005
  let matches;
1998
2006
 
1999
2007
  if (prop.type === 'cohort') {
2000
- matches = matchCohort(prop, propertyValues, cohortProperties);
2008
+ matches = matchCohort(prop, propertyValues, cohortProperties, debugMode);
2001
2009
  } else {
2002
2010
  matches = matchProperty(prop, propertyValues);
2003
2011
  }
@@ -2025,7 +2033,10 @@ function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties) {
2025
2033
  }
2026
2034
  } catch (err) {
2027
2035
  if (err instanceof InconclusiveMatchError) {
2028
- console.debug(`Failed to compute property ${prop} locally: ${err}`);
2036
+ if (debugMode) {
2037
+ console.debug(`Failed to compute property ${prop} locally: ${err}`);
2038
+ }
2039
+
2029
2040
  errorMatchingLocally = true;
2030
2041
  } else {
2031
2042
  throw err;
@@ -2374,11 +2385,7 @@ class PostHog extends PostHogCoreStateless {
2374
2385
  response = await super.getFeatureFlagPayloadStateless(key, distinctId, groups, personProperties, groupProperties, disableGeoip);
2375
2386
  }
2376
2387
 
2377
- try {
2378
- return JSON.parse(response);
2379
- } catch {
2380
- return response;
2381
- }
2388
+ return response;
2382
2389
  }
2383
2390
 
2384
2391
  async isFeatureEnabled(key, distinctId, options) {
@@ -2512,7 +2519,7 @@ class PostHogSentryIntegration {
2512
2519
  this.organization = organization;
2513
2520
  this.prefix = prefix;
2514
2521
  this.name = 'posthog-node';
2515
- this.posthogHost = posthog.options.host ?? 'https://app.posthog.com';
2522
+ this.posthogHost = posthog.options.host ?? 'https://us.i.posthog.com';
2516
2523
  }
2517
2524
 
2518
2525
  setupOnce(addGlobalEventProcessor, getCurrentHub) {