posthog-js-lite 2.0.0 → 2.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
@@ -9,6 +9,7 @@ declare type PosthogCoreOptions = {
9
9
  distinctId?: string;
10
10
  isIdentifiedId?: boolean;
11
11
  featureFlags?: Record<string, boolean | string>;
12
+ featureFlagPayloads?: Record<string, JsonType>;
12
13
  };
13
14
  fetchRetryCount?: number;
14
15
  fetchRetryDelay?: number;
@@ -21,6 +22,7 @@ declare enum PostHogPersistedProperty {
21
22
  DistinctId = "distinct_id",
22
23
  Props = "props",
23
24
  FeatureFlags = "feature_flags",
25
+ FeatureFlagPayloads = "feature_flag_payloads",
24
26
  OverrideFeatureFlags = "override_feature_flags",
25
27
  Queue = "queue",
26
28
  OptedOut = "opted_out",
@@ -70,8 +72,15 @@ declare type PostHogDecideResponse = {
70
72
  featureFlags: {
71
73
  [key: string]: string | boolean;
72
74
  };
75
+ featureFlagPayloads: {
76
+ [key: string]: JsonType;
77
+ };
78
+ errorsWhileComputingFlags: boolean;
73
79
  sessionRecording: boolean;
74
- };
80
+ };
81
+ declare type JsonType = string | number | boolean | null | {
82
+ [key: string]: JsonType;
83
+ } | Array<JsonType>;
75
84
 
76
85
  interface RetriableOptions {
77
86
  retryCount?: number;
@@ -164,8 +173,16 @@ declare abstract class PostHogCore {
164
173
  private decideAsync;
165
174
  private _decideAsync;
166
175
  private setKnownFeatureFlags;
176
+ private setKnownFeatureFlagPayloads;
167
177
  getFeatureFlag(key: string): boolean | string | undefined;
178
+ getFeatureFlagPayload(key: string): JsonType | undefined;
179
+ getFeatureFlagPayloads(): PostHogDecideResponse['featureFlagPayloads'] | undefined;
180
+ _parsePayload(response: any): any;
168
181
  getFeatureFlags(): PostHogDecideResponse['featureFlags'] | undefined;
182
+ getFeatureFlagsAndPayloads(): {
183
+ flags: PostHogDecideResponse['featureFlags'] | undefined;
184
+ payloads: PostHogDecideResponse['featureFlagPayloads'] | undefined;
185
+ };
169
186
  isFeatureEnabled(key: string): boolean | undefined;
170
187
  reloadFeatureFlagsAsync(sendAnonDistinctId?: boolean): Promise<PostHogDecideResponse['featureFlags']>;
171
188
  onFeatureFlags(cb: (flags: PostHogDecideResponse['featureFlags']) => void): () => void;
package/lib/index.esm.js CHANGED
@@ -146,6 +146,7 @@ var PostHogPersistedProperty;
146
146
  PostHogPersistedProperty["DistinctId"] = "distinct_id";
147
147
  PostHogPersistedProperty["Props"] = "props";
148
148
  PostHogPersistedProperty["FeatureFlags"] = "feature_flags";
149
+ PostHogPersistedProperty["FeatureFlagPayloads"] = "feature_flag_payloads";
149
150
  PostHogPersistedProperty["OverrideFeatureFlags"] = "override_feature_flags";
150
151
  PostHogPersistedProperty["Queue"] = "queue";
151
152
  PostHogPersistedProperty["OptedOut"] = "opted_out";
@@ -754,6 +755,7 @@ var PostHogCore = /** @class */ (function () {
754
755
  return ((res[key] = ((_b = (_a = options.bootstrap) === null || _a === void 0 ? void 0 : _a.featureFlags) === null || _b === void 0 ? void 0 : _b[key]) || false), res);
755
756
  }, {});
756
757
  this.setKnownFeatureFlags(activeFlags);
758
+ (options === null || options === void 0 ? void 0 : options.bootstrap.featureFlagPayloads) && this.setKnownFeatureFlagPayloads(options === null || options === void 0 ? void 0 : options.bootstrap.featureFlagPayloads);
757
759
  }
758
760
  };
759
761
  Object.defineProperty(PostHogCore.prototype, "props", {
@@ -933,11 +935,10 @@ var PostHogCore = /** @class */ (function () {
933
935
  return this;
934
936
  };
935
937
  PostHogCore.prototype.groupIdentify = function (groupType, groupKey, groupProperties) {
936
- var payload = {
938
+ var payload = this.buildPayload({
937
939
  event: '$groupidentify',
938
- distinctId: "$".concat(groupType, "_").concat(groupKey),
939
940
  properties: __assign({ $group_type: groupType, $group_key: groupKey, $group_set: groupProperties || {} }, this.getCommonEventProperties()),
940
- };
941
+ });
941
942
  this.enqueue('capture', payload);
942
943
  return this;
943
944
  };
@@ -978,7 +979,7 @@ var PostHogCore = /** @class */ (function () {
978
979
  var url, distinctId, groups, personProperties, groupProperties, fetchOptions;
979
980
  var _this = this;
980
981
  return __generator(this, function (_a) {
981
- url = "".concat(this.host, "/decide/?v=2");
982
+ url = "".concat(this.host, "/decide/?v=3");
982
983
  distinctId = this.getDistinctId();
983
984
  groups = this.props.$groups || {};
984
985
  personProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
@@ -999,7 +1000,17 @@ var PostHogCore = /** @class */ (function () {
999
1000
  .then(function (r) { return r.json(); })
1000
1001
  .then(function (res) {
1001
1002
  if (res.featureFlags) {
1002
- _this.setKnownFeatureFlags(res.featureFlags);
1003
+ var newFeatureFlags = res.featureFlags;
1004
+ var newFeatureFlagPayloads = res.featureFlagPayloads;
1005
+ if (res.errorsWhileComputingFlags) {
1006
+ // if not all flags were computed, we upsert flags instead of replacing them
1007
+ var currentFlags = _this.getPersistedProperty(PostHogPersistedProperty.FeatureFlags);
1008
+ var currentFlagPayloads = _this.getPersistedProperty(PostHogPersistedProperty.FeatureFlagPayloads);
1009
+ newFeatureFlags = __assign(__assign({}, currentFlags), res.featureFlags);
1010
+ newFeatureFlagPayloads = __assign(__assign({}, currentFlagPayloads), res.featureFlagPayloads);
1011
+ }
1012
+ _this.setKnownFeatureFlags(newFeatureFlags);
1013
+ _this.setKnownFeatureFlagPayloads(newFeatureFlagPayloads);
1003
1014
  }
1004
1015
  return res;
1005
1016
  })
@@ -1014,6 +1025,9 @@ var PostHogCore = /** @class */ (function () {
1014
1025
  this.setPersistedProperty(PostHogPersistedProperty.FeatureFlags, featureFlags);
1015
1026
  this._events.emit('featureflags', featureFlags);
1016
1027
  };
1028
+ PostHogCore.prototype.setKnownFeatureFlagPayloads = function (featureFlagPayloads) {
1029
+ this.setPersistedProperty(PostHogPersistedProperty.FeatureFlagPayloads, featureFlagPayloads);
1030
+ };
1017
1031
  PostHogCore.prototype.getFeatureFlag = function (key) {
1018
1032
  var featureFlags = this.getFeatureFlags();
1019
1033
  if (!featureFlags) {
@@ -1021,8 +1035,9 @@ var PostHogCore = /** @class */ (function () {
1021
1035
  return undefined;
1022
1036
  }
1023
1037
  var response = featureFlags[key];
1038
+ // `/decide` v3 returns all flags
1024
1039
  if (response === undefined) {
1025
- // `/decide` returns nothing for flags which are false.
1040
+ // For cases where the flag is unknown, return false
1026
1041
  response = false;
1027
1042
  }
1028
1043
  if (this.sendFeatureFlagEvent && !this.flagCallReported[key]) {
@@ -1035,6 +1050,37 @@ var PostHogCore = /** @class */ (function () {
1035
1050
  // If we have flags we either return the value (true or string) or false
1036
1051
  return response;
1037
1052
  };
1053
+ PostHogCore.prototype.getFeatureFlagPayload = function (key) {
1054
+ var payloads = this.getFeatureFlagPayloads();
1055
+ if (!payloads) {
1056
+ return undefined;
1057
+ }
1058
+ var response = payloads[key];
1059
+ // Undefined means a loading or missing data issue. Null means evaluation happened and there was no match
1060
+ if (response === undefined) {
1061
+ return null;
1062
+ }
1063
+ return this._parsePayload(response);
1064
+ };
1065
+ PostHogCore.prototype.getFeatureFlagPayloads = function () {
1066
+ var _this = this;
1067
+ var payloads = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlagPayloads);
1068
+ if (payloads) {
1069
+ return Object.fromEntries(Object.entries(payloads).map(function (_a) {
1070
+ var k = _a[0], v = _a[1];
1071
+ return [k, _this._parsePayload(v)];
1072
+ }));
1073
+ }
1074
+ return payloads;
1075
+ };
1076
+ PostHogCore.prototype._parsePayload = function (response) {
1077
+ try {
1078
+ return JSON.parse(response);
1079
+ }
1080
+ catch (_a) {
1081
+ return response;
1082
+ }
1083
+ };
1038
1084
  PostHogCore.prototype.getFeatureFlags = function () {
1039
1085
  var flags = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlags);
1040
1086
  var overriddenFlags = this.getPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags);
@@ -1052,6 +1098,14 @@ var PostHogCore = /** @class */ (function () {
1052
1098
  }
1053
1099
  return flags;
1054
1100
  };
1101
+ PostHogCore.prototype.getFeatureFlagsAndPayloads = function () {
1102
+ var flags = this.getFeatureFlags();
1103
+ var payloads = this.getFeatureFlagPayloads();
1104
+ return {
1105
+ flags: flags,
1106
+ payloads: payloads,
1107
+ };
1108
+ };
1055
1109
  PostHogCore.prototype.isFeatureEnabled = function (key) {
1056
1110
  var response = this.getFeatureFlag(key);
1057
1111
  if (response === undefined) {
@@ -1237,7 +1291,7 @@ var PostHogCore = /** @class */ (function () {
1237
1291
  return PostHogCore;
1238
1292
  }());
1239
1293
 
1240
- var version = "2.0.0";
1294
+ var version = "2.1.0";
1241
1295
 
1242
1296
  function getContext(window) {
1243
1297
  var context = {};