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/module.js CHANGED
@@ -865,7 +865,7 @@ var LZString = {
865
865
  }
866
866
  };
867
867
 
868
- var version = "1.17.8";
868
+ var version = "1.19.0";
869
869
 
870
870
  var Config$1 = {
871
871
  DEBUG: false,
@@ -902,15 +902,18 @@ var _ = {
902
902
  }
903
903
  }; // Console override
904
904
 
905
- var console$1 = {
905
+ var logger = {
906
906
  /** @type {function(...*)} */
907
907
  log: function log() {
908
908
  if (Config$1.DEBUG && !_.isUndefined(window.console) && window.console) {
909
+ // Don't log PostHog debug messages in rrweb
910
+ var log = window.console.log['__rrweb_original__'] ? window.console.log['__rrweb_original__'] : window.console.log;
911
+
909
912
  try {
910
- window.console.log.apply(window.console, arguments);
913
+ log.apply(window.console, arguments);
911
914
  } catch (err) {
912
915
  _.each(arguments, function (arg) {
913
- window.console.log(arg);
916
+ log(arg);
914
917
  });
915
918
  }
916
919
  }
@@ -919,13 +922,15 @@ var console$1 = {
919
922
  /** @type {function(...*)} */
920
923
  error: function error() {
921
924
  if (Config$1.DEBUG && !_.isUndefined(window.console) && window.console) {
922
- var args = ['PostHog error:'].concat(Array.prototype.slice.call(arguments));
925
+ var args = ['PostHog error:'].concat(Array.prototype.slice.call(arguments)); // Don't log PostHog debug messages in rrweb
926
+
927
+ var error = window.console.error['__rrweb_original__'] ? window.console.error['__rrweb_original__'] : window.console.error;
923
928
 
924
929
  try {
925
- window.console.error.apply(window.console, args);
930
+ error.apply(window.console, args);
926
931
  } catch (err) {
927
932
  _.each(args, function (arg) {
928
- window.console.error(arg);
933
+ error(arg);
929
934
  });
930
935
  }
931
936
  }
@@ -934,13 +939,15 @@ var console$1 = {
934
939
  /** @type {function(...*)} */
935
940
  critical: function critical() {
936
941
  if (!_.isUndefined(window.console) && window.console) {
937
- var args = ['PostHog error:'].concat(Array.prototype.slice.call(arguments));
942
+ var args = ['PostHog error:'].concat(Array.prototype.slice.call(arguments)); // Don't log PostHog debug messages in rrweb
943
+
944
+ var error = window.console.error['__rrweb_original__'] ? window.console.error['__rrweb_original__'] : window.console.error;
938
945
 
939
946
  try {
940
- window.console.error.apply(window.console, args);
947
+ error.apply(window.console, args);
941
948
  } catch (err) {
942
949
  _.each(args, function (arg) {
943
- window.console.error(arg);
950
+ error(arg);
944
951
  });
945
952
  }
946
953
  }
@@ -1140,10 +1147,10 @@ _.safewrap = function (f) {
1140
1147
  try {
1141
1148
  return f.apply(this, arguments);
1142
1149
  } catch (e) {
1143
- console$1.critical('Implementation error. Please turn on debug and contact support@posthog.com.');
1150
+ logger.critical('Implementation error. Please turn on debug and contact support@posthog.com.');
1144
1151
 
1145
1152
  if (Config$1.DEBUG) {
1146
- console$1.critical(e);
1153
+ logger.critical(e);
1147
1154
  }
1148
1155
  }
1149
1156
  };
@@ -1434,7 +1441,7 @@ _.getQueryParam = function (url, param) {
1434
1441
  try {
1435
1442
  result = decodeURIComponent(result);
1436
1443
  } catch (err) {
1437
- console$1.error('Skipping decoding for malformed query param: ' + result);
1444
+ logger.error('Skipping decoding for malformed query param: ' + result);
1438
1445
  }
1439
1446
 
1440
1447
  return result.replace(/\+/g, ' ');
@@ -1463,7 +1470,7 @@ _.register_event = function () {
1463
1470
  */
1464
1471
  var register_event = function register_event(element, type, handler, oldSchool, useCapture) {
1465
1472
  if (!element) {
1466
- console$1.error('No valid element provided to register_event');
1473
+ logger.error('No valid element provided to register_event');
1467
1474
  return;
1468
1475
  }
1469
1476
 
@@ -2304,7 +2311,7 @@ var autocapture = {
2304
2311
  var token = instance.get_config('token');
2305
2312
 
2306
2313
  if (this._initializedTokens.indexOf(token) > -1) {
2307
- console.log('autocapture already initialized for token "' + token + '"');
2314
+ logger.log('autocapture already initialized for token "' + token + '"');
2308
2315
  return;
2309
2316
  }
2310
2317
 
@@ -2443,14 +2450,14 @@ var localStore = {
2443
2450
  }
2444
2451
 
2445
2452
  if (!supported) {
2446
- console$1.error('localStorage unsupported; falling back to cookie store');
2453
+ logger.error('localStorage unsupported; falling back to cookie store');
2447
2454
  }
2448
2455
 
2449
2456
  _localStorage_supported = supported;
2450
2457
  return supported;
2451
2458
  },
2452
2459
  error: function error(msg) {
2453
- console$1.error('localStorage error: ' + msg);
2460
+ logger.error('localStorage error: ' + msg);
2454
2461
  },
2455
2462
  get: function get(name) {
2456
2463
  try {
@@ -2541,7 +2548,7 @@ var memoryStore = {
2541
2548
  return true;
2542
2549
  },
2543
2550
  error: function error(msg) {
2544
- console$1.error('memoryStorage error: ' + msg);
2551
+ logger.error('memoryStorage error: ' + msg);
2545
2552
  },
2546
2553
  parse: function parse(name) {
2547
2554
  return memoryStorage[name] || null;
@@ -2585,7 +2592,7 @@ var sessionStore = {
2585
2592
  },
2586
2593
  error: function error(msg) {
2587
2594
  if (Config.DEBUG) {
2588
- console$1.error('sessionStorage error: ', msg);
2595
+ logger.error('sessionStorage error: ', msg);
2589
2596
  }
2590
2597
  },
2591
2598
  get: function get(name) {
@@ -3445,7 +3452,7 @@ var PostHogPersistence = function PostHogPersistence(config) {
3445
3452
  var storage_type = config['persistence'];
3446
3453
 
3447
3454
  if (storage_type !== 'cookie' && storage_type.indexOf('localStorage') === -1 && storage_type !== 'memory') {
3448
- console$1.critical('Unknown persistence type ' + storage_type + '; falling back to cookie');
3455
+ logger.critical('Unknown persistence type ' + storage_type + '; falling back to cookie');
3449
3456
  storage_type = config['persistence'] = 'cookie';
3450
3457
  }
3451
3458
 
@@ -3833,7 +3840,7 @@ var SessionRecording = /*#__PURE__*/function () {
3833
3840
 
3834
3841
 
3835
3842
  if ((this.windowId !== windowId || this.sessionId !== sessionId) && [FULL_SNAPSHOT_EVENT_TYPE, META_EVENT_TYPE].indexOf(event.type) === -1) {
3836
- window.rrweb.record.takeFullSnapshot();
3843
+ this.rrwebRecord.takeFullSnapshot();
3837
3844
  }
3838
3845
 
3839
3846
  this.windowId = windowId;
@@ -3856,7 +3863,10 @@ var SessionRecording = /*#__PURE__*/function () {
3856
3863
  maskInputFn: null,
3857
3864
  slimDOMOptions: {},
3858
3865
  collectFonts: false
3859
- }; // only allows user to set our 'whitelisted' options
3866
+ }; // We switched from loading all of rrweb to just the record part, but
3867
+ // keep backwards compatibility if someone hasn't upgraded PostHog
3868
+
3869
+ this.rrwebRecord = window.rrweb ? window.rrweb.record : window.rrwebRecord; // only allows user to set our 'whitelisted' options
3860
3870
 
3861
3871
  var userSessionRecordingOptions = this.instance.get_config('session_recording');
3862
3872
 
@@ -3870,7 +3880,7 @@ var SessionRecording = /*#__PURE__*/function () {
3870
3880
  }
3871
3881
  }
3872
3882
 
3873
- this.stopRrweb = window.rrweb.record(_objectSpread2({
3883
+ this.stopRrweb = this.rrwebRecord(_objectSpread2({
3874
3884
  emit: function emit(event) {
3875
3885
  event = filterDataURLsFromLargeDataObjects(event);
3876
3886
 
@@ -3891,13 +3901,14 @@ var SessionRecording = /*#__PURE__*/function () {
3891
3901
  } else {
3892
3902
  _this2.snapshots.push(properties);
3893
3903
  }
3894
- }
3904
+ },
3905
+ plugins: window.rrwebConsoleRecord && this.instance.get_config('enable_recording_console_log') ? [window.rrwebConsoleRecord.getRecordConsolePlugin()] : []
3895
3906
  }, sessionRecordingOptions)); // :TRICKY: rrweb does not capture navigation within SPA-s, so hook into our $pageview events to get access to all events.
3896
3907
  // Dropping the initial event is fine (it's always captured by rrweb).
3897
3908
 
3898
3909
  this.instance._addCaptureHook(function (eventName) {
3899
3910
  if (eventName === '$pageview') {
3900
- window.rrweb.record.addCustomEvent('$pageview', {
3911
+ _this2.rrwebRecord.addCustomEvent('$pageview', {
3901
3912
  href: window.location.href
3902
3913
  });
3903
3914
  }
@@ -5157,6 +5168,14 @@ function compressData(compression, jsonData, options) {
5157
5168
  }
5158
5169
  }
5159
5170
 
5171
+ var addParamsToURL = function addParamsToURL(url, urlQueryArgs, parameterOptions) {
5172
+ var args = urlQueryArgs || {};
5173
+ args['ip'] = parameterOptions['ip'] ? 1 : 0;
5174
+ args['_'] = new Date().getTime().toString();
5175
+ args['v'] = Config$1.LIB_VERSION;
5176
+ var argSeparator = url.indexOf('?') > -1 ? '&' : '?';
5177
+ return url + argSeparator + _.HTTPBuildQuery(args);
5178
+ };
5160
5179
  var encodePostData = function encodePostData(data, options) {
5161
5180
  if (options.blob && data.buffer) {
5162
5181
  return new Blob([data.buffer], {
@@ -5236,7 +5255,7 @@ var xhr = function xhr(_ref) {
5236
5255
  try {
5237
5256
  response = JSON.parse(req.responseText);
5238
5257
  } catch (e) {
5239
- console$1.error(e);
5258
+ logger.error(e);
5240
5259
  return;
5241
5260
  }
5242
5261
 
@@ -5619,9 +5638,33 @@ function deduplicateKeys(performanceEntries) {
5619
5638
  });
5620
5639
  })];
5621
5640
  }
5641
+ /*
5642
+ The duration property is on the PerformanceNavigationTiming object.
5643
+
5644
+ It is a timestamp that is the difference between the PerformanceNavigationTiming.loadEventEnd
5645
+ and PerformanceEntry.startTime properties.
5646
+ https://developer.mozilla.org/en-US/docs/Web/API/PerformanceNavigationTiming
5647
+
5648
+ Even in browsers that implement it, it is not always available to us
5649
+ */
5650
+
5622
5651
  function pageLoadFrom(performanceData) {
5623
- var keyIndex = performanceData.navigation && performanceData.navigation[0] && performanceData.navigation[0].indexOf('duration');
5624
- return performanceData.navigation[1] && performanceData.navigation[1][0][keyIndex];
5652
+ var keys = performanceData.navigation && performanceData.navigation[0];
5653
+ var values = performanceData.navigation && performanceData.navigation[1] && performanceData.navigation[1][0];
5654
+ var durationIndex = keys && keys.indexOf('duration');
5655
+
5656
+ if (durationIndex > -1) {
5657
+ return values[durationIndex];
5658
+ } else {
5659
+ var endKeyIndex = keys && keys.indexOf('loadEventEnd');
5660
+ var startKeyIndex = keys && keys.indexOf('startTime'); // start key is not present if start is 0
5661
+
5662
+ if (endKeyIndex > -1) {
5663
+ var end = values && values[endKeyIndex];
5664
+ var start = values && values[startKeyIndex] || 0;
5665
+ return end - start;
5666
+ }
5667
+ }
5625
5668
  }
5626
5669
  function getPerformanceData() {
5627
5670
  var performanceEntries = {
@@ -5697,6 +5740,7 @@ var defaultConfig = function defaultConfig() {
5697
5740
  disable_session_recording: false,
5698
5741
  disable_persistence: false,
5699
5742
  disable_cookie: false,
5743
+ enable_recording_console_log: false,
5700
5744
  secure_cookie: win.location.protocol === 'https:',
5701
5745
  ip: true,
5702
5746
  opt_out_capturing_by_default: false,
@@ -5729,7 +5773,7 @@ var defaultConfig = function defaultConfig() {
5729
5773
  advanced_disable_toolbar_metrics: false,
5730
5774
  on_xhr_error: function on_xhr_error(req) {
5731
5775
  var error = 'Bad HTTP status: ' + req.status + ' ' + req.statusText;
5732
- console$1.error(error);
5776
+ console.error(error);
5733
5777
  },
5734
5778
  // Used for internal testing
5735
5779
  _onCapture: function _onCapture() {},
@@ -5761,7 +5805,7 @@ var create_mplib = function create_mplib(token, config, name) {
5761
5805
  instance = target;
5762
5806
  } else {
5763
5807
  if (target && !_.isArray(target)) {
5764
- console$1.error('You have already initialized ' + name);
5808
+ console.error('You have already initialized ' + name);
5765
5809
  return;
5766
5810
  }
5767
5811
 
@@ -5788,10 +5832,10 @@ var create_mplib = function create_mplib(token, config, name) {
5788
5832
 
5789
5833
  if (!autocapture.enabledForProject(instance.get_config('token'), num_buckets, num_enabled_buckets)) {
5790
5834
  instance['__autocapture_enabled'] = false;
5791
- console$1.log('Not in active bucket: disabling Automatic Event Collection.');
5835
+ logger.log('Not in active bucket: disabling Automatic Event Collection.');
5792
5836
  } else if (!autocapture.isBrowserSupported()) {
5793
5837
  instance['__autocapture_enabled'] = false;
5794
- console$1.log('Disabling Automatic Event Collection because this browser is not supported');
5838
+ logger.log('Disabling Automatic Event Collection because this browser is not supported');
5795
5839
  } else {
5796
5840
  autocapture.init(instance);
5797
5841
  }
@@ -5833,12 +5877,12 @@ var create_mplib = function create_mplib(token, config, name) {
5833
5877
 
5834
5878
  PostHogLib.prototype.init = function (token, config, name) {
5835
5879
  if (_.isUndefined(name)) {
5836
- console$1.error('You must name your new library: init(token, config, name)');
5880
+ console.error('You must name your new library: init(token, config, name)');
5837
5881
  return;
5838
5882
  }
5839
5883
 
5840
5884
  if (name === PRIMARY_INSTANCE_NAME) {
5841
- console$1.error('You must initialize the main posthog object right after you include the PostHog js snippet');
5885
+ console.error('You must initialize the main posthog object right after you include the PostHog js snippet');
5842
5886
  return;
5843
5887
  }
5844
5888
 
@@ -5905,7 +5949,7 @@ PostHogLib.prototype._loaded = function () {
5905
5949
  try {
5906
5950
  this.get_config('loaded')(this);
5907
5951
  } catch (err) {
5908
- console$1.error('`loaded` function failed', err);
5952
+ console.error('`loaded` function failed', err);
5909
5953
  }
5910
5954
 
5911
5955
  this._start_queue_if_opted_in(); // this happens after so a user can call identify in
@@ -6047,11 +6091,9 @@ PostHogLib.prototype._send_request = function (url, data, options, callback) {
6047
6091
  }
6048
6092
 
6049
6093
  var useSendBeacon = win.navigator.sendBeacon && options.transport.toLowerCase() === 'sendbeacon';
6050
- var args = options.urlQueryArgs || {};
6051
- args['ip'] = this.get_config('ip') ? 1 : 0;
6052
- args['_'] = new Date().getTime().toString();
6053
- var argSeparator = url.indexOf('?') > -1 ? '&' : '?';
6054
- url += argSeparator + _.HTTPBuildQuery(args);
6094
+ url = addParamsToURL(url, options.urlQueryArgs, {
6095
+ ip: this.get_config('ip')
6096
+ });
6055
6097
 
6056
6098
  if (_.isObject(data) && this.get_config('img')) {
6057
6099
  var img = document$1.createElement('img');
@@ -6068,7 +6110,7 @@ PostHogLib.prototype._send_request = function (url, data, options, callback) {
6068
6110
  })));
6069
6111
  } catch (e) {
6070
6112
  if (this.get_config('debug')) {
6071
- console$1.error(e);
6113
+ console.error(e);
6072
6114
  }
6073
6115
  }
6074
6116
  } else if (USE_XHR) {
@@ -6085,7 +6127,7 @@ PostHogLib.prototype._send_request = function (url, data, options, callback) {
6085
6127
  onXHRError: this.get_config('on_xhr_error')
6086
6128
  });
6087
6129
  } catch (e) {
6088
- console$1.error(e);
6130
+ console.error(e);
6089
6131
  }
6090
6132
  } else {
6091
6133
  var script = document$1.createElement('script');
@@ -6211,7 +6253,7 @@ PostHogLib.prototype.capture = addOptOutCheckPostHogLib(function (event_name, pr
6211
6253
  }
6212
6254
 
6213
6255
  if (_.isUndefined(event_name) || typeof event_name !== 'string') {
6214
- console$1.error('No event name provided to posthog.capture');
6256
+ console.error('No event name provided to posthog.capture');
6215
6257
  return;
6216
6258
  }
6217
6259
 
@@ -6243,7 +6285,7 @@ PostHogLib.prototype.capture = addOptOutCheckPostHogLib(function (event_name, pr
6243
6285
  data = _.copyAndTruncateStrings(data, options._noTruncate ? null : this.get_config('properties_string_max_length'));
6244
6286
 
6245
6287
  if (this.get_config('debug')) {
6246
- console$1.log('PostHog.js send', data);
6288
+ logger.log('PostHog.js send', data);
6247
6289
  }
6248
6290
 
6249
6291
  var jsonData = JSON.stringify(data);
@@ -6277,7 +6319,8 @@ PostHogLib.prototype._invokeCaptureHooks = function (eventName, eventData) {
6277
6319
 
6278
6320
  PostHogLib.prototype._calculate_event_properties = function (event_name, event_properties, start_timestamp) {
6279
6321
  // set defaults
6280
- var properties = event_properties || {};
6322
+ var properties = _objectSpread2({}, event_properties);
6323
+
6281
6324
  properties['token'] = this.get_config('token');
6282
6325
 
6283
6326
  if (event_name === '$snapshot') {
@@ -6318,7 +6361,7 @@ PostHogLib.prototype._calculate_event_properties = function (event_name, event_p
6318
6361
  delete properties[blacklisted_prop];
6319
6362
  });
6320
6363
  } else {
6321
- console$1.error('Invalid value for property_blacklist config: ' + property_blacklist);
6364
+ console.error('Invalid value for property_blacklist config: ' + property_blacklist);
6322
6365
  }
6323
6366
 
6324
6367
  var sanitize_properties = this.get_config('sanitize_properties');
@@ -6487,7 +6530,7 @@ PostHogLib.prototype.onFeatureFlags = function (callback) {
6487
6530
  PostHogLib.prototype.identify = function (new_distinct_id, userPropertiesToSet, userPropertiesToSetOnce) {
6488
6531
  //if the new_distinct_id has not been set ignore the identify event
6489
6532
  if (!new_distinct_id) {
6490
- console$1.error('Unique user id has not been set in posthog.identify');
6533
+ console.error('Unique user id has not been set in posthog.identify');
6491
6534
  return;
6492
6535
  }
6493
6536
 
@@ -6557,7 +6600,7 @@ PostHogLib.prototype.identify = function (new_distinct_id, userPropertiesToSet,
6557
6600
 
6558
6601
  PostHogLib.prototype.group = function (groupType, groupKey, groupPropertiesToSet) {
6559
6602
  if (!groupType || !groupKey) {
6560
- console$1.error('posthog.group requires a group type and group key');
6603
+ console.error('posthog.group requires a group type and group key');
6561
6604
  return;
6562
6605
  }
6563
6606
 
@@ -6651,7 +6694,7 @@ PostHogLib.prototype.alias = function (alias, original) {
6651
6694
  // posthog.people.identify() call made for this user. It is VERY BAD to make an alias with
6652
6695
  // this ID, as it will duplicate users.
6653
6696
  if (alias === this.get_property(PEOPLE_DISTINCT_ID_KEY)) {
6654
- console$1.critical('Attempting to create alias for existing People user - aborting.');
6697
+ console.critical('Attempting to create alias for existing People user - aborting.');
6655
6698
  return -2;
6656
6699
  }
6657
6700
 
@@ -6672,7 +6715,7 @@ PostHogLib.prototype.alias = function (alias, original) {
6672
6715
  _this.identify(alias);
6673
6716
  });
6674
6717
  } else {
6675
- console$1.error('alias matches current distinct_id - skipping api call.');
6718
+ console.error('alias matches current distinct_id - skipping api call.');
6676
6719
  this.identify(alias);
6677
6720
  return -1;
6678
6721
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "posthog-js",
3
- "version": "1.17.8",
3
+ "version": "1.19.0",
4
4
  "description": "Posthog-js allows you to automatically capture usage and send events to PostHog.",
5
5
  "repository": "https://github.com/PostHog/posthog-js",
6
6
  "author": "hey@posthog.com",
@@ -50,7 +50,7 @@
50
50
  "fast-check": "^2.17.0",
51
51
  "given2": "^2.1.7",
52
52
  "husky": "^4.2.5",
53
- "jest": "^26.1.0",
53
+ "jest": "^27.5.1",
54
54
  "jsdom": "16.2.2",
55
55
  "jsdom-global": "3.0.2",
56
56
  "lint-staged": "^10.2.11",
@@ -60,7 +60,7 @@
60
60
  "posthog-js": "link:.",
61
61
  "prettier": "^2.0.5",
62
62
  "rollup": "^2.18.2",
63
- "rrweb": "^1.0.6",
63
+ "rrweb": "^1.1.2",
64
64
  "sinon": "9.0.2",
65
65
  "testcafe": "^1.17.1",
66
66
  "testcafe-browser-provider-browserstack": "^1.13.2-alpha.1",
@@ -92,6 +92,7 @@
92
92
  "setupFilesAfterEnv": [
93
93
  "given2/setup"
94
94
  ],
95
- "clearMocks": true
95
+ "clearMocks": true,
96
+ "testEnvironment": "jsdom"
96
97
  }
97
98
  }