posthog-js 1.17.8 → 1.19.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
@@ -863,7 +863,7 @@ var LZString = {
863
863
  }
864
864
  };
865
865
 
866
- var version = "1.17.8";
866
+ var version = "1.19.0";
867
867
 
868
868
  var Config$1 = {
869
869
  DEBUG: false,
@@ -900,15 +900,18 @@ var _ = {
900
900
  }
901
901
  }; // Console override
902
902
 
903
- var console$1 = {
903
+ var logger = {
904
904
  /** @type {function(...*)} */
905
905
  log: function log() {
906
906
  if (Config$1.DEBUG && !_.isUndefined(window.console) && window.console) {
907
+ // Don't log PostHog debug messages in rrweb
908
+ var log = window.console.log['__rrweb_original__'] ? window.console.log['__rrweb_original__'] : window.console.log;
909
+
907
910
  try {
908
- window.console.log.apply(window.console, arguments);
911
+ log.apply(window.console, arguments);
909
912
  } catch (err) {
910
913
  _.each(arguments, function (arg) {
911
- window.console.log(arg);
914
+ log(arg);
912
915
  });
913
916
  }
914
917
  }
@@ -917,13 +920,15 @@ var console$1 = {
917
920
  /** @type {function(...*)} */
918
921
  error: function error() {
919
922
  if (Config$1.DEBUG && !_.isUndefined(window.console) && window.console) {
920
- var args = ['PostHog error:'].concat(Array.prototype.slice.call(arguments));
923
+ var args = ['PostHog error:'].concat(Array.prototype.slice.call(arguments)); // Don't log PostHog debug messages in rrweb
924
+
925
+ var error = window.console.error['__rrweb_original__'] ? window.console.error['__rrweb_original__'] : window.console.error;
921
926
 
922
927
  try {
923
- window.console.error.apply(window.console, args);
928
+ error.apply(window.console, args);
924
929
  } catch (err) {
925
930
  _.each(args, function (arg) {
926
- window.console.error(arg);
931
+ error(arg);
927
932
  });
928
933
  }
929
934
  }
@@ -932,13 +937,15 @@ var console$1 = {
932
937
  /** @type {function(...*)} */
933
938
  critical: function critical() {
934
939
  if (!_.isUndefined(window.console) && window.console) {
935
- var args = ['PostHog error:'].concat(Array.prototype.slice.call(arguments));
940
+ var args = ['PostHog error:'].concat(Array.prototype.slice.call(arguments)); // Don't log PostHog debug messages in rrweb
941
+
942
+ var error = window.console.error['__rrweb_original__'] ? window.console.error['__rrweb_original__'] : window.console.error;
936
943
 
937
944
  try {
938
- window.console.error.apply(window.console, args);
945
+ error.apply(window.console, args);
939
946
  } catch (err) {
940
947
  _.each(args, function (arg) {
941
- window.console.error(arg);
948
+ error(arg);
942
949
  });
943
950
  }
944
951
  }
@@ -1138,10 +1145,10 @@ _.safewrap = function (f) {
1138
1145
  try {
1139
1146
  return f.apply(this, arguments);
1140
1147
  } catch (e) {
1141
- console$1.critical('Implementation error. Please turn on debug and contact support@posthog.com.');
1148
+ logger.critical('Implementation error. Please turn on debug and contact support@posthog.com.');
1142
1149
 
1143
1150
  if (Config$1.DEBUG) {
1144
- console$1.critical(e);
1151
+ logger.critical(e);
1145
1152
  }
1146
1153
  }
1147
1154
  };
@@ -1432,7 +1439,7 @@ _.getQueryParam = function (url, param) {
1432
1439
  try {
1433
1440
  result = decodeURIComponent(result);
1434
1441
  } catch (err) {
1435
- console$1.error('Skipping decoding for malformed query param: ' + result);
1442
+ logger.error('Skipping decoding for malformed query param: ' + result);
1436
1443
  }
1437
1444
 
1438
1445
  return result.replace(/\+/g, ' ');
@@ -1461,7 +1468,7 @@ _.register_event = function () {
1461
1468
  */
1462
1469
  var register_event = function register_event(element, type, handler, oldSchool, useCapture) {
1463
1470
  if (!element) {
1464
- console$1.error('No valid element provided to register_event');
1471
+ logger.error('No valid element provided to register_event');
1465
1472
  return;
1466
1473
  }
1467
1474
 
@@ -2302,7 +2309,7 @@ var autocapture = {
2302
2309
  var token = instance.get_config('token');
2303
2310
 
2304
2311
  if (this._initializedTokens.indexOf(token) > -1) {
2305
- console.log('autocapture already initialized for token "' + token + '"');
2312
+ logger.log('autocapture already initialized for token "' + token + '"');
2306
2313
  return;
2307
2314
  }
2308
2315
 
@@ -2441,14 +2448,14 @@ var localStore = {
2441
2448
  }
2442
2449
 
2443
2450
  if (!supported) {
2444
- console$1.error('localStorage unsupported; falling back to cookie store');
2451
+ logger.error('localStorage unsupported; falling back to cookie store');
2445
2452
  }
2446
2453
 
2447
2454
  _localStorage_supported = supported;
2448
2455
  return supported;
2449
2456
  },
2450
2457
  error: function error(msg) {
2451
- console$1.error('localStorage error: ' + msg);
2458
+ logger.error('localStorage error: ' + msg);
2452
2459
  },
2453
2460
  get: function get(name) {
2454
2461
  try {
@@ -2539,7 +2546,7 @@ var memoryStore = {
2539
2546
  return true;
2540
2547
  },
2541
2548
  error: function error(msg) {
2542
- console$1.error('memoryStorage error: ' + msg);
2549
+ logger.error('memoryStorage error: ' + msg);
2543
2550
  },
2544
2551
  parse: function parse(name) {
2545
2552
  return memoryStorage[name] || null;
@@ -2583,7 +2590,7 @@ var sessionStore = {
2583
2590
  },
2584
2591
  error: function error(msg) {
2585
2592
  if (Config.DEBUG) {
2586
- console$1.error('sessionStorage error: ', msg);
2593
+ logger.error('sessionStorage error: ', msg);
2587
2594
  }
2588
2595
  },
2589
2596
  get: function get(name) {
@@ -3443,7 +3450,7 @@ var PostHogPersistence = function PostHogPersistence(config) {
3443
3450
  var storage_type = config['persistence'];
3444
3451
 
3445
3452
  if (storage_type !== 'cookie' && storage_type.indexOf('localStorage') === -1 && storage_type !== 'memory') {
3446
- console$1.critical('Unknown persistence type ' + storage_type + '; falling back to cookie');
3453
+ logger.critical('Unknown persistence type ' + storage_type + '; falling back to cookie');
3447
3454
  storage_type = config['persistence'] = 'cookie';
3448
3455
  }
3449
3456
 
@@ -3831,7 +3838,7 @@ var SessionRecording = /*#__PURE__*/function () {
3831
3838
 
3832
3839
 
3833
3840
  if ((this.windowId !== windowId || this.sessionId !== sessionId) && [FULL_SNAPSHOT_EVENT_TYPE, META_EVENT_TYPE].indexOf(event.type) === -1) {
3834
- window.rrweb.record.takeFullSnapshot();
3841
+ this.rrwebRecord.takeFullSnapshot();
3835
3842
  }
3836
3843
 
3837
3844
  this.windowId = windowId;
@@ -3854,7 +3861,10 @@ var SessionRecording = /*#__PURE__*/function () {
3854
3861
  maskInputFn: null,
3855
3862
  slimDOMOptions: {},
3856
3863
  collectFonts: false
3857
- }; // only allows user to set our 'whitelisted' options
3864
+ }; // We switched from loading all of rrweb to just the record part, but
3865
+ // keep backwards compatibility if someone hasn't upgraded PostHog
3866
+
3867
+ this.rrwebRecord = window.rrweb ? window.rrweb.record : window.rrwebRecord; // only allows user to set our 'whitelisted' options
3858
3868
 
3859
3869
  var userSessionRecordingOptions = this.instance.get_config('session_recording');
3860
3870
 
@@ -3868,7 +3878,7 @@ var SessionRecording = /*#__PURE__*/function () {
3868
3878
  }
3869
3879
  }
3870
3880
 
3871
- this.stopRrweb = window.rrweb.record(_objectSpread2({
3881
+ this.stopRrweb = this.rrwebRecord(_objectSpread2({
3872
3882
  emit: function emit(event) {
3873
3883
  event = filterDataURLsFromLargeDataObjects(event);
3874
3884
 
@@ -3889,13 +3899,14 @@ var SessionRecording = /*#__PURE__*/function () {
3889
3899
  } else {
3890
3900
  _this2.snapshots.push(properties);
3891
3901
  }
3892
- }
3902
+ },
3903
+ plugins: window.rrwebConsoleRecord && this.instance.get_config('enable_recording_console_log') ? [window.rrwebConsoleRecord.getRecordConsolePlugin()] : []
3893
3904
  }, sessionRecordingOptions)); // :TRICKY: rrweb does not capture navigation within SPA-s, so hook into our $pageview events to get access to all events.
3894
3905
  // Dropping the initial event is fine (it's always captured by rrweb).
3895
3906
 
3896
3907
  this.instance._addCaptureHook(function (eventName) {
3897
3908
  if (eventName === '$pageview') {
3898
- window.rrweb.record.addCustomEvent('$pageview', {
3909
+ _this2.rrwebRecord.addCustomEvent('$pageview', {
3899
3910
  href: window.location.href
3900
3911
  });
3901
3912
  }
@@ -5155,6 +5166,14 @@ function compressData(compression, jsonData, options) {
5155
5166
  }
5156
5167
  }
5157
5168
 
5169
+ var addParamsToURL = function addParamsToURL(url, urlQueryArgs, parameterOptions) {
5170
+ var args = urlQueryArgs || {};
5171
+ args['ip'] = parameterOptions['ip'] ? 1 : 0;
5172
+ args['_'] = new Date().getTime().toString();
5173
+ args['v'] = Config$1.LIB_VERSION;
5174
+ var argSeparator = url.indexOf('?') > -1 ? '&' : '?';
5175
+ return url + argSeparator + _.HTTPBuildQuery(args);
5176
+ };
5158
5177
  var encodePostData = function encodePostData(data, options) {
5159
5178
  if (options.blob && data.buffer) {
5160
5179
  return new Blob([data.buffer], {
@@ -5234,7 +5253,7 @@ var xhr = function xhr(_ref) {
5234
5253
  try {
5235
5254
  response = JSON.parse(req.responseText);
5236
5255
  } catch (e) {
5237
- console$1.error(e);
5256
+ logger.error(e);
5238
5257
  return;
5239
5258
  }
5240
5259
 
@@ -5617,9 +5636,33 @@ function deduplicateKeys(performanceEntries) {
5617
5636
  });
5618
5637
  })];
5619
5638
  }
5639
+ /*
5640
+ The duration property is on the PerformanceNavigationTiming object.
5641
+
5642
+ It is a timestamp that is the difference between the PerformanceNavigationTiming.loadEventEnd
5643
+ and PerformanceEntry.startTime properties.
5644
+ https://developer.mozilla.org/en-US/docs/Web/API/PerformanceNavigationTiming
5645
+
5646
+ Even in browsers that implement it, it is not always available to us
5647
+ */
5648
+
5620
5649
  function pageLoadFrom(performanceData) {
5621
- var keyIndex = performanceData.navigation && performanceData.navigation[0] && performanceData.navigation[0].indexOf('duration');
5622
- return performanceData.navigation[1] && performanceData.navigation[1][0][keyIndex];
5650
+ var keys = performanceData.navigation && performanceData.navigation[0];
5651
+ var values = performanceData.navigation && performanceData.navigation[1] && performanceData.navigation[1][0];
5652
+ var durationIndex = keys && keys.indexOf('duration');
5653
+
5654
+ if (durationIndex > -1) {
5655
+ return values[durationIndex];
5656
+ } else {
5657
+ var endKeyIndex = keys && keys.indexOf('loadEventEnd');
5658
+ var startKeyIndex = keys && keys.indexOf('startTime'); // start key is not present if start is 0
5659
+
5660
+ if (endKeyIndex > -1) {
5661
+ var end = values && values[endKeyIndex];
5662
+ var start = values && values[startKeyIndex] || 0;
5663
+ return end - start;
5664
+ }
5665
+ }
5623
5666
  }
5624
5667
  function getPerformanceData() {
5625
5668
  var performanceEntries = {
@@ -5695,6 +5738,7 @@ var defaultConfig = function defaultConfig() {
5695
5738
  disable_session_recording: false,
5696
5739
  disable_persistence: false,
5697
5740
  disable_cookie: false,
5741
+ enable_recording_console_log: false,
5698
5742
  secure_cookie: win.location.protocol === 'https:',
5699
5743
  ip: true,
5700
5744
  opt_out_capturing_by_default: false,
@@ -5727,7 +5771,7 @@ var defaultConfig = function defaultConfig() {
5727
5771
  advanced_disable_toolbar_metrics: false,
5728
5772
  on_xhr_error: function on_xhr_error(req) {
5729
5773
  var error = 'Bad HTTP status: ' + req.status + ' ' + req.statusText;
5730
- console$1.error(error);
5774
+ console.error(error);
5731
5775
  },
5732
5776
  // Used for internal testing
5733
5777
  _onCapture: function _onCapture() {},
@@ -5759,7 +5803,7 @@ var create_mplib = function create_mplib(token, config, name) {
5759
5803
  instance = target;
5760
5804
  } else {
5761
5805
  if (target && !_.isArray(target)) {
5762
- console$1.error('You have already initialized ' + name);
5806
+ console.error('You have already initialized ' + name);
5763
5807
  return;
5764
5808
  }
5765
5809
 
@@ -5786,10 +5830,10 @@ var create_mplib = function create_mplib(token, config, name) {
5786
5830
 
5787
5831
  if (!autocapture.enabledForProject(instance.get_config('token'), num_buckets, num_enabled_buckets)) {
5788
5832
  instance['__autocapture_enabled'] = false;
5789
- console$1.log('Not in active bucket: disabling Automatic Event Collection.');
5833
+ logger.log('Not in active bucket: disabling Automatic Event Collection.');
5790
5834
  } else if (!autocapture.isBrowserSupported()) {
5791
5835
  instance['__autocapture_enabled'] = false;
5792
- console$1.log('Disabling Automatic Event Collection because this browser is not supported');
5836
+ logger.log('Disabling Automatic Event Collection because this browser is not supported');
5793
5837
  } else {
5794
5838
  autocapture.init(instance);
5795
5839
  }
@@ -5831,12 +5875,12 @@ var create_mplib = function create_mplib(token, config, name) {
5831
5875
 
5832
5876
  PostHogLib.prototype.init = function (token, config, name) {
5833
5877
  if (_.isUndefined(name)) {
5834
- console$1.error('You must name your new library: init(token, config, name)');
5878
+ console.error('You must name your new library: init(token, config, name)');
5835
5879
  return;
5836
5880
  }
5837
5881
 
5838
5882
  if (name === PRIMARY_INSTANCE_NAME) {
5839
- console$1.error('You must initialize the main posthog object right after you include the PostHog js snippet');
5883
+ console.error('You must initialize the main posthog object right after you include the PostHog js snippet');
5840
5884
  return;
5841
5885
  }
5842
5886
 
@@ -5903,7 +5947,7 @@ PostHogLib.prototype._loaded = function () {
5903
5947
  try {
5904
5948
  this.get_config('loaded')(this);
5905
5949
  } catch (err) {
5906
- console$1.error('`loaded` function failed', err);
5950
+ console.error('`loaded` function failed', err);
5907
5951
  }
5908
5952
 
5909
5953
  this._start_queue_if_opted_in(); // this happens after so a user can call identify in
@@ -6045,11 +6089,9 @@ PostHogLib.prototype._send_request = function (url, data, options, callback) {
6045
6089
  }
6046
6090
 
6047
6091
  var useSendBeacon = win.navigator.sendBeacon && options.transport.toLowerCase() === 'sendbeacon';
6048
- var args = options.urlQueryArgs || {};
6049
- args['ip'] = this.get_config('ip') ? 1 : 0;
6050
- args['_'] = new Date().getTime().toString();
6051
- var argSeparator = url.indexOf('?') > -1 ? '&' : '?';
6052
- url += argSeparator + _.HTTPBuildQuery(args);
6092
+ url = addParamsToURL(url, options.urlQueryArgs, {
6093
+ ip: this.get_config('ip')
6094
+ });
6053
6095
 
6054
6096
  if (_.isObject(data) && this.get_config('img')) {
6055
6097
  var img = document$1.createElement('img');
@@ -6066,7 +6108,7 @@ PostHogLib.prototype._send_request = function (url, data, options, callback) {
6066
6108
  })));
6067
6109
  } catch (e) {
6068
6110
  if (this.get_config('debug')) {
6069
- console$1.error(e);
6111
+ console.error(e);
6070
6112
  }
6071
6113
  }
6072
6114
  } else if (USE_XHR) {
@@ -6083,7 +6125,7 @@ PostHogLib.prototype._send_request = function (url, data, options, callback) {
6083
6125
  onXHRError: this.get_config('on_xhr_error')
6084
6126
  });
6085
6127
  } catch (e) {
6086
- console$1.error(e);
6128
+ console.error(e);
6087
6129
  }
6088
6130
  } else {
6089
6131
  var script = document$1.createElement('script');
@@ -6209,7 +6251,7 @@ PostHogLib.prototype.capture = addOptOutCheckPostHogLib(function (event_name, pr
6209
6251
  }
6210
6252
 
6211
6253
  if (_.isUndefined(event_name) || typeof event_name !== 'string') {
6212
- console$1.error('No event name provided to posthog.capture');
6254
+ console.error('No event name provided to posthog.capture');
6213
6255
  return;
6214
6256
  }
6215
6257
 
@@ -6241,7 +6283,7 @@ PostHogLib.prototype.capture = addOptOutCheckPostHogLib(function (event_name, pr
6241
6283
  data = _.copyAndTruncateStrings(data, options._noTruncate ? null : this.get_config('properties_string_max_length'));
6242
6284
 
6243
6285
  if (this.get_config('debug')) {
6244
- console$1.log('PostHog.js send', data);
6286
+ logger.log('PostHog.js send', data);
6245
6287
  }
6246
6288
 
6247
6289
  var jsonData = JSON.stringify(data);
@@ -6275,7 +6317,8 @@ PostHogLib.prototype._invokeCaptureHooks = function (eventName, eventData) {
6275
6317
 
6276
6318
  PostHogLib.prototype._calculate_event_properties = function (event_name, event_properties, start_timestamp) {
6277
6319
  // set defaults
6278
- var properties = event_properties || {};
6320
+ var properties = _objectSpread2({}, event_properties);
6321
+
6279
6322
  properties['token'] = this.get_config('token');
6280
6323
 
6281
6324
  if (event_name === '$snapshot') {
@@ -6316,7 +6359,7 @@ PostHogLib.prototype._calculate_event_properties = function (event_name, event_p
6316
6359
  delete properties[blacklisted_prop];
6317
6360
  });
6318
6361
  } else {
6319
- console$1.error('Invalid value for property_blacklist config: ' + property_blacklist);
6362
+ console.error('Invalid value for property_blacklist config: ' + property_blacklist);
6320
6363
  }
6321
6364
 
6322
6365
  var sanitize_properties = this.get_config('sanitize_properties');
@@ -6485,7 +6528,7 @@ PostHogLib.prototype.onFeatureFlags = function (callback) {
6485
6528
  PostHogLib.prototype.identify = function (new_distinct_id, userPropertiesToSet, userPropertiesToSetOnce) {
6486
6529
  //if the new_distinct_id has not been set ignore the identify event
6487
6530
  if (!new_distinct_id) {
6488
- console$1.error('Unique user id has not been set in posthog.identify');
6531
+ console.error('Unique user id has not been set in posthog.identify');
6489
6532
  return;
6490
6533
  }
6491
6534
 
@@ -6555,7 +6598,7 @@ PostHogLib.prototype.identify = function (new_distinct_id, userPropertiesToSet,
6555
6598
 
6556
6599
  PostHogLib.prototype.group = function (groupType, groupKey, groupPropertiesToSet) {
6557
6600
  if (!groupType || !groupKey) {
6558
- console$1.error('posthog.group requires a group type and group key');
6601
+ console.error('posthog.group requires a group type and group key');
6559
6602
  return;
6560
6603
  }
6561
6604
 
@@ -6649,7 +6692,7 @@ PostHogLib.prototype.alias = function (alias, original) {
6649
6692
  // posthog.people.identify() call made for this user. It is VERY BAD to make an alias with
6650
6693
  // this ID, as it will duplicate users.
6651
6694
  if (alias === this.get_property(PEOPLE_DISTINCT_ID_KEY)) {
6652
- console$1.critical('Attempting to create alias for existing People user - aborting.');
6695
+ console.critical('Attempting to create alias for existing People user - aborting.');
6653
6696
  return -2;
6654
6697
  }
6655
6698
 
@@ -6670,7 +6713,7 @@ PostHogLib.prototype.alias = function (alias, original) {
6670
6713
  _this.identify(alias);
6671
6714
  });
6672
6715
  } else {
6673
- console$1.error('alias matches current distinct_id - skipping api call.');
6716
+ console.error('alias matches current distinct_id - skipping api call.');
6674
6717
  this.identify(alias);
6675
6718
  return -1;
6676
6719
  }