posthog-js-lite 3.4.1 → 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,11 @@
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
+
3
9
  # 3.4.1 - 2025-02-20
4
10
 
5
11
  ## Fixed
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
@@ -954,6 +954,11 @@ class PostHogFetchNetworkError extends Error {
954
954
  function isPostHogFetchError(err) {
955
955
  return typeof err === 'object' && (err instanceof PostHogFetchHttpError || err instanceof PostHogFetchNetworkError);
956
956
  }
957
+ var QuotaLimitedFeature;
958
+ (function (QuotaLimitedFeature) {
959
+ QuotaLimitedFeature["FeatureFlags"] = "feature_flags";
960
+ QuotaLimitedFeature["Recordings"] = "recordings";
961
+ })(QuotaLimitedFeature || (QuotaLimitedFeature = {}));
957
962
  class PostHogCoreStateless {
958
963
  constructor(apiKey, options) {
959
964
  this.flushPromise = null;
@@ -1197,6 +1202,14 @@ class PostHogCoreStateless {
1197
1202
  extraPayload['geoip_disable'] = true;
1198
1203
  }
1199
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
+ }
1200
1213
  const flags = decideResponse?.featureFlags;
1201
1214
  const payloads = decideResponse?.featureFlagPayloads;
1202
1215
  let parsedPayloads = payloads;
@@ -1749,7 +1762,7 @@ class PostHogCore extends PostHogCoreStateless {
1749
1762
  }
1750
1763
  async _decideAsync(sendAnonDistinctId = true) {
1751
1764
  this._decideResponsePromise = this._initPromise
1752
- .then(() => {
1765
+ .then(async () => {
1753
1766
  const distinctId = this.getDistinctId();
1754
1767
  const groups = this.props.$groups || {};
1755
1768
  const personProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
@@ -1758,38 +1771,45 @@ class PostHogCore extends PostHogCoreStateless {
1758
1771
  const extraProperties = {
1759
1772
  $anon_distinct_id: sendAnonDistinctId ? this.getAnonymousId() : undefined,
1760
1773
  };
1761
- return super.getDecide(distinctId, groups, personProperties, groupProperties, extraProperties).then((res) => {
1762
- if (res?.featureFlags) {
1763
- // clear flag call reported if we have new flags since they might have changed
1764
- if (this.sendFeatureFlagEvent) {
1765
- this.flagCallReported = {};
1766
- }
1767
- let newFeatureFlags = res.featureFlags;
1768
- let newFeatureFlagPayloads = res.featureFlagPayloads;
1769
- if (res.errorsWhileComputingFlags) {
1770
- // if not all flags were computed, we upsert flags instead of replacing them
1771
- const currentFlags = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlags);
1772
- this.logMsgIfDebug(() => console.log('PostHog Debug', 'Cached feature flags: ', JSON.stringify(currentFlags)));
1773
- const currentFlagPayloads = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlagPayloads);
1774
- newFeatureFlags = { ...currentFlags, ...res.featureFlags };
1775
- newFeatureFlagPayloads = { ...currentFlagPayloads, ...res.featureFlagPayloads };
1776
- }
1777
- this.setKnownFeatureFlags(newFeatureFlags);
1778
- this.setKnownFeatureFlagPayloads(Object.fromEntries(Object.entries(newFeatureFlagPayloads || {}).map(([k, v]) => [k, this._parsePayload(v)])));
1779
- // Mark that we hit the /decide endpoint so we can capture this in the $feature_flag_called event
1780
- this.setPersistedProperty(PostHogPersistedProperty.DecideEndpointWasHit, true);
1781
- const sessionReplay = res?.sessionRecording;
1782
- if (sessionReplay) {
1783
- this.setPersistedProperty(PostHogPersistedProperty.SessionReplay, sessionReplay);
1784
- this.logMsgIfDebug(() => console.log('PostHog Debug', 'Session replay config: ', JSON.stringify(sessionReplay)));
1785
- }
1786
- else {
1787
- this.logMsgIfDebug(() => console.info('PostHog Debug', 'Session replay config disabled.'));
1788
- this.setPersistedProperty(PostHogPersistedProperty.SessionReplay, null);
1789
- }
1790
- }
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');
1791
1781
  return res;
1792
- });
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;
1793
1813
  })
1794
1814
  .finally(() => {
1795
1815
  this._decideResponsePromise = undefined;
@@ -1970,7 +1990,7 @@ class PostHogCore extends PostHogCoreStateless {
1970
1990
  }
1971
1991
  }
1972
1992
 
1973
- var version = "3.4.1";
1993
+ var version = "3.4.2";
1974
1994
 
1975
1995
  function getContext(window) {
1976
1996
  let context = {};