posthog-js 1.34.0 → 1.35.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/dist/module.d.ts CHANGED
@@ -189,11 +189,13 @@ interface PostHogConfig {
189
189
  _onCapture: (eventName: string, eventData: CaptureResult) => void;
190
190
  _capture_metrics: boolean;
191
191
  _capture_performance: boolean;
192
+ disable_compression: boolean;
192
193
  bootstrap: {
193
194
  distinctID?: string;
194
195
  isIdentifiedID?: boolean;
195
196
  featureFlags?: Record<string, boolean | string>;
196
197
  };
198
+ segment?: any;
197
199
  }
198
200
  interface OptInOutCapturingOptions {
199
201
  capture: (event: string, properties: Properties, options: CaptureOptions) => void;
@@ -908,6 +910,7 @@ declare class PostHog {
908
910
  __autocapture_enabled: boolean | undefined;
909
911
  decideEndpointWasHit: boolean;
910
912
  SentryIntegration: typeof SentryIntegration;
913
+ segmentIntegration: () => any;
911
914
  constructor();
912
915
  /**
913
916
  * This function initializes a new instance of the PostHog capturing object.
@@ -992,7 +995,7 @@ declare class PostHog {
992
995
  capture(event_name: string, properties?: Properties | null, options?: CaptureOptions): CaptureResult | void;
993
996
  _addCaptureHook(callback: (eventName: string) => void): void;
994
997
  _invokeCaptureHooks(eventName: string, eventData: CaptureResult): void;
995
- _calculate_event_properties(event_name: string, event_properties: Properties, start_timestamp: number): Properties;
998
+ _calculate_event_properties(event_name: string, event_properties: Properties): Properties;
996
999
  /**
997
1000
  * Register a set of super properties, which are included with all
998
1001
  * events. This will overwrite previous super property values.
package/dist/module.js CHANGED
@@ -921,7 +921,7 @@ var LZString = {
921
921
  }
922
922
  };
923
923
 
924
- var version = "1.34.0";
924
+ var version = "1.35.0";
925
925
 
926
926
  // e.g. Config.DEBUG = Config.DEBUG || instance.get_config('debug')
927
927
 
@@ -4079,8 +4079,9 @@ var Decide = /*#__PURE__*/function () {
4079
4079
  (_this$instance$sessio = this.instance.sessionRecording) === null || _this$instance$sessio === void 0 ? void 0 : _this$instance$sessio.afterDecideResponse(response);
4080
4080
  autocapture.afterDecideResponse(response, this.instance);
4081
4081
  this.instance.featureFlags.receivedFeatureFlags(response);
4082
+ this.instance['compression'] = {};
4082
4083
 
4083
- if (response['supportedCompression']) {
4084
+ if (response['supportedCompression'] && !this.instance.get_config('disable_compression')) {
4084
4085
  var compression = {};
4085
4086
 
4086
4087
  var _iterator = _createForOfIteratorHelper(response['supportedCompression']),
@@ -4098,8 +4099,6 @@ var Decide = /*#__PURE__*/function () {
4098
4099
  }
4099
4100
 
4100
4101
  this.instance['compression'] = compression;
4101
- } else {
4102
- this.instance['compression'] = {};
4103
4102
  }
4104
4103
 
4105
4104
  if (response['siteApps']) {
@@ -6010,6 +6009,70 @@ var SentryIntegration = /*#__PURE__*/_createClass(function SentryIntegration(_po
6010
6009
  };
6011
6010
  });
6012
6011
 
6012
+ /**
6013
+ * Extend Segment with extra PostHog JS functionality. Required for things like Recordings and feature flags to work correctly.
6014
+ *
6015
+ * ### Usage
6016
+ *
6017
+ * ```js
6018
+ * // After your standard segment anyalytics install
6019
+ * analytics.load("GOEDfA21zZTtR7clsBuDvmBKAtAdZ6Np");
6020
+ *
6021
+ * analytics.ready(() => {
6022
+ * posthog.init('<posthog-api-key>', {
6023
+ * capture_pageview: false,
6024
+ * segment: window.analytics, // NOTE: Be sure to use window.analytics here!
6025
+ * });
6026
+ * window.analytics.page();
6027
+ * })
6028
+ * ```
6029
+ */
6030
+ // Loosely based on https://github.com/segmentio/analytics-next/blob/master/packages/core/src/plugins/index.ts
6031
+ var createSegmentIntegration = function createSegmentIntegration(posthog) {
6032
+ var enrichEvent = function enrichEvent(ctx, eventName) {
6033
+ if (!ctx.event.userId && ctx.event.anonymousId !== posthog.get_distinct_id()) {
6034
+ // This is our only way of detecting that segment's analytics.reset() has been called so we also call it
6035
+ posthog.reset();
6036
+ }
6037
+
6038
+ if (ctx.event.userId && ctx.event.userId !== posthog.get_distinct_id()) {
6039
+ posthog.register({
6040
+ distinct_id: ctx.event.userId
6041
+ });
6042
+ posthog.reloadFeatureFlags();
6043
+ }
6044
+
6045
+ var additionalProperties = posthog._calculate_event_properties(eventName, ctx.event.properties);
6046
+
6047
+ ctx.event.properties = Object.assign({}, additionalProperties, ctx.event.properties);
6048
+ return ctx;
6049
+ };
6050
+
6051
+ return {
6052
+ name: 'PostHog JS',
6053
+ type: 'enrichment',
6054
+ version: '1.0.0',
6055
+ isLoaded: function isLoaded() {
6056
+ return true;
6057
+ },
6058
+ load: function load() {
6059
+ return Promise.resolve();
6060
+ },
6061
+ track: function track(ctx) {
6062
+ return enrichEvent(ctx, ctx.event.event);
6063
+ },
6064
+ page: function page(ctx) {
6065
+ return enrichEvent(ctx, '$pageview');
6066
+ },
6067
+ identify: function identify(ctx) {
6068
+ return enrichEvent(ctx, '$identify');
6069
+ },
6070
+ screen: function screen(ctx) {
6071
+ return enrichEvent(ctx, '$screen');
6072
+ }
6073
+ };
6074
+ };
6075
+
6013
6076
  /*
6014
6077
  SIMPLE STYLE GUIDE:
6015
6078
 
@@ -6122,7 +6185,8 @@ var defaultConfig = function defaultConfig() {
6122
6185
  _capture_performance: false,
6123
6186
  name: 'posthog',
6124
6187
  callback_fn: 'posthog._jsc',
6125
- bootstrap: {}
6188
+ bootstrap: {},
6189
+ disable_compression: false
6126
6190
  };
6127
6191
  };
6128
6192
  /**
@@ -6198,12 +6262,19 @@ var create_mplib = function create_mplib(token, config, name) {
6198
6262
 
6199
6263
  var PostHog = /*#__PURE__*/function () {
6200
6264
  function PostHog() {
6265
+ var _this = this;
6266
+
6201
6267
  _classCallCheck(this, PostHog);
6202
6268
 
6203
6269
  this.config = defaultConfig();
6204
6270
  this.compression = {};
6205
6271
  this.decideEndpointWasHit = false;
6206
6272
  this.SentryIntegration = SentryIntegration;
6273
+
6274
+ this.segmentIntegration = function () {
6275
+ return createSegmentIntegration(_this);
6276
+ };
6277
+
6207
6278
  this.__captureHooks = [];
6208
6279
  this.__request_queue = [];
6209
6280
  this.__loaded = false;
@@ -6297,6 +6368,22 @@ var PostHog = /*#__PURE__*/function () {
6297
6368
 
6298
6369
  this._gdpr_init();
6299
6370
 
6371
+ if (config.segment) {
6372
+ // Use segments anonymousId instead
6373
+ this.config.get_device_id = function () {
6374
+ return config.segment.user().anonymousId();
6375
+ }; // If a segment user ID exists, set it as the distinct_id
6376
+
6377
+
6378
+ if (config.segment.user().id()) {
6379
+ this.register({
6380
+ distinct_id: config.segment.user().id()
6381
+ });
6382
+ }
6383
+
6384
+ config.segment.register(this.segmentIntegration());
6385
+ }
6386
+
6300
6387
  if (((_config$bootstrap = config.bootstrap) === null || _config$bootstrap === void 0 ? void 0 : _config$bootstrap.distinctID) !== undefined) {
6301
6388
  var _config$bootstrap2;
6302
6389
 
@@ -6387,11 +6474,11 @@ var PostHog = /*#__PURE__*/function () {
6387
6474
  }, {
6388
6475
  key: "_dom_loaded",
6389
6476
  value: function _dom_loaded() {
6390
- var _this = this;
6477
+ var _this2 = this;
6391
6478
 
6392
6479
  if (!this.has_opted_out_capturing()) {
6393
6480
  _eachArray(this.__request_queue, function (item) {
6394
- _this._send_request.apply(_this, _toConsumableArray(item));
6481
+ _this2._send_request.apply(_this2, _toConsumableArray(item));
6395
6482
  });
6396
6483
  }
6397
6484
 
@@ -6568,7 +6655,7 @@ var PostHog = /*#__PURE__*/function () {
6568
6655
  }, {
6569
6656
  key: "_execute_array",
6570
6657
  value: function _execute_array(array) {
6571
- var _this2 = this;
6658
+ var _this3 = this;
6572
6659
 
6573
6660
  var fn_name;
6574
6661
  var alias_calls = [];
@@ -6582,10 +6669,10 @@ var PostHog = /*#__PURE__*/function () {
6582
6669
  if (_isArray(fn_name)) {
6583
6670
  capturing_calls.push(item); // chained call e.g. posthog.get_group().set()
6584
6671
  } else if (typeof item === 'function') {
6585
- item.call(_this2);
6672
+ item.call(_this3);
6586
6673
  } else if (_isArray(item) && fn_name === 'alias') {
6587
6674
  alias_calls.push(item);
6588
- } else if (_isArray(item) && fn_name.indexOf('capture') !== -1 && typeof _this2[fn_name] === 'function') {
6675
+ } else if (_isArray(item) && fn_name.indexOf('capture') !== -1 && typeof _this3[fn_name] === 'function') {
6589
6676
  capturing_calls.push(item);
6590
6677
  } else {
6591
6678
  other_calls.push(item);
@@ -6694,9 +6781,8 @@ var PostHog = /*#__PURE__*/function () {
6694
6781
 
6695
6782
  if (_isBlockedUA(userAgent)) {
6696
6783
  return;
6697
- }
6784
+ } // update persistence
6698
6785
 
6699
- var start_timestamp = this.persistence.remove_event_timer(event_name); // update persistence
6700
6786
 
6701
6787
  this.persistence.update_search_keyword(document$1.referrer);
6702
6788
 
@@ -6710,7 +6796,7 @@ var PostHog = /*#__PURE__*/function () {
6710
6796
 
6711
6797
  var data = {
6712
6798
  event: event_name,
6713
- properties: this._calculate_event_properties(event_name, properties || {}, start_timestamp)
6799
+ properties: this._calculate_event_properties(event_name, properties || {})
6714
6800
  };
6715
6801
 
6716
6802
  if (event_name === '$identify' && options.$set) {
@@ -6754,8 +6840,10 @@ var PostHog = /*#__PURE__*/function () {
6754
6840
  }
6755
6841
  }, {
6756
6842
  key: "_calculate_event_properties",
6757
- value: function _calculate_event_properties(event_name, event_properties, start_timestamp) {
6843
+ value: function _calculate_event_properties(event_name, event_properties) {
6758
6844
  // set defaults
6845
+ var start_timestamp = this.persistence.remove_event_timer(event_name);
6846
+
6759
6847
  var properties = _objectSpread2({}, event_properties);
6760
6848
 
6761
6849
  properties['token'] = this.get_config('token');