posthog-js 1.30.0 → 1.31.1

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
@@ -881,7 +881,7 @@ var LZString = {
881
881
  }
882
882
  };
883
883
 
884
- var version = "1.30.0";
884
+ var version = "1.31.1";
885
885
 
886
886
  // e.g. Config.DEBUG = Config.DEBUG || instance.get_config('debug')
887
887
 
@@ -3377,6 +3377,7 @@ var ALIAS_ID_KEY = '__alias';
3377
3377
  var CAMPAIGN_IDS_KEY = '__cmpns';
3378
3378
  var EVENT_TIMERS_KEY = '__timers';
3379
3379
  var SESSION_RECORDING_ENABLED_SERVER_SIDE = '$session_recording_enabled_server_side';
3380
+ var CONSOLE_LOG_RECORDING_ENABLED_SERVER_SIDE = '$console_log_recording_enabled_server_side';
3380
3381
  var SESSION_ID = '$sesid';
3381
3382
  var ENABLED_FEATURE_FLAGS = '$enabled_feature_flags';
3382
3383
  var RESERVED_PROPERTIES = [SET_QUEUE_KEY, SET_ONCE_QUEUE_KEY, UNSET_QUEUE_KEY, ADD_QUEUE_KEY, APPEND_QUEUE_KEY, REMOVE_QUEUE_KEY, UNION_QUEUE_KEY, PEOPLE_DISTINCT_ID_KEY, ALIAS_ID_KEY, CAMPAIGN_IDS_KEY, EVENT_TIMERS_KEY, SESSION_RECORDING_ENABLED_SERVER_SIDE, SESSION_ID, ENABLED_FEATURE_FLAGS];
@@ -3790,21 +3791,30 @@ var SessionRecording = /*#__PURE__*/function () {
3790
3791
  var enabled_client_side = !this.instance.get_config('disable_session_recording');
3791
3792
  return enabled_server_side && enabled_client_side;
3792
3793
  }
3794
+ }, {
3795
+ key: "isConsoleLogCaptureEnabled",
3796
+ value: function isConsoleLogCaptureEnabled() {
3797
+ var enabled_server_side = !!this.instance.get_property(CONSOLE_LOG_RECORDING_ENABLED_SERVER_SIDE);
3798
+ var enabled_client_side = this.instance.get_config('enable_recording_console_log');
3799
+ return enabled_client_side !== null && enabled_client_side !== void 0 ? enabled_client_side : enabled_server_side;
3800
+ }
3793
3801
  }, {
3794
3802
  key: "afterDecideResponse",
3795
3803
  value: function afterDecideResponse(response) {
3796
- var _response$sessionReco;
3804
+ var _response$sessionReco2;
3797
3805
 
3798
3806
  this.receivedDecide = true;
3799
3807
 
3800
3808
  if (this.instance.persistence) {
3801
- this.instance.persistence.register(_defineProperty({}, SESSION_RECORDING_ENABLED_SERVER_SIDE, !!response['sessionRecording']));
3809
+ var _response$sessionReco, _this$instance$persis;
3810
+
3811
+ this.instance.persistence.register((_this$instance$persis = {}, _defineProperty(_this$instance$persis, SESSION_RECORDING_ENABLED_SERVER_SIDE, !!response['sessionRecording']), _defineProperty(_this$instance$persis, CONSOLE_LOG_RECORDING_ENABLED_SERVER_SIDE, (_response$sessionReco = response.sessionRecording) === null || _response$sessionReco === void 0 ? void 0 : _response$sessionReco.consoleLogRecordingEnabled), _this$instance$persis));
3802
3812
  }
3803
3813
 
3804
- if ((_response$sessionReco = response.sessionRecording) !== null && _response$sessionReco !== void 0 && _response$sessionReco.endpoint) {
3805
- var _response$sessionReco2;
3814
+ if ((_response$sessionReco2 = response.sessionRecording) !== null && _response$sessionReco2 !== void 0 && _response$sessionReco2.endpoint) {
3815
+ var _response$sessionReco3;
3806
3816
 
3807
- this.endpoint = (_response$sessionReco2 = response.sessionRecording) === null || _response$sessionReco2 === void 0 ? void 0 : _response$sessionReco2.endpoint;
3817
+ this.endpoint = (_response$sessionReco3 = response.sessionRecording) === null || _response$sessionReco3 === void 0 ? void 0 : _response$sessionReco3.endpoint;
3808
3818
  }
3809
3819
 
3810
3820
  this.startRecordingIfEnabled();
@@ -3930,7 +3940,7 @@ var SessionRecording = /*#__PURE__*/function () {
3930
3940
  _this2.snapshots.push(properties);
3931
3941
  }
3932
3942
  },
3933
- plugins: window.rrwebConsoleRecord && this.instance.get_config('enable_recording_console_log') ? [window.rrwebConsoleRecord.getRecordConsolePlugin()] : []
3943
+ plugins: window.rrwebConsoleRecord && this.isConsoleLogCaptureEnabled() ? [window.rrwebConsoleRecord.getRecordConsolePlugin()] : []
3934
3944
  }, sessionRecordingOptions)); // :TRICKY: rrweb does not capture navigation within SPA-s, so hook into our $pageview events to get access to all events.
3935
3945
  // Dropping the initial event is fine (it's always captured by rrweb).
3936
3946
 
@@ -5545,12 +5555,28 @@ var SessionIdManager = /*#__PURE__*/function () {
5545
5555
  this._sessionId = undefined;
5546
5556
  this._sessionStartTimestamp = null;
5547
5557
  this._sessionActivityTimestamp = null;
5558
+ var persistenceName = config['persistence_name'] || config['token'];
5559
+ this.window_id_storage_key = 'ph_' + persistenceName + '_window_id';
5560
+ this.primary_window_exists_storage_key = 'ph_' + persistenceName + '_primary_window_exists'; // primary_window_exists is set when the DOM has been loaded and is cleared on unload
5561
+ // if it exists here it means there was no unload which suggests this window is opened as a tab duplication, window.open, etc.
5562
+
5563
+ if (!this.persistence.disabled && sessionStore.is_supported()) {
5564
+ var lastWindowId = sessionStore.parse(this.window_id_storage_key);
5565
+ var primaryWindowExists = sessionStore.parse(this.primary_window_exists_storage_key);
5566
+
5567
+ if (lastWindowId && !primaryWindowExists) {
5568
+ // Persist window from previous storage state
5569
+ this._windowId = lastWindowId;
5570
+ } else {
5571
+ // Wipe any reference to previous window id
5572
+ sessionStore.remove(this.window_id_storage_key);
5573
+ } // Flag this session as having a primary window
5548
5574
 
5549
- if (config['persistence_name']) {
5550
- this.window_id_storage_key = 'ph_' + config['persistence_name'] + '_window_id';
5551
- } else {
5552
- this.window_id_storage_key = 'ph_' + config['token'] + '_window_id';
5575
+
5576
+ sessionStore.set(this.primary_window_exists_storage_key, true);
5553
5577
  }
5578
+
5579
+ this._listenToReloadWindow();
5554
5580
  } // Note: this tries to store the windowId in sessionStorage. SessionStorage is unique to the current window/tab,
5555
5581
  // and persists page loads/reloads. So it's uniquely suited for storing the windowId. This function also respects
5556
5582
  // when persistence is disabled (by user config) and when sessionStorage is not supported (it *should* be supported on all browsers),
@@ -5577,7 +5603,8 @@ var SessionIdManager = /*#__PURE__*/function () {
5577
5603
 
5578
5604
  if (!this.persistence.disabled && sessionStore.is_supported()) {
5579
5605
  return sessionStore.parse(this.window_id_storage_key);
5580
- }
5606
+ } // New window id will be generated
5607
+
5581
5608
 
5582
5609
  return null;
5583
5610
  } // Note: 'this.persistence.register' can be disabled in the config.
@@ -5616,6 +5643,24 @@ var SessionIdManager = /*#__PURE__*/function () {
5616
5643
  value: function resetSessionId() {
5617
5644
  this._setSessionId(null, null, null);
5618
5645
  }
5646
+ /*
5647
+ * Listens to window unloads and removes the primaryWindowExists key from sessionStorage.
5648
+ * Reloaded or fresh tabs created after a DOM unloads (reloading the same tab) WILL NOT have this primaryWindowExists flag in session storage.
5649
+ * Cloned sessions (new tab, tab duplication, window.open(), ...) WILL have this primaryWindowExists flag in their copied session storage.
5650
+ * We conditionally check the primaryWindowExists value in the constructor to decide if the window id in the last session storage should be carried over.
5651
+ */
5652
+
5653
+ }, {
5654
+ key: "_listenToReloadWindow",
5655
+ value: function _listenToReloadWindow() {
5656
+ var _this = this;
5657
+
5658
+ window.addEventListener('beforeunload', function () {
5659
+ if (!_this.persistence.disabled && sessionStore.is_supported()) {
5660
+ sessionStore.remove(_this.primary_window_exists_storage_key);
5661
+ }
5662
+ });
5663
+ }
5619
5664
  /*
5620
5665
  * This function returns the current sessionId and windowId. It should be used to
5621
5666
  * access these values over directly calling `._sessionId` or `._windowId`. In addition
@@ -5826,6 +5871,8 @@ var SentryIntegration = /*#__PURE__*/_createClass(function SentryIntegration(_po
5826
5871
 
5827
5872
  this.setupOnce = function (addGlobalEventProcessor) {
5828
5873
  addGlobalEventProcessor(function (event) {
5874
+ var _event$exception, _exceptions$, _exceptions$2;
5875
+
5829
5876
  if (event.level !== 'error' || !_posthog.__loaded) return event;
5830
5877
  if (!event.tags) event.tags = {};
5831
5878
  event.tags['PostHog Person URL'] = _posthog.config.api_host + '/person/' + _posthog.get_distinct_id();
@@ -5834,9 +5881,13 @@ var SentryIntegration = /*#__PURE__*/_createClass(function SentryIntegration(_po
5834
5881
  event.tags['PostHog Recording URL'] = _posthog.config.api_host + '/recordings/#sessionRecordingId=' + _posthog.sessionManager.checkAndGetSessionAndWindowId(true).sessionId;
5835
5882
  }
5836
5883
 
5884
+ var exceptions = ((_event$exception = event.exception) === null || _event$exception === void 0 ? void 0 : _event$exception.values) || [];
5837
5885
  var data = {
5838
5886
  $sentry_event_id: event.event_id,
5839
- $sentry_exception: event.exception
5887
+ $sentry_exception: event.exception,
5888
+ $sentry_exception_message: (_exceptions$ = exceptions[0]) === null || _exceptions$ === void 0 ? void 0 : _exceptions$.value,
5889
+ $sentry_exception_type: (_exceptions$2 = exceptions[0]) === null || _exceptions$2 === void 0 ? void 0 : _exceptions$2.type,
5890
+ $sentry_tags: event.tags
5840
5891
  };
5841
5892
  if (organization && projectId) data['$sentry_url'] = (prefix || 'https://sentry.io/organizations/') + organization + '/issues/?project=' + projectId + '&query=' + event.event_id;
5842
5893
 
@@ -5911,7 +5962,8 @@ var defaultConfig = function defaultConfig() {
5911
5962
  disable_session_recording: false,
5912
5963
  disable_persistence: false,
5913
5964
  disable_cookie: false,
5914
- enable_recording_console_log: false,
5965
+ enable_recording_console_log: undefined,
5966
+ // When undefined, it falls back to the server-side setting
5915
5967
  secure_cookie: (win === null || win === void 0 ? void 0 : (_window$location = win.location) === null || _window$location === void 0 ? void 0 : _window$location.protocol) === 'https:',
5916
5968
  ip: true,
5917
5969
  opt_out_capturing_by_default: false,