posthog-node 2.6.0 → 3.0.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 CHANGED
@@ -1,3 +1,21 @@
1
+ # 3.0.0 - 2023-04-14
2
+
3
+ Breaking change:
4
+
5
+ All events by default now send the `$geoip_disable` property to disable geoip lookup in app. This is because usually we don't
6
+ want to update person properties to take the server's location.
7
+
8
+ The same now happens for feature flag requests, where we discard the IP address of the server for matching on geoip properties like city, country, continent.
9
+
10
+ To restore previous behaviour, you can set the default to False like so:
11
+
12
+ ```javascript
13
+ const posthog = new PostHog(PH_API_KEY, {
14
+ host: PH_HOST,
15
+ disableGeoip: false
16
+ })
17
+ ```
18
+
1
19
  # 2.6.0 - 2023-03-14
2
20
 
3
21
  1. Add support for all cohorts local evaluation in feature flags.
package/lib/index.cjs.js CHANGED
@@ -163,7 +163,7 @@ function __spreadArray(to, from, pack) {
163
163
  return to.concat(ar || Array.prototype.slice.call(from));
164
164
  }
165
165
 
166
- var version = "2.6.0";
166
+ var version = "3.0.0";
167
167
 
168
168
  var PostHogPersistedProperty;
169
169
  (function (PostHogPersistedProperty) {
@@ -724,9 +724,10 @@ var SimpleEventEmitter = /** @class */ (function () {
724
724
 
725
725
  var PostHogCoreStateless = /** @class */ (function () {
726
726
  function PostHogCoreStateless(apiKey, options) {
727
- var _a, _b, _c, _d;
727
+ var _a, _b, _c, _d, _e;
728
728
  this.debugMode = false;
729
729
  this.pendingPromises = {};
730
+ this.disableGeoip = true;
730
731
  // internal
731
732
  this._events = new SimpleEventEmitter();
732
733
  assert(apiKey, "You must pass your PostHog project's api key.");
@@ -742,6 +743,7 @@ var PostHogCoreStateless = /** @class */ (function () {
742
743
  retryDelay: (_c = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _c !== void 0 ? _c : 3000,
743
744
  };
744
745
  this.requestTimeout = (_d = options === null || options === void 0 ? void 0 : options.requestTimeout) !== null && _d !== void 0 ? _d : 10000; // 10 seconds
746
+ this.disableGeoip = (_e = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _e !== void 0 ? _e : true;
745
747
  }
746
748
  PostHogCoreStateless.prototype.getCommonEventProperties = function () {
747
749
  return {
@@ -801,13 +803,13 @@ var PostHogCoreStateless = /** @class */ (function () {
801
803
  this.enqueue('capture', payload, options);
802
804
  return this;
803
805
  };
804
- PostHogCoreStateless.prototype.aliasStateless = function (alias, distinctId, properties) {
806
+ PostHogCoreStateless.prototype.aliasStateless = function (alias, distinctId, properties, options) {
805
807
  var payload = this.buildPayload({
806
808
  event: '$create_alias',
807
809
  distinct_id: distinctId,
808
810
  properties: __assign(__assign({}, (properties || {})), { distinct_id: distinctId, alias: alias }),
809
811
  });
810
- this.enqueue('alias', payload);
812
+ this.enqueue('alias', payload, options);
811
813
  return this;
812
814
  };
813
815
  /***
@@ -848,7 +850,7 @@ var PostHogCoreStateless = /** @class */ (function () {
848
850
  });
849
851
  });
850
852
  };
851
- PostHogCoreStateless.prototype.getFeatureFlagStateless = function (key, distinctId, groups, personProperties, groupProperties) {
853
+ PostHogCoreStateless.prototype.getFeatureFlagStateless = function (key, distinctId, groups, personProperties, groupProperties, disableGeoip) {
852
854
  if (groups === void 0) { groups = {}; }
853
855
  if (personProperties === void 0) { personProperties = {}; }
854
856
  if (groupProperties === void 0) { groupProperties = {}; }
@@ -856,7 +858,7 @@ var PostHogCoreStateless = /** @class */ (function () {
856
858
  var featureFlags, response;
857
859
  return __generator(this, function (_a) {
858
860
  switch (_a.label) {
859
- case 0: return [4 /*yield*/, this.getFeatureFlagsStateless(distinctId, groups, personProperties, groupProperties)];
861
+ case 0: return [4 /*yield*/, this.getFeatureFlagsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)];
860
862
  case 1:
861
863
  featureFlags = _a.sent();
862
864
  if (!featureFlags) {
@@ -875,7 +877,7 @@ var PostHogCoreStateless = /** @class */ (function () {
875
877
  });
876
878
  });
877
879
  };
878
- PostHogCoreStateless.prototype.getFeatureFlagPayloadStateless = function (key, distinctId, groups, personProperties, groupProperties) {
880
+ PostHogCoreStateless.prototype.getFeatureFlagPayloadStateless = function (key, distinctId, groups, personProperties, groupProperties, disableGeoip) {
879
881
  if (groups === void 0) { groups = {}; }
880
882
  if (personProperties === void 0) { personProperties = {}; }
881
883
  if (groupProperties === void 0) { groupProperties = {}; }
@@ -883,7 +885,7 @@ var PostHogCoreStateless = /** @class */ (function () {
883
885
  var payloads, response;
884
886
  return __generator(this, function (_a) {
885
887
  switch (_a.label) {
886
- case 0: return [4 /*yield*/, this.getFeatureFlagPayloadsStateless(distinctId, groups, personProperties, groupProperties)];
888
+ case 0: return [4 /*yield*/, this.getFeatureFlagPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)];
887
889
  case 1:
888
890
  payloads = _a.sent();
889
891
  if (!payloads) {
@@ -899,7 +901,7 @@ var PostHogCoreStateless = /** @class */ (function () {
899
901
  });
900
902
  });
901
903
  };
902
- PostHogCoreStateless.prototype.getFeatureFlagPayloadsStateless = function (distinctId, groups, personProperties, groupProperties) {
904
+ PostHogCoreStateless.prototype.getFeatureFlagPayloadsStateless = function (distinctId, groups, personProperties, groupProperties, disableGeoip) {
903
905
  if (groups === void 0) { groups = {}; }
904
906
  if (personProperties === void 0) { personProperties = {}; }
905
907
  if (groupProperties === void 0) { groupProperties = {}; }
@@ -908,7 +910,7 @@ var PostHogCoreStateless = /** @class */ (function () {
908
910
  var _this = this;
909
911
  return __generator(this, function (_a) {
910
912
  switch (_a.label) {
911
- case 0: return [4 /*yield*/, this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties)];
913
+ case 0: return [4 /*yield*/, this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)];
912
914
  case 1:
913
915
  payloads = (_a.sent()).payloads;
914
916
  if (payloads) {
@@ -930,28 +932,33 @@ var PostHogCoreStateless = /** @class */ (function () {
930
932
  return response;
931
933
  }
932
934
  };
933
- PostHogCoreStateless.prototype.getFeatureFlagsStateless = function (distinctId, groups, personProperties, groupProperties) {
935
+ PostHogCoreStateless.prototype.getFeatureFlagsStateless = function (distinctId, groups, personProperties, groupProperties, disableGeoip) {
934
936
  if (groups === void 0) { groups = {}; }
935
937
  if (personProperties === void 0) { personProperties = {}; }
936
938
  if (groupProperties === void 0) { groupProperties = {}; }
937
939
  return __awaiter(this, void 0, void 0, function () {
938
940
  return __generator(this, function (_a) {
939
941
  switch (_a.label) {
940
- case 0: return [4 /*yield*/, this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties)];
942
+ case 0: return [4 /*yield*/, this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)];
941
943
  case 1: return [2 /*return*/, (_a.sent()).flags];
942
944
  }
943
945
  });
944
946
  });
945
947
  };
946
- PostHogCoreStateless.prototype.getFeatureFlagsAndPayloadsStateless = function (distinctId, groups, personProperties, groupProperties) {
948
+ PostHogCoreStateless.prototype.getFeatureFlagsAndPayloadsStateless = function (distinctId, groups, personProperties, groupProperties, disableGeoip) {
947
949
  if (groups === void 0) { groups = {}; }
948
950
  if (personProperties === void 0) { personProperties = {}; }
949
951
  if (groupProperties === void 0) { groupProperties = {}; }
950
952
  return __awaiter(this, void 0, void 0, function () {
951
- var decideResponse, flags, payloads;
953
+ var extraPayload, decideResponse, flags, payloads;
952
954
  return __generator(this, function (_a) {
953
955
  switch (_a.label) {
954
- case 0: return [4 /*yield*/, this.getDecide(distinctId, groups, personProperties, groupProperties)];
956
+ case 0:
957
+ extraPayload = {};
958
+ if (disableGeoip !== null && disableGeoip !== void 0 ? disableGeoip : this.disableGeoip) {
959
+ extraPayload['geoip_disable'] = true;
960
+ }
961
+ return [4 /*yield*/, this.getDecide(distinctId, groups, personProperties, groupProperties, extraPayload)];
955
962
  case 1:
956
963
  decideResponse = _a.sent();
957
964
  flags = decideResponse === null || decideResponse === void 0 ? void 0 : decideResponse.featureFlags;
@@ -969,11 +976,19 @@ var PostHogCoreStateless = /** @class */ (function () {
969
976
  ***/
970
977
  PostHogCoreStateless.prototype.enqueue = function (type, _message, options) {
971
978
  var _this = this;
979
+ var _a;
972
980
  if (this.optedOut) {
973
981
  this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.enable()");
974
982
  return;
975
983
  }
976
984
  var message = __assign(__assign({}, _message), { type: type, library: this.getLibraryId(), library_version: this.getLibraryVersion(), timestamp: (options === null || options === void 0 ? void 0 : options.timestamp) ? options === null || options === void 0 ? void 0 : options.timestamp : currentISOTime() });
985
+ var addGeoipDisableProperty = (_a = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _a !== void 0 ? _a : this.disableGeoip;
986
+ if (addGeoipDisableProperty) {
987
+ if (!message.properties) {
988
+ message.properties = {};
989
+ }
990
+ message['properties']['$geoip_disable'] = true;
991
+ }
977
992
  if (message.distinctId) {
978
993
  message.distinct_id = message.distinctId;
979
994
  delete message.distinctId;
@@ -1100,11 +1115,13 @@ var PostHogCoreStateless = /** @class */ (function () {
1100
1115
  __extends(PostHogCore, _super);
1101
1116
  function PostHogCore(apiKey, options) {
1102
1117
  var _this = this;
1103
- var _a, _b;
1104
- _this = _super.call(this, apiKey, options) || this;
1118
+ var _a, _b, _c;
1119
+ // Default for stateful mode is to not disable geoip. Only override if explicitly set
1120
+ var disableGeoipOption = (_a = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _a !== void 0 ? _a : false;
1121
+ _this = _super.call(this, apiKey, __assign(__assign({}, options), { disableGeoip: disableGeoipOption })) || this;
1105
1122
  _this.flagCallReported = {};
1106
- _this.sendFeatureFlagEvent = (_a = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _a !== void 0 ? _a : true;
1107
- _this._sessionExpirationTimeSeconds = (_b = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _b !== void 0 ? _b : 1800; // 30 minutes
1123
+ _this.sendFeatureFlagEvent = (_b = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _b !== void 0 ? _b : true;
1124
+ _this._sessionExpirationTimeSeconds = (_c = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _c !== void 0 ? _c : 1800; // 30 minutes
1108
1125
  // NOTE: It is important we don't initiate anything in the constructor as some async IO may still be underway on the parent
1109
1126
  if ((options === null || options === void 0 ? void 0 : options.preloadFeatureFlags) !== false) {
1110
1127
  safeSetTimeout(function () {
@@ -2491,16 +2508,18 @@ function (_super) {
2491
2508
  properties = _a.properties,
2492
2509
  groups = _a.groups,
2493
2510
  sendFeatureFlags = _a.sendFeatureFlags,
2494
- timestamp = _a.timestamp;
2511
+ timestamp = _a.timestamp,
2512
+ disableGeoip = _a.disableGeoip;
2495
2513
 
2496
2514
  var _capture = function (props) {
2497
2515
  _super.prototype.captureStateless.call(_this, distinctId, event, props, {
2498
- timestamp: timestamp
2516
+ timestamp: timestamp,
2517
+ disableGeoip: disableGeoip
2499
2518
  });
2500
2519
  };
2501
2520
 
2502
2521
  if (sendFeatureFlags) {
2503
- _super.prototype.getFeatureFlagsStateless.call(this, distinctId, groups).then(function (flags) {
2522
+ _super.prototype.getFeatureFlagsStateless.call(this, distinctId, groups, undefined, undefined, disableGeoip).then(function (flags) {
2504
2523
  var featureVariantProperties = {};
2505
2524
 
2506
2525
  if (flags) {
@@ -2536,29 +2555,34 @@ function (_super) {
2536
2555
 
2537
2556
  PostHog.prototype.identify = function (_a) {
2538
2557
  var distinctId = _a.distinctId,
2539
- properties = _a.properties; // Catch properties passed as $set and move them to the top level
2558
+ properties = _a.properties,
2559
+ disableGeoip = _a.disableGeoip; // Catch properties passed as $set and move them to the top level
2540
2560
 
2541
2561
  var personProperties = (properties === null || properties === void 0 ? void 0 : properties.$set) || properties;
2542
2562
 
2543
2563
  _super.prototype.identifyStateless.call(this, distinctId, {
2544
2564
  $set: personProperties
2565
+ }, {
2566
+ disableGeoip: disableGeoip
2545
2567
  });
2546
2568
  };
2547
2569
 
2548
2570
  PostHog.prototype.alias = function (data) {
2549
- _super.prototype.aliasStateless.call(this, data.alias, data.distinctId);
2571
+ _super.prototype.aliasStateless.call(this, data.alias, data.distinctId, undefined, {
2572
+ disableGeoip: data.disableGeoip
2573
+ });
2550
2574
  };
2551
2575
 
2552
2576
  PostHog.prototype.getFeatureFlag = function (key, distinctId, options) {
2553
2577
  var _a;
2554
2578
 
2555
2579
  return __awaiter(this, void 0, void 0, function () {
2556
- var _b, groups, personProperties, groupProperties, _c, onlyEvaluateLocally, sendFeatureFlagEvents, response, flagWasLocallyEvaluated, featureFlagReportedKey;
2580
+ var _b, groups, personProperties, groupProperties, disableGeoip, _c, onlyEvaluateLocally, sendFeatureFlagEvents, response, flagWasLocallyEvaluated, featureFlagReportedKey;
2557
2581
 
2558
2582
  return __generator(this, function (_d) {
2559
2583
  switch (_d.label) {
2560
2584
  case 0:
2561
- _b = options || {}, groups = _b.groups, personProperties = _b.personProperties, groupProperties = _b.groupProperties;
2585
+ _b = options || {}, groups = _b.groups, personProperties = _b.personProperties, groupProperties = _b.groupProperties, disableGeoip = _b.disableGeoip;
2562
2586
  _c = options || {}, onlyEvaluateLocally = _c.onlyEvaluateLocally, sendFeatureFlagEvents = _c.sendFeatureFlagEvents; // set defaults
2563
2587
 
2564
2588
  if (onlyEvaluateLocally == undefined) {
@@ -2581,7 +2605,7 @@ function (_super) {
2581
2605
  , 3];
2582
2606
  return [4
2583
2607
  /*yield*/
2584
- , _super.prototype.getFeatureFlagStateless.call(this, key, distinctId, groups, personProperties, groupProperties)];
2608
+ , _super.prototype.getFeatureFlagStateless.call(this, key, distinctId, groups, personProperties, groupProperties, disableGeoip)];
2585
2609
 
2586
2610
  case 2:
2587
2611
  response = _d.sent();
@@ -2609,7 +2633,8 @@ function (_super) {
2609
2633
  $feature_flag_response: response,
2610
2634
  locally_evaluated: flagWasLocallyEvaluated
2611
2635
  },
2612
- groups: groups
2636
+ groups: groups,
2637
+ disableGeoip: disableGeoip
2613
2638
  });
2614
2639
  }
2615
2640
 
@@ -2625,12 +2650,12 @@ function (_super) {
2625
2650
  var _a;
2626
2651
 
2627
2652
  return __awaiter(this, void 0, void 0, function () {
2628
- var _b, groups, personProperties, groupProperties, _c, onlyEvaluateLocally, response, payloadWasLocallyEvaluated;
2653
+ var _b, groups, personProperties, groupProperties, disableGeoip, _c, onlyEvaluateLocally, response, payloadWasLocallyEvaluated;
2629
2654
 
2630
2655
  return __generator(this, function (_d) {
2631
2656
  switch (_d.label) {
2632
2657
  case 0:
2633
- _b = options || {}, groups = _b.groups, personProperties = _b.personProperties, groupProperties = _b.groupProperties;
2658
+ _b = options || {}, groups = _b.groups, personProperties = _b.personProperties, groupProperties = _b.groupProperties, disableGeoip = _b.disableGeoip;
2634
2659
  _c = options || {}, onlyEvaluateLocally = _c.onlyEvaluateLocally, _c.sendFeatureFlagEvents;
2635
2660
  response = undefined;
2636
2661
  if (!!matchValue) return [3
@@ -2675,7 +2700,7 @@ function (_super) {
2675
2700
  , 6];
2676
2701
  return [4
2677
2702
  /*yield*/
2678
- , _super.prototype.getFeatureFlagPayloadStateless.call(this, key, distinctId, groups, personProperties, groupProperties)];
2703
+ , _super.prototype.getFeatureFlagPayloadStateless.call(this, key, distinctId, groups, personProperties, groupProperties, disableGeoip)];
2679
2704
 
2680
2705
  case 5:
2681
2706
  response = _d.sent();
@@ -2751,12 +2776,12 @@ function (_super) {
2751
2776
  var _a;
2752
2777
 
2753
2778
  return __awaiter(this, void 0, void 0, function () {
2754
- var _b, groups, personProperties, groupProperties, onlyEvaluateLocally, localEvaluationResult, featureFlags, featureFlagPayloads, fallbackToDecide, remoteEvaluationResult;
2779
+ var _b, groups, personProperties, groupProperties, disableGeoip, onlyEvaluateLocally, localEvaluationResult, featureFlags, featureFlagPayloads, fallbackToDecide, remoteEvaluationResult;
2755
2780
 
2756
2781
  return __generator(this, function (_c) {
2757
2782
  switch (_c.label) {
2758
2783
  case 0:
2759
- _b = options || {}, groups = _b.groups, personProperties = _b.personProperties, groupProperties = _b.groupProperties;
2784
+ _b = options || {}, groups = _b.groups, personProperties = _b.personProperties, groupProperties = _b.groupProperties, disableGeoip = _b.disableGeoip;
2760
2785
  onlyEvaluateLocally = (options || {}).onlyEvaluateLocally; // set defaults
2761
2786
 
2762
2787
  if (onlyEvaluateLocally == undefined) {
@@ -2784,7 +2809,7 @@ function (_super) {
2784
2809
  , 3];
2785
2810
  return [4
2786
2811
  /*yield*/
2787
- , _super.prototype.getFeatureFlagsAndPayloadsStateless.call(this, distinctId, groups, personProperties, groupProperties)];
2812
+ , _super.prototype.getFeatureFlagsAndPayloadsStateless.call(this, distinctId, groups, personProperties, groupProperties, disableGeoip)];
2788
2813
 
2789
2814
  case 2:
2790
2815
  remoteEvaluationResult = _c.sent();
@@ -2808,9 +2833,12 @@ function (_super) {
2808
2833
  var groupType = _a.groupType,
2809
2834
  groupKey = _a.groupKey,
2810
2835
  properties = _a.properties,
2811
- distinctId = _a.distinctId;
2836
+ distinctId = _a.distinctId,
2837
+ disableGeoip = _a.disableGeoip;
2812
2838
 
2813
- _super.prototype.groupIdentifyStateless.call(this, groupType, groupKey, properties, undefined, distinctId);
2839
+ _super.prototype.groupIdentifyStateless.call(this, groupType, groupKey, properties, {
2840
+ disableGeoip: disableGeoip
2841
+ }, distinctId);
2814
2842
  };
2815
2843
 
2816
2844
  PostHog.prototype.reloadFeatureFlags = function () {