posthog-js 1.34.1 → 1.36.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/es.js CHANGED
@@ -917,7 +917,7 @@ var LZString = {
917
917
  }
918
918
  };
919
919
 
920
- var version = "1.34.1";
920
+ var version = "1.36.0";
921
921
 
922
922
  // e.g. Config.DEBUG = Config.DEBUG || instance.get_config('debug')
923
923
 
@@ -5982,10 +5982,11 @@ var SentryIntegration = /*#__PURE__*/_createClass(function SentryIntegration(_po
5982
5982
 
5983
5983
  if (event.level !== 'error' || !_posthog.__loaded) return event;
5984
5984
  if (!event.tags) event.tags = {};
5985
- event.tags['PostHog Person URL'] = _posthog.config.api_host + '/person/' + _posthog.get_distinct_id();
5985
+ var host = _posthog.config.ui_host || _posthog.config.api_host;
5986
+ event.tags['PostHog Person URL'] = host + '/person/' + _posthog.get_distinct_id();
5986
5987
 
5987
5988
  if (_posthog.sessionRecordingStarted()) {
5988
- event.tags['PostHog Recording URL'] = _posthog.config.api_host + '/recordings/#sessionRecordingId=' + _posthog.sessionManager.checkAndGetSessionAndWindowId(true).sessionId;
5989
+ event.tags['PostHog Recording URL'] = host + '/recordings/#sessionRecordingId=' + _posthog.sessionManager.checkAndGetSessionAndWindowId(true).sessionId;
5989
5990
  }
5990
5991
 
5991
5992
  var exceptions = ((_event$exception = event.exception) === null || _event$exception === void 0 ? void 0 : _event$exception.values) || [];
@@ -6005,6 +6006,70 @@ var SentryIntegration = /*#__PURE__*/_createClass(function SentryIntegration(_po
6005
6006
  };
6006
6007
  });
6007
6008
 
6009
+ /**
6010
+ * Extend Segment with extra PostHog JS functionality. Required for things like Recordings and feature flags to work correctly.
6011
+ *
6012
+ * ### Usage
6013
+ *
6014
+ * ```js
6015
+ * // After your standard segment anyalytics install
6016
+ * analytics.load("GOEDfA21zZTtR7clsBuDvmBKAtAdZ6Np");
6017
+ *
6018
+ * analytics.ready(() => {
6019
+ * posthog.init('<posthog-api-key>', {
6020
+ * capture_pageview: false,
6021
+ * segment: window.analytics, // NOTE: Be sure to use window.analytics here!
6022
+ * });
6023
+ * window.analytics.page();
6024
+ * })
6025
+ * ```
6026
+ */
6027
+ // Loosely based on https://github.com/segmentio/analytics-next/blob/master/packages/core/src/plugins/index.ts
6028
+ var createSegmentIntegration = function createSegmentIntegration(posthog) {
6029
+ var enrichEvent = function enrichEvent(ctx, eventName) {
6030
+ if (!ctx.event.userId && ctx.event.anonymousId !== posthog.get_distinct_id()) {
6031
+ // This is our only way of detecting that segment's analytics.reset() has been called so we also call it
6032
+ posthog.reset();
6033
+ }
6034
+
6035
+ if (ctx.event.userId && ctx.event.userId !== posthog.get_distinct_id()) {
6036
+ posthog.register({
6037
+ distinct_id: ctx.event.userId
6038
+ });
6039
+ posthog.reloadFeatureFlags();
6040
+ }
6041
+
6042
+ var additionalProperties = posthog._calculate_event_properties(eventName, ctx.event.properties);
6043
+
6044
+ ctx.event.properties = Object.assign({}, additionalProperties, ctx.event.properties);
6045
+ return ctx;
6046
+ };
6047
+
6048
+ return {
6049
+ name: 'PostHog JS',
6050
+ type: 'enrichment',
6051
+ version: '1.0.0',
6052
+ isLoaded: function isLoaded() {
6053
+ return true;
6054
+ },
6055
+ load: function load() {
6056
+ return Promise.resolve();
6057
+ },
6058
+ track: function track(ctx) {
6059
+ return enrichEvent(ctx, ctx.event.event);
6060
+ },
6061
+ page: function page(ctx) {
6062
+ return enrichEvent(ctx, '$pageview');
6063
+ },
6064
+ identify: function identify(ctx) {
6065
+ return enrichEvent(ctx, '$identify');
6066
+ },
6067
+ screen: function screen(ctx) {
6068
+ return enrichEvent(ctx, '$screen');
6069
+ }
6070
+ };
6071
+ };
6072
+
6008
6073
  /*
6009
6074
  SIMPLE STYLE GUIDE:
6010
6075
 
@@ -6049,6 +6114,7 @@ var defaultConfig = function defaultConfig() {
6049
6114
  api_host: 'https://app.posthog.com',
6050
6115
  api_method: 'POST',
6051
6116
  api_transport: 'XHR',
6117
+ ui_host: null,
6052
6118
  token: '',
6053
6119
  autocapture: true,
6054
6120
  rageclick: false,
@@ -6194,12 +6260,19 @@ var create_mplib = function create_mplib(token, config, name) {
6194
6260
 
6195
6261
  var PostHog = /*#__PURE__*/function () {
6196
6262
  function PostHog() {
6263
+ var _this = this;
6264
+
6197
6265
  _classCallCheck(this, PostHog);
6198
6266
 
6199
6267
  this.config = defaultConfig();
6200
6268
  this.compression = {};
6201
6269
  this.decideEndpointWasHit = false;
6202
6270
  this.SentryIntegration = SentryIntegration;
6271
+
6272
+ this.segmentIntegration = function () {
6273
+ return createSegmentIntegration(_this);
6274
+ };
6275
+
6203
6276
  this.__captureHooks = [];
6204
6277
  this.__request_queue = [];
6205
6278
  this.__loaded = false;
@@ -6293,6 +6366,22 @@ var PostHog = /*#__PURE__*/function () {
6293
6366
 
6294
6367
  this._gdpr_init();
6295
6368
 
6369
+ if (config.segment) {
6370
+ // Use segments anonymousId instead
6371
+ this.config.get_device_id = function () {
6372
+ return config.segment.user().anonymousId();
6373
+ }; // If a segment user ID exists, set it as the distinct_id
6374
+
6375
+
6376
+ if (config.segment.user().id()) {
6377
+ this.register({
6378
+ distinct_id: config.segment.user().id()
6379
+ });
6380
+ }
6381
+
6382
+ config.segment.register(this.segmentIntegration());
6383
+ }
6384
+
6296
6385
  if (((_config$bootstrap = config.bootstrap) === null || _config$bootstrap === void 0 ? void 0 : _config$bootstrap.distinctID) !== undefined) {
6297
6386
  var _config$bootstrap2;
6298
6387
 
@@ -6383,11 +6472,11 @@ var PostHog = /*#__PURE__*/function () {
6383
6472
  }, {
6384
6473
  key: "_dom_loaded",
6385
6474
  value: function _dom_loaded() {
6386
- var _this = this;
6475
+ var _this2 = this;
6387
6476
 
6388
6477
  if (!this.has_opted_out_capturing()) {
6389
6478
  _eachArray(this.__request_queue, function (item) {
6390
- _this._send_request.apply(_this, _toConsumableArray(item));
6479
+ _this2._send_request.apply(_this2, _toConsumableArray(item));
6391
6480
  });
6392
6481
  }
6393
6482
 
@@ -6564,7 +6653,7 @@ var PostHog = /*#__PURE__*/function () {
6564
6653
  }, {
6565
6654
  key: "_execute_array",
6566
6655
  value: function _execute_array(array) {
6567
- var _this2 = this;
6656
+ var _this3 = this;
6568
6657
 
6569
6658
  var fn_name;
6570
6659
  var alias_calls = [];
@@ -6578,10 +6667,10 @@ var PostHog = /*#__PURE__*/function () {
6578
6667
  if (_isArray(fn_name)) {
6579
6668
  capturing_calls.push(item); // chained call e.g. posthog.get_group().set()
6580
6669
  } else if (typeof item === 'function') {
6581
- item.call(_this2);
6670
+ item.call(_this3);
6582
6671
  } else if (_isArray(item) && fn_name === 'alias') {
6583
6672
  alias_calls.push(item);
6584
- } else if (_isArray(item) && fn_name.indexOf('capture') !== -1 && typeof _this2[fn_name] === 'function') {
6673
+ } else if (_isArray(item) && fn_name.indexOf('capture') !== -1 && typeof _this3[fn_name] === 'function') {
6585
6674
  capturing_calls.push(item);
6586
6675
  } else {
6587
6676
  other_calls.push(item);
@@ -6690,9 +6779,8 @@ var PostHog = /*#__PURE__*/function () {
6690
6779
 
6691
6780
  if (_isBlockedUA(userAgent)) {
6692
6781
  return;
6693
- }
6782
+ } // update persistence
6694
6783
 
6695
- var start_timestamp = this.persistence.remove_event_timer(event_name); // update persistence
6696
6784
 
6697
6785
  this.persistence.update_search_keyword(document$1.referrer);
6698
6786
 
@@ -6706,7 +6794,7 @@ var PostHog = /*#__PURE__*/function () {
6706
6794
 
6707
6795
  var data = {
6708
6796
  event: event_name,
6709
- properties: this._calculate_event_properties(event_name, properties || {}, start_timestamp)
6797
+ properties: this._calculate_event_properties(event_name, properties || {})
6710
6798
  };
6711
6799
 
6712
6800
  if (event_name === '$identify' && options.$set) {
@@ -6750,8 +6838,10 @@ var PostHog = /*#__PURE__*/function () {
6750
6838
  }
6751
6839
  }, {
6752
6840
  key: "_calculate_event_properties",
6753
- value: function _calculate_event_properties(event_name, event_properties, start_timestamp) {
6841
+ value: function _calculate_event_properties(event_name, event_properties) {
6754
6842
  // set defaults
6843
+ var start_timestamp = this.persistence.remove_event_timer(event_name);
6844
+
6755
6845
  var properties = _objectSpread2({}, event_properties);
6756
6846
 
6757
6847
  properties['token'] = this.get_config('token');
@@ -7165,12 +7255,17 @@ var PostHog = /*#__PURE__*/function () {
7165
7255
  * The default config is:
7166
7256
  *
7167
7257
  * {
7168
- * // Posthog host
7258
+ * // PostHog API host
7169
7259
  * api_host: 'https://app.posthog.com',
7170
7260
  *
7171
7261
  * // HTTP method for capturing requests
7172
7262
  * api_method: 'POST'
7173
7263
  *
7264
+ * // PostHog web app host, currently only used by the Sentry integration.
7265
+ * // This will only be different from api_host when using a reverse-proxied API host – in that case
7266
+ * // the original web app host needs to be passed here so that links to the web app are still convenient.
7267
+ * ui_host: 'https://app.posthog.com',
7268
+ *
7174
7269
  * // Automatically capture clicks, form submissions and change events
7175
7270
  * autocapture: true
7176
7271
  *