posthog-js-lite 3.4.0 → 3.4.2

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,5 +1,17 @@
1
1
  # Next
2
2
 
3
+ # 3.4.2 - 2025-02-27
4
+
5
+ ## Fixed
6
+
7
+ 1. Supports gracefully handling quotaLimited responses from the PostHog API for feature flags.
8
+
9
+ # 3.4.1 - 2025-02-20
10
+
11
+ ## Fixed
12
+
13
+ 1. fix: handle cases when non Error is passed to `captureException`
14
+
3
15
  # 3.4.0 - 2025-02-20
4
16
 
5
17
  ## Added
package/README.md CHANGED
@@ -28,9 +28,9 @@ const posthog = new PostHog('my-api-key', {
28
28
  posthog.capture('my-event', { myProperty: 'foo' })
29
29
 
30
30
  // Identify a user (e.g. on login)
31
- posthog.identify('my-unique-user-id', { email: 'exampke@posthog.com', name: 'Jane Doe' })
31
+ posthog.identify('my-unique-user-id', { email: 'example@posthog.com', name: 'Jane Doe' })
32
32
  // ...or with Set Once additional properties
33
- posthog.identify('my-unique-user-id', { $set: { email: 'exampke@posthog.com', name: 'Jane Doe' }, $set_once: { vip: true } })
33
+ posthog.identify('my-unique-user-id', { $set: { email: 'example@posthog.com', name: 'Jane Doe' }, $set_once: { vip: true } })
34
34
 
35
35
  // Reset a user (e.g. on logout)
36
36
  posthog.reset()
package/lib/index.cjs.js CHANGED
@@ -66,6 +66,9 @@ function safeSetTimeout(fn, timeout) {
66
66
  t?.unref && t?.unref();
67
67
  return t;
68
68
  }
69
+ const isError = (x) => {
70
+ return x instanceof Error;
71
+ };
69
72
  function getFetch() {
70
73
  return typeof fetch !== 'undefined' ? fetch : typeof global.fetch !== 'undefined' ? global.fetch : undefined;
71
74
  }
@@ -951,6 +954,11 @@ class PostHogFetchNetworkError extends Error {
951
954
  function isPostHogFetchError(err) {
952
955
  return typeof err === 'object' && (err instanceof PostHogFetchHttpError || err instanceof PostHogFetchNetworkError);
953
956
  }
957
+ var QuotaLimitedFeature;
958
+ (function (QuotaLimitedFeature) {
959
+ QuotaLimitedFeature["FeatureFlags"] = "feature_flags";
960
+ QuotaLimitedFeature["Recordings"] = "recordings";
961
+ })(QuotaLimitedFeature || (QuotaLimitedFeature = {}));
954
962
  class PostHogCoreStateless {
955
963
  constructor(apiKey, options) {
956
964
  this.flushPromise = null;
@@ -1194,6 +1202,14 @@ class PostHogCoreStateless {
1194
1202
  extraPayload['geoip_disable'] = true;
1195
1203
  }
1196
1204
  const decideResponse = await this.getDecide(distinctId, groups, personProperties, groupProperties, extraPayload);
1205
+ // Add check for quota limitation on feature flags
1206
+ if (decideResponse?.quotaLimited?.includes(QuotaLimitedFeature.FeatureFlags)) {
1207
+ console.warn('[FEATURE FLAGS] Feature flags quota limit exceeded - feature flags unavailable. Learn more about billing limits at https://posthog.com/docs/billing/limits-alerts');
1208
+ return {
1209
+ flags: undefined,
1210
+ payloads: undefined,
1211
+ };
1212
+ }
1197
1213
  const flags = decideResponse?.featureFlags;
1198
1214
  const payloads = decideResponse?.featureFlagPayloads;
1199
1215
  let parsedPayloads = payloads;
@@ -1746,7 +1762,7 @@ class PostHogCore extends PostHogCoreStateless {
1746
1762
  }
1747
1763
  async _decideAsync(sendAnonDistinctId = true) {
1748
1764
  this._decideResponsePromise = this._initPromise
1749
- .then(() => {
1765
+ .then(async () => {
1750
1766
  const distinctId = this.getDistinctId();
1751
1767
  const groups = this.props.$groups || {};
1752
1768
  const personProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
@@ -1755,38 +1771,45 @@ class PostHogCore extends PostHogCoreStateless {
1755
1771
  const extraProperties = {
1756
1772
  $anon_distinct_id: sendAnonDistinctId ? this.getAnonymousId() : undefined,
1757
1773
  };
1758
- return super.getDecide(distinctId, groups, personProperties, groupProperties, extraProperties).then((res) => {
1759
- if (res?.featureFlags) {
1760
- // clear flag call reported if we have new flags since they might have changed
1761
- if (this.sendFeatureFlagEvent) {
1762
- this.flagCallReported = {};
1763
- }
1764
- let newFeatureFlags = res.featureFlags;
1765
- let newFeatureFlagPayloads = res.featureFlagPayloads;
1766
- if (res.errorsWhileComputingFlags) {
1767
- // if not all flags were computed, we upsert flags instead of replacing them
1768
- const currentFlags = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlags);
1769
- this.logMsgIfDebug(() => console.log('PostHog Debug', 'Cached feature flags: ', JSON.stringify(currentFlags)));
1770
- const currentFlagPayloads = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlagPayloads);
1771
- newFeatureFlags = { ...currentFlags, ...res.featureFlags };
1772
- newFeatureFlagPayloads = { ...currentFlagPayloads, ...res.featureFlagPayloads };
1773
- }
1774
- this.setKnownFeatureFlags(newFeatureFlags);
1775
- this.setKnownFeatureFlagPayloads(Object.fromEntries(Object.entries(newFeatureFlagPayloads || {}).map(([k, v]) => [k, this._parsePayload(v)])));
1776
- // Mark that we hit the /decide endpoint so we can capture this in the $feature_flag_called event
1777
- this.setPersistedProperty(PostHogPersistedProperty.DecideEndpointWasHit, true);
1778
- const sessionReplay = res?.sessionRecording;
1779
- if (sessionReplay) {
1780
- this.setPersistedProperty(PostHogPersistedProperty.SessionReplay, sessionReplay);
1781
- this.logMsgIfDebug(() => console.log('PostHog Debug', 'Session replay config: ', JSON.stringify(sessionReplay)));
1782
- }
1783
- else {
1784
- this.logMsgIfDebug(() => console.info('PostHog Debug', 'Session replay config disabled.'));
1785
- this.setPersistedProperty(PostHogPersistedProperty.SessionReplay, null);
1786
- }
1787
- }
1774
+ const res = await super.getDecide(distinctId, groups, personProperties, groupProperties, extraProperties);
1775
+ // Add check for quota limitation on feature flags
1776
+ if (res?.quotaLimited?.includes(QuotaLimitedFeature.FeatureFlags)) {
1777
+ // Unset all feature flags by setting to null
1778
+ this.setKnownFeatureFlags(null);
1779
+ this.setKnownFeatureFlagPayloads(null);
1780
+ console.warn('[FEATURE FLAGS] Feature flags quota limit exceeded - unsetting all flags. Learn more about billing limits at https://posthog.com/docs/billing/limits-alerts');
1788
1781
  return res;
1789
- });
1782
+ }
1783
+ if (res?.featureFlags) {
1784
+ // clear flag call reported if we have new flags since they might have changed
1785
+ if (this.sendFeatureFlagEvent) {
1786
+ this.flagCallReported = {};
1787
+ }
1788
+ let newFeatureFlags = res.featureFlags;
1789
+ let newFeatureFlagPayloads = res.featureFlagPayloads;
1790
+ if (res.errorsWhileComputingFlags) {
1791
+ // if not all flags were computed, we upsert flags instead of replacing them
1792
+ const currentFlags = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlags);
1793
+ this.logMsgIfDebug(() => console.log('PostHog Debug', 'Cached feature flags: ', JSON.stringify(currentFlags)));
1794
+ const currentFlagPayloads = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlagPayloads);
1795
+ newFeatureFlags = { ...currentFlags, ...res.featureFlags };
1796
+ newFeatureFlagPayloads = { ...currentFlagPayloads, ...res.featureFlagPayloads };
1797
+ }
1798
+ this.setKnownFeatureFlags(newFeatureFlags);
1799
+ this.setKnownFeatureFlagPayloads(Object.fromEntries(Object.entries(newFeatureFlagPayloads || {}).map(([k, v]) => [k, this._parsePayload(v)])));
1800
+ // Mark that we hit the /decide endpoint so we can capture this in the $feature_flag_called event
1801
+ this.setPersistedProperty(PostHogPersistedProperty.DecideEndpointWasHit, true);
1802
+ const sessionReplay = res?.sessionRecording;
1803
+ if (sessionReplay) {
1804
+ this.setPersistedProperty(PostHogPersistedProperty.SessionReplay, sessionReplay);
1805
+ this.logMsgIfDebug(() => console.log('PostHog Debug', 'Session replay config: ', JSON.stringify(sessionReplay)));
1806
+ }
1807
+ else {
1808
+ this.logMsgIfDebug(() => console.info('PostHog Debug', 'Session replay config disabled.'));
1809
+ this.setPersistedProperty(PostHogPersistedProperty.SessionReplay, null);
1810
+ }
1811
+ }
1812
+ return res;
1790
1813
  })
1791
1814
  .finally(() => {
1792
1815
  this._decideResponsePromise = undefined;
@@ -1928,8 +1951,8 @@ class PostHogCore extends PostHogCoreStateless {
1928
1951
  $exception_level: 'error',
1929
1952
  $exception_list: [
1930
1953
  {
1931
- type: error.name,
1932
- value: error.message,
1954
+ type: isError(error) ? error.name : 'Error',
1955
+ value: isError(error) ? error.message : error,
1933
1956
  mechanism: {
1934
1957
  handled: true,
1935
1958
  synthetic: false,
@@ -1967,7 +1990,7 @@ class PostHogCore extends PostHogCoreStateless {
1967
1990
  }
1968
1991
  }
1969
1992
 
1970
- var version = "3.4.0";
1993
+ var version = "3.4.2";
1971
1994
 
1972
1995
  function getContext(window) {
1973
1996
  let context = {};