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/module.d.ts CHANGED
@@ -159,7 +159,7 @@ interface PostHogConfig {
159
159
  disable_session_recording: boolean;
160
160
  disable_persistence: boolean;
161
161
  disable_cookie: boolean;
162
- enable_recording_console_log: boolean;
162
+ enable_recording_console_log?: boolean;
163
163
  secure_cookie: boolean;
164
164
  ip: boolean;
165
165
  opt_out_capturing_by_default: boolean;
@@ -272,6 +272,7 @@ interface DecideResponse {
272
272
  featureFlags: Record<string, string | boolean>;
273
273
  sessionRecording?: {
274
274
  endpoint?: string;
275
+ consoleLogRecordingEnabled?: boolean;
275
276
  };
276
277
  editorParams: EditorParams;
277
278
  toolbarVersion: 'toolbar'; /** deprecated, moved to editorParams */
@@ -781,6 +782,7 @@ declare class SessionRecording {
781
782
  started(): boolean;
782
783
  stopRecording(): void;
783
784
  isRecordingEnabled(): boolean;
785
+ isConsoleLogCaptureEnabled(): boolean;
784
786
  afterDecideResponse(response: DecideResponse): void;
785
787
  startCaptureAndTrySendingQueuedSnapshots(): void;
786
788
  _startCapture(): void;
@@ -818,6 +820,7 @@ declare class SessionIdManager {
818
820
  _windowId: string | null | undefined;
819
821
  _sessionId: string | null | undefined;
820
822
  window_id_storage_key: string;
823
+ primary_window_exists_storage_key: string;
821
824
  _sessionStartTimestamp: number | null;
822
825
  _sessionActivityTimestamp: number | null;
823
826
  constructor(config: Partial<PostHogConfig>, persistence: PostHogPersistence);
@@ -826,6 +829,7 @@ declare class SessionIdManager {
826
829
  _setSessionId(sessionId: string | null, sessionActivityTimestamp: number | null, sessionStartTimestamp: number | null): void;
827
830
  _getSessionId(): [number, string, number];
828
831
  resetSessionId(): void;
832
+ _listenToReloadWindow(): void;
829
833
  checkAndGetSessionAndWindowId(readOnly?: boolean, _timestamp?: number | null): {
830
834
  sessionId: string;
831
835
  windowId: string;
package/dist/module.js CHANGED
@@ -885,7 +885,7 @@ var LZString = {
885
885
  }
886
886
  };
887
887
 
888
- var version = "1.30.0";
888
+ var version = "1.31.1";
889
889
 
890
890
  // e.g. Config.DEBUG = Config.DEBUG || instance.get_config('debug')
891
891
 
@@ -3381,6 +3381,7 @@ var ALIAS_ID_KEY = '__alias';
3381
3381
  var CAMPAIGN_IDS_KEY = '__cmpns';
3382
3382
  var EVENT_TIMERS_KEY = '__timers';
3383
3383
  var SESSION_RECORDING_ENABLED_SERVER_SIDE = '$session_recording_enabled_server_side';
3384
+ var CONSOLE_LOG_RECORDING_ENABLED_SERVER_SIDE = '$console_log_recording_enabled_server_side';
3384
3385
  var SESSION_ID = '$sesid';
3385
3386
  var ENABLED_FEATURE_FLAGS = '$enabled_feature_flags';
3386
3387
  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];
@@ -3794,21 +3795,30 @@ var SessionRecording = /*#__PURE__*/function () {
3794
3795
  var enabled_client_side = !this.instance.get_config('disable_session_recording');
3795
3796
  return enabled_server_side && enabled_client_side;
3796
3797
  }
3798
+ }, {
3799
+ key: "isConsoleLogCaptureEnabled",
3800
+ value: function isConsoleLogCaptureEnabled() {
3801
+ var enabled_server_side = !!this.instance.get_property(CONSOLE_LOG_RECORDING_ENABLED_SERVER_SIDE);
3802
+ var enabled_client_side = this.instance.get_config('enable_recording_console_log');
3803
+ return enabled_client_side !== null && enabled_client_side !== void 0 ? enabled_client_side : enabled_server_side;
3804
+ }
3797
3805
  }, {
3798
3806
  key: "afterDecideResponse",
3799
3807
  value: function afterDecideResponse(response) {
3800
- var _response$sessionReco;
3808
+ var _response$sessionReco2;
3801
3809
 
3802
3810
  this.receivedDecide = true;
3803
3811
 
3804
3812
  if (this.instance.persistence) {
3805
- this.instance.persistence.register(_defineProperty({}, SESSION_RECORDING_ENABLED_SERVER_SIDE, !!response['sessionRecording']));
3813
+ var _response$sessionReco, _this$instance$persis;
3814
+
3815
+ 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));
3806
3816
  }
3807
3817
 
3808
- if ((_response$sessionReco = response.sessionRecording) !== null && _response$sessionReco !== void 0 && _response$sessionReco.endpoint) {
3809
- var _response$sessionReco2;
3818
+ if ((_response$sessionReco2 = response.sessionRecording) !== null && _response$sessionReco2 !== void 0 && _response$sessionReco2.endpoint) {
3819
+ var _response$sessionReco3;
3810
3820
 
3811
- this.endpoint = (_response$sessionReco2 = response.sessionRecording) === null || _response$sessionReco2 === void 0 ? void 0 : _response$sessionReco2.endpoint;
3821
+ this.endpoint = (_response$sessionReco3 = response.sessionRecording) === null || _response$sessionReco3 === void 0 ? void 0 : _response$sessionReco3.endpoint;
3812
3822
  }
3813
3823
 
3814
3824
  this.startRecordingIfEnabled();
@@ -3934,7 +3944,7 @@ var SessionRecording = /*#__PURE__*/function () {
3934
3944
  _this2.snapshots.push(properties);
3935
3945
  }
3936
3946
  },
3937
- plugins: window.rrwebConsoleRecord && this.instance.get_config('enable_recording_console_log') ? [window.rrwebConsoleRecord.getRecordConsolePlugin()] : []
3947
+ plugins: window.rrwebConsoleRecord && this.isConsoleLogCaptureEnabled() ? [window.rrwebConsoleRecord.getRecordConsolePlugin()] : []
3938
3948
  }, sessionRecordingOptions)); // :TRICKY: rrweb does not capture navigation within SPA-s, so hook into our $pageview events to get access to all events.
3939
3949
  // Dropping the initial event is fine (it's always captured by rrweb).
3940
3950
 
@@ -5549,12 +5559,28 @@ var SessionIdManager = /*#__PURE__*/function () {
5549
5559
  this._sessionId = undefined;
5550
5560
  this._sessionStartTimestamp = null;
5551
5561
  this._sessionActivityTimestamp = null;
5562
+ var persistenceName = config['persistence_name'] || config['token'];
5563
+ this.window_id_storage_key = 'ph_' + persistenceName + '_window_id';
5564
+ 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
5565
+ // if it exists here it means there was no unload which suggests this window is opened as a tab duplication, window.open, etc.
5566
+
5567
+ if (!this.persistence.disabled && sessionStore.is_supported()) {
5568
+ var lastWindowId = sessionStore.parse(this.window_id_storage_key);
5569
+ var primaryWindowExists = sessionStore.parse(this.primary_window_exists_storage_key);
5570
+
5571
+ if (lastWindowId && !primaryWindowExists) {
5572
+ // Persist window from previous storage state
5573
+ this._windowId = lastWindowId;
5574
+ } else {
5575
+ // Wipe any reference to previous window id
5576
+ sessionStore.remove(this.window_id_storage_key);
5577
+ } // Flag this session as having a primary window
5552
5578
 
5553
- if (config['persistence_name']) {
5554
- this.window_id_storage_key = 'ph_' + config['persistence_name'] + '_window_id';
5555
- } else {
5556
- this.window_id_storage_key = 'ph_' + config['token'] + '_window_id';
5579
+
5580
+ sessionStore.set(this.primary_window_exists_storage_key, true);
5557
5581
  }
5582
+
5583
+ this._listenToReloadWindow();
5558
5584
  } // Note: this tries to store the windowId in sessionStorage. SessionStorage is unique to the current window/tab,
5559
5585
  // and persists page loads/reloads. So it's uniquely suited for storing the windowId. This function also respects
5560
5586
  // when persistence is disabled (by user config) and when sessionStorage is not supported (it *should* be supported on all browsers),
@@ -5581,7 +5607,8 @@ var SessionIdManager = /*#__PURE__*/function () {
5581
5607
 
5582
5608
  if (!this.persistence.disabled && sessionStore.is_supported()) {
5583
5609
  return sessionStore.parse(this.window_id_storage_key);
5584
- }
5610
+ } // New window id will be generated
5611
+
5585
5612
 
5586
5613
  return null;
5587
5614
  } // Note: 'this.persistence.register' can be disabled in the config.
@@ -5620,6 +5647,24 @@ var SessionIdManager = /*#__PURE__*/function () {
5620
5647
  value: function resetSessionId() {
5621
5648
  this._setSessionId(null, null, null);
5622
5649
  }
5650
+ /*
5651
+ * Listens to window unloads and removes the primaryWindowExists key from sessionStorage.
5652
+ * Reloaded or fresh tabs created after a DOM unloads (reloading the same tab) WILL NOT have this primaryWindowExists flag in session storage.
5653
+ * Cloned sessions (new tab, tab duplication, window.open(), ...) WILL have this primaryWindowExists flag in their copied session storage.
5654
+ * We conditionally check the primaryWindowExists value in the constructor to decide if the window id in the last session storage should be carried over.
5655
+ */
5656
+
5657
+ }, {
5658
+ key: "_listenToReloadWindow",
5659
+ value: function _listenToReloadWindow() {
5660
+ var _this = this;
5661
+
5662
+ window.addEventListener('beforeunload', function () {
5663
+ if (!_this.persistence.disabled && sessionStore.is_supported()) {
5664
+ sessionStore.remove(_this.primary_window_exists_storage_key);
5665
+ }
5666
+ });
5667
+ }
5623
5668
  /*
5624
5669
  * This function returns the current sessionId and windowId. It should be used to
5625
5670
  * access these values over directly calling `._sessionId` or `._windowId`. In addition
@@ -5830,6 +5875,8 @@ var SentryIntegration = /*#__PURE__*/_createClass(function SentryIntegration(_po
5830
5875
 
5831
5876
  this.setupOnce = function (addGlobalEventProcessor) {
5832
5877
  addGlobalEventProcessor(function (event) {
5878
+ var _event$exception, _exceptions$, _exceptions$2;
5879
+
5833
5880
  if (event.level !== 'error' || !_posthog.__loaded) return event;
5834
5881
  if (!event.tags) event.tags = {};
5835
5882
  event.tags['PostHog Person URL'] = _posthog.config.api_host + '/person/' + _posthog.get_distinct_id();
@@ -5838,9 +5885,13 @@ var SentryIntegration = /*#__PURE__*/_createClass(function SentryIntegration(_po
5838
5885
  event.tags['PostHog Recording URL'] = _posthog.config.api_host + '/recordings/#sessionRecordingId=' + _posthog.sessionManager.checkAndGetSessionAndWindowId(true).sessionId;
5839
5886
  }
5840
5887
 
5888
+ var exceptions = ((_event$exception = event.exception) === null || _event$exception === void 0 ? void 0 : _event$exception.values) || [];
5841
5889
  var data = {
5842
5890
  $sentry_event_id: event.event_id,
5843
- $sentry_exception: event.exception
5891
+ $sentry_exception: event.exception,
5892
+ $sentry_exception_message: (_exceptions$ = exceptions[0]) === null || _exceptions$ === void 0 ? void 0 : _exceptions$.value,
5893
+ $sentry_exception_type: (_exceptions$2 = exceptions[0]) === null || _exceptions$2 === void 0 ? void 0 : _exceptions$2.type,
5894
+ $sentry_tags: event.tags
5844
5895
  };
5845
5896
  if (organization && projectId) data['$sentry_url'] = (prefix || 'https://sentry.io/organizations/') + organization + '/issues/?project=' + projectId + '&query=' + event.event_id;
5846
5897
 
@@ -5915,7 +5966,8 @@ var defaultConfig = function defaultConfig() {
5915
5966
  disable_session_recording: false,
5916
5967
  disable_persistence: false,
5917
5968
  disable_cookie: false,
5918
- enable_recording_console_log: false,
5969
+ enable_recording_console_log: undefined,
5970
+ // When undefined, it falls back to the server-side setting
5919
5971
  secure_cookie: (win === null || win === void 0 ? void 0 : (_window$location = win.location) === null || _window$location === void 0 ? void 0 : _window$location.protocol) === 'https:',
5920
5972
  ip: true,
5921
5973
  opt_out_capturing_by_default: false,