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/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ # 2.1.0 - 2022-1-26
2
+
3
+ 1. uses v3 decide endpoint
4
+ 2. JSON payloads will be returned with feature flags
5
+ 3. Feature flags will gracefully fail and optimistically save evaluated flags if server is down
6
+ # 2.0.1 - 2023-01-25
7
+
8
+ - Ensures the distinctId used in `.groupIdentify` is the same as the currently identified user
package/lib/index.cjs.js CHANGED
@@ -150,6 +150,7 @@ var PostHogPersistedProperty;
150
150
  PostHogPersistedProperty["DistinctId"] = "distinct_id";
151
151
  PostHogPersistedProperty["Props"] = "props";
152
152
  PostHogPersistedProperty["FeatureFlags"] = "feature_flags";
153
+ PostHogPersistedProperty["FeatureFlagPayloads"] = "feature_flag_payloads";
153
154
  PostHogPersistedProperty["OverrideFeatureFlags"] = "override_feature_flags";
154
155
  PostHogPersistedProperty["Queue"] = "queue";
155
156
  PostHogPersistedProperty["OptedOut"] = "opted_out";
@@ -758,6 +759,7 @@ var PostHogCore = /** @class */ (function () {
758
759
  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);
759
760
  }, {});
760
761
  this.setKnownFeatureFlags(activeFlags);
762
+ (options === null || options === void 0 ? void 0 : options.bootstrap.featureFlagPayloads) && this.setKnownFeatureFlagPayloads(options === null || options === void 0 ? void 0 : options.bootstrap.featureFlagPayloads);
761
763
  }
762
764
  };
763
765
  Object.defineProperty(PostHogCore.prototype, "props", {
@@ -937,11 +939,10 @@ var PostHogCore = /** @class */ (function () {
937
939
  return this;
938
940
  };
939
941
  PostHogCore.prototype.groupIdentify = function (groupType, groupKey, groupProperties) {
940
- var payload = {
942
+ var payload = this.buildPayload({
941
943
  event: '$groupidentify',
942
- distinctId: "$".concat(groupType, "_").concat(groupKey),
943
944
  properties: __assign({ $group_type: groupType, $group_key: groupKey, $group_set: groupProperties || {} }, this.getCommonEventProperties()),
944
- };
945
+ });
945
946
  this.enqueue('capture', payload);
946
947
  return this;
947
948
  };
@@ -982,7 +983,7 @@ var PostHogCore = /** @class */ (function () {
982
983
  var url, distinctId, groups, personProperties, groupProperties, fetchOptions;
983
984
  var _this = this;
984
985
  return __generator(this, function (_a) {
985
- url = "".concat(this.host, "/decide/?v=2");
986
+ url = "".concat(this.host, "/decide/?v=3");
986
987
  distinctId = this.getDistinctId();
987
988
  groups = this.props.$groups || {};
988
989
  personProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
@@ -1003,7 +1004,17 @@ var PostHogCore = /** @class */ (function () {
1003
1004
  .then(function (r) { return r.json(); })
1004
1005
  .then(function (res) {
1005
1006
  if (res.featureFlags) {
1006
- _this.setKnownFeatureFlags(res.featureFlags);
1007
+ var newFeatureFlags = res.featureFlags;
1008
+ var newFeatureFlagPayloads = res.featureFlagPayloads;
1009
+ if (res.errorsWhileComputingFlags) {
1010
+ // if not all flags were computed, we upsert flags instead of replacing them
1011
+ var currentFlags = _this.getPersistedProperty(PostHogPersistedProperty.FeatureFlags);
1012
+ var currentFlagPayloads = _this.getPersistedProperty(PostHogPersistedProperty.FeatureFlagPayloads);
1013
+ newFeatureFlags = __assign(__assign({}, currentFlags), res.featureFlags);
1014
+ newFeatureFlagPayloads = __assign(__assign({}, currentFlagPayloads), res.featureFlagPayloads);
1015
+ }
1016
+ _this.setKnownFeatureFlags(newFeatureFlags);
1017
+ _this.setKnownFeatureFlagPayloads(newFeatureFlagPayloads);
1007
1018
  }
1008
1019
  return res;
1009
1020
  })
@@ -1018,6 +1029,9 @@ var PostHogCore = /** @class */ (function () {
1018
1029
  this.setPersistedProperty(PostHogPersistedProperty.FeatureFlags, featureFlags);
1019
1030
  this._events.emit('featureflags', featureFlags);
1020
1031
  };
1032
+ PostHogCore.prototype.setKnownFeatureFlagPayloads = function (featureFlagPayloads) {
1033
+ this.setPersistedProperty(PostHogPersistedProperty.FeatureFlagPayloads, featureFlagPayloads);
1034
+ };
1021
1035
  PostHogCore.prototype.getFeatureFlag = function (key) {
1022
1036
  var featureFlags = this.getFeatureFlags();
1023
1037
  if (!featureFlags) {
@@ -1025,8 +1039,9 @@ var PostHogCore = /** @class */ (function () {
1025
1039
  return undefined;
1026
1040
  }
1027
1041
  var response = featureFlags[key];
1042
+ // `/decide` v3 returns all flags
1028
1043
  if (response === undefined) {
1029
- // `/decide` returns nothing for flags which are false.
1044
+ // For cases where the flag is unknown, return false
1030
1045
  response = false;
1031
1046
  }
1032
1047
  if (this.sendFeatureFlagEvent && !this.flagCallReported[key]) {
@@ -1039,6 +1054,37 @@ var PostHogCore = /** @class */ (function () {
1039
1054
  // If we have flags we either return the value (true or string) or false
1040
1055
  return response;
1041
1056
  };
1057
+ PostHogCore.prototype.getFeatureFlagPayload = function (key) {
1058
+ var payloads = this.getFeatureFlagPayloads();
1059
+ if (!payloads) {
1060
+ return undefined;
1061
+ }
1062
+ var response = payloads[key];
1063
+ // Undefined means a loading or missing data issue. Null means evaluation happened and there was no match
1064
+ if (response === undefined) {
1065
+ return null;
1066
+ }
1067
+ return this._parsePayload(response);
1068
+ };
1069
+ PostHogCore.prototype.getFeatureFlagPayloads = function () {
1070
+ var _this = this;
1071
+ var payloads = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlagPayloads);
1072
+ if (payloads) {
1073
+ return Object.fromEntries(Object.entries(payloads).map(function (_a) {
1074
+ var k = _a[0], v = _a[1];
1075
+ return [k, _this._parsePayload(v)];
1076
+ }));
1077
+ }
1078
+ return payloads;
1079
+ };
1080
+ PostHogCore.prototype._parsePayload = function (response) {
1081
+ try {
1082
+ return JSON.parse(response);
1083
+ }
1084
+ catch (_a) {
1085
+ return response;
1086
+ }
1087
+ };
1042
1088
  PostHogCore.prototype.getFeatureFlags = function () {
1043
1089
  var flags = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlags);
1044
1090
  var overriddenFlags = this.getPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags);
@@ -1056,6 +1102,14 @@ var PostHogCore = /** @class */ (function () {
1056
1102
  }
1057
1103
  return flags;
1058
1104
  };
1105
+ PostHogCore.prototype.getFeatureFlagsAndPayloads = function () {
1106
+ var flags = this.getFeatureFlags();
1107
+ var payloads = this.getFeatureFlagPayloads();
1108
+ return {
1109
+ flags: flags,
1110
+ payloads: payloads,
1111
+ };
1112
+ };
1059
1113
  PostHogCore.prototype.isFeatureEnabled = function (key) {
1060
1114
  var response = this.getFeatureFlag(key);
1061
1115
  if (response === undefined) {
@@ -1241,7 +1295,7 @@ var PostHogCore = /** @class */ (function () {
1241
1295
  return PostHogCore;
1242
1296
  }());
1243
1297
 
1244
- var version = "2.0.0";
1298
+ var version = "2.1.0";
1245
1299
 
1246
1300
  function getContext(window) {
1247
1301
  var context = {};