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/module.d.ts CHANGED
@@ -139,6 +139,7 @@ interface PostHogConfig {
139
139
  api_host: string;
140
140
  api_method: string;
141
141
  api_transport: string;
142
+ ui_host: string | null;
142
143
  token: string;
143
144
  autocapture: boolean;
144
145
  rageclick: boolean;
@@ -195,6 +196,7 @@ interface PostHogConfig {
195
196
  isIdentifiedID?: boolean;
196
197
  featureFlags?: Record<string, boolean | string>;
197
198
  };
199
+ segment?: any;
198
200
  }
199
201
  interface OptInOutCapturingOptions {
200
202
  capture: (event: string, properties: Properties, options: CaptureOptions) => void;
@@ -909,6 +911,7 @@ declare class PostHog {
909
911
  __autocapture_enabled: boolean | undefined;
910
912
  decideEndpointWasHit: boolean;
911
913
  SentryIntegration: typeof SentryIntegration;
914
+ segmentIntegration: () => any;
912
915
  constructor();
913
916
  /**
914
917
  * This function initializes a new instance of the PostHog capturing object.
@@ -993,7 +996,7 @@ declare class PostHog {
993
996
  capture(event_name: string, properties?: Properties | null, options?: CaptureOptions): CaptureResult | void;
994
997
  _addCaptureHook(callback: (eventName: string) => void): void;
995
998
  _invokeCaptureHooks(eventName: string, eventData: CaptureResult): void;
996
- _calculate_event_properties(event_name: string, event_properties: Properties, start_timestamp: number): Properties;
999
+ _calculate_event_properties(event_name: string, event_properties: Properties): Properties;
997
1000
  /**
998
1001
  * Register a set of super properties, which are included with all
999
1002
  * events. This will overwrite previous super property values.
@@ -1146,12 +1149,17 @@ declare class PostHog {
1146
1149
  * The default config is:
1147
1150
  *
1148
1151
  * {
1149
- * // Posthog host
1152
+ * // PostHog API host
1150
1153
  * api_host: 'https://app.posthog.com',
1151
1154
  *
1152
1155
  * // HTTP method for capturing requests
1153
1156
  * api_method: 'POST'
1154
1157
  *
1158
+ * // PostHog web app host, currently only used by the Sentry integration.
1159
+ * // This will only be different from api_host when using a reverse-proxied API host – in that case
1160
+ * // the original web app host needs to be passed here so that links to the web app are still convenient.
1161
+ * ui_host: 'https://app.posthog.com',
1162
+ *
1155
1163
  * // Automatically capture clicks, form submissions and change events
1156
1164
  * autocapture: true
1157
1165
  *
package/dist/module.js CHANGED
@@ -921,7 +921,7 @@ var LZString = {
921
921
  }
922
922
  };
923
923
 
924
- var version = "1.34.1";
924
+ var version = "1.36.0";
925
925
 
926
926
  // e.g. Config.DEBUG = Config.DEBUG || instance.get_config('debug')
927
927
 
@@ -5986,10 +5986,11 @@ var SentryIntegration = /*#__PURE__*/_createClass(function SentryIntegration(_po
5986
5986
 
5987
5987
  if (event.level !== 'error' || !_posthog.__loaded) return event;
5988
5988
  if (!event.tags) event.tags = {};
5989
- event.tags['PostHog Person URL'] = _posthog.config.api_host + '/person/' + _posthog.get_distinct_id();
5989
+ var host = _posthog.config.ui_host || _posthog.config.api_host;
5990
+ event.tags['PostHog Person URL'] = host + '/person/' + _posthog.get_distinct_id();
5990
5991
 
5991
5992
  if (_posthog.sessionRecordingStarted()) {
5992
- event.tags['PostHog Recording URL'] = _posthog.config.api_host + '/recordings/#sessionRecordingId=' + _posthog.sessionManager.checkAndGetSessionAndWindowId(true).sessionId;
5993
+ event.tags['PostHog Recording URL'] = host + '/recordings/#sessionRecordingId=' + _posthog.sessionManager.checkAndGetSessionAndWindowId(true).sessionId;
5993
5994
  }
5994
5995
 
5995
5996
  var exceptions = ((_event$exception = event.exception) === null || _event$exception === void 0 ? void 0 : _event$exception.values) || [];
@@ -6009,6 +6010,70 @@ var SentryIntegration = /*#__PURE__*/_createClass(function SentryIntegration(_po
6009
6010
  };
6010
6011
  });
6011
6012
 
6013
+ /**
6014
+ * Extend Segment with extra PostHog JS functionality. Required for things like Recordings and feature flags to work correctly.
6015
+ *
6016
+ * ### Usage
6017
+ *
6018
+ * ```js
6019
+ * // After your standard segment anyalytics install
6020
+ * analytics.load("GOEDfA21zZTtR7clsBuDvmBKAtAdZ6Np");
6021
+ *
6022
+ * analytics.ready(() => {
6023
+ * posthog.init('<posthog-api-key>', {
6024
+ * capture_pageview: false,
6025
+ * segment: window.analytics, // NOTE: Be sure to use window.analytics here!
6026
+ * });
6027
+ * window.analytics.page();
6028
+ * })
6029
+ * ```
6030
+ */
6031
+ // Loosely based on https://github.com/segmentio/analytics-next/blob/master/packages/core/src/plugins/index.ts
6032
+ var createSegmentIntegration = function createSegmentIntegration(posthog) {
6033
+ var enrichEvent = function enrichEvent(ctx, eventName) {
6034
+ if (!ctx.event.userId && ctx.event.anonymousId !== posthog.get_distinct_id()) {
6035
+ // This is our only way of detecting that segment's analytics.reset() has been called so we also call it
6036
+ posthog.reset();
6037
+ }
6038
+
6039
+ if (ctx.event.userId && ctx.event.userId !== posthog.get_distinct_id()) {
6040
+ posthog.register({
6041
+ distinct_id: ctx.event.userId
6042
+ });
6043
+ posthog.reloadFeatureFlags();
6044
+ }
6045
+
6046
+ var additionalProperties = posthog._calculate_event_properties(eventName, ctx.event.properties);
6047
+
6048
+ ctx.event.properties = Object.assign({}, additionalProperties, ctx.event.properties);
6049
+ return ctx;
6050
+ };
6051
+
6052
+ return {
6053
+ name: 'PostHog JS',
6054
+ type: 'enrichment',
6055
+ version: '1.0.0',
6056
+ isLoaded: function isLoaded() {
6057
+ return true;
6058
+ },
6059
+ load: function load() {
6060
+ return Promise.resolve();
6061
+ },
6062
+ track: function track(ctx) {
6063
+ return enrichEvent(ctx, ctx.event.event);
6064
+ },
6065
+ page: function page(ctx) {
6066
+ return enrichEvent(ctx, '$pageview');
6067
+ },
6068
+ identify: function identify(ctx) {
6069
+ return enrichEvent(ctx, '$identify');
6070
+ },
6071
+ screen: function screen(ctx) {
6072
+ return enrichEvent(ctx, '$screen');
6073
+ }
6074
+ };
6075
+ };
6076
+
6012
6077
  /*
6013
6078
  SIMPLE STYLE GUIDE:
6014
6079
 
@@ -6053,6 +6118,7 @@ var defaultConfig = function defaultConfig() {
6053
6118
  api_host: 'https://app.posthog.com',
6054
6119
  api_method: 'POST',
6055
6120
  api_transport: 'XHR',
6121
+ ui_host: null,
6056
6122
  token: '',
6057
6123
  autocapture: true,
6058
6124
  rageclick: false,
@@ -6198,12 +6264,19 @@ var create_mplib = function create_mplib(token, config, name) {
6198
6264
 
6199
6265
  var PostHog = /*#__PURE__*/function () {
6200
6266
  function PostHog() {
6267
+ var _this = this;
6268
+
6201
6269
  _classCallCheck(this, PostHog);
6202
6270
 
6203
6271
  this.config = defaultConfig();
6204
6272
  this.compression = {};
6205
6273
  this.decideEndpointWasHit = false;
6206
6274
  this.SentryIntegration = SentryIntegration;
6275
+
6276
+ this.segmentIntegration = function () {
6277
+ return createSegmentIntegration(_this);
6278
+ };
6279
+
6207
6280
  this.__captureHooks = [];
6208
6281
  this.__request_queue = [];
6209
6282
  this.__loaded = false;
@@ -6297,6 +6370,22 @@ var PostHog = /*#__PURE__*/function () {
6297
6370
 
6298
6371
  this._gdpr_init();
6299
6372
 
6373
+ if (config.segment) {
6374
+ // Use segments anonymousId instead
6375
+ this.config.get_device_id = function () {
6376
+ return config.segment.user().anonymousId();
6377
+ }; // If a segment user ID exists, set it as the distinct_id
6378
+
6379
+
6380
+ if (config.segment.user().id()) {
6381
+ this.register({
6382
+ distinct_id: config.segment.user().id()
6383
+ });
6384
+ }
6385
+
6386
+ config.segment.register(this.segmentIntegration());
6387
+ }
6388
+
6300
6389
  if (((_config$bootstrap = config.bootstrap) === null || _config$bootstrap === void 0 ? void 0 : _config$bootstrap.distinctID) !== undefined) {
6301
6390
  var _config$bootstrap2;
6302
6391
 
@@ -6387,11 +6476,11 @@ var PostHog = /*#__PURE__*/function () {
6387
6476
  }, {
6388
6477
  key: "_dom_loaded",
6389
6478
  value: function _dom_loaded() {
6390
- var _this = this;
6479
+ var _this2 = this;
6391
6480
 
6392
6481
  if (!this.has_opted_out_capturing()) {
6393
6482
  _eachArray(this.__request_queue, function (item) {
6394
- _this._send_request.apply(_this, _toConsumableArray(item));
6483
+ _this2._send_request.apply(_this2, _toConsumableArray(item));
6395
6484
  });
6396
6485
  }
6397
6486
 
@@ -6568,7 +6657,7 @@ var PostHog = /*#__PURE__*/function () {
6568
6657
  }, {
6569
6658
  key: "_execute_array",
6570
6659
  value: function _execute_array(array) {
6571
- var _this2 = this;
6660
+ var _this3 = this;
6572
6661
 
6573
6662
  var fn_name;
6574
6663
  var alias_calls = [];
@@ -6582,10 +6671,10 @@ var PostHog = /*#__PURE__*/function () {
6582
6671
  if (_isArray(fn_name)) {
6583
6672
  capturing_calls.push(item); // chained call e.g. posthog.get_group().set()
6584
6673
  } else if (typeof item === 'function') {
6585
- item.call(_this2);
6674
+ item.call(_this3);
6586
6675
  } else if (_isArray(item) && fn_name === 'alias') {
6587
6676
  alias_calls.push(item);
6588
- } else if (_isArray(item) && fn_name.indexOf('capture') !== -1 && typeof _this2[fn_name] === 'function') {
6677
+ } else if (_isArray(item) && fn_name.indexOf('capture') !== -1 && typeof _this3[fn_name] === 'function') {
6589
6678
  capturing_calls.push(item);
6590
6679
  } else {
6591
6680
  other_calls.push(item);
@@ -6694,9 +6783,8 @@ var PostHog = /*#__PURE__*/function () {
6694
6783
 
6695
6784
  if (_isBlockedUA(userAgent)) {
6696
6785
  return;
6697
- }
6786
+ } // update persistence
6698
6787
 
6699
- var start_timestamp = this.persistence.remove_event_timer(event_name); // update persistence
6700
6788
 
6701
6789
  this.persistence.update_search_keyword(document$1.referrer);
6702
6790
 
@@ -6710,7 +6798,7 @@ var PostHog = /*#__PURE__*/function () {
6710
6798
 
6711
6799
  var data = {
6712
6800
  event: event_name,
6713
- properties: this._calculate_event_properties(event_name, properties || {}, start_timestamp)
6801
+ properties: this._calculate_event_properties(event_name, properties || {})
6714
6802
  };
6715
6803
 
6716
6804
  if (event_name === '$identify' && options.$set) {
@@ -6754,8 +6842,10 @@ var PostHog = /*#__PURE__*/function () {
6754
6842
  }
6755
6843
  }, {
6756
6844
  key: "_calculate_event_properties",
6757
- value: function _calculate_event_properties(event_name, event_properties, start_timestamp) {
6845
+ value: function _calculate_event_properties(event_name, event_properties) {
6758
6846
  // set defaults
6847
+ var start_timestamp = this.persistence.remove_event_timer(event_name);
6848
+
6759
6849
  var properties = _objectSpread2({}, event_properties);
6760
6850
 
6761
6851
  properties['token'] = this.get_config('token');
@@ -7169,12 +7259,17 @@ var PostHog = /*#__PURE__*/function () {
7169
7259
  * The default config is:
7170
7260
  *
7171
7261
  * {
7172
- * // Posthog host
7262
+ * // PostHog API host
7173
7263
  * api_host: 'https://app.posthog.com',
7174
7264
  *
7175
7265
  * // HTTP method for capturing requests
7176
7266
  * api_method: 'POST'
7177
7267
  *
7268
+ * // PostHog web app host, currently only used by the Sentry integration.
7269
+ * // This will only be different from api_host when using a reverse-proxied API host – in that case
7270
+ * // the original web app host needs to be passed here so that links to the web app are still convenient.
7271
+ * ui_host: 'https://app.posthog.com',
7272
+ *
7178
7273
  * // Automatically capture clicks, form submissions and change events
7179
7274
  * autocapture: true
7180
7275
  *