mixpanel-browser 2.66.0 → 2.67.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.
@@ -2,7 +2,7 @@
2
2
 
3
3
  var Config = {
4
4
  DEBUG: false,
5
- LIB_VERSION: '2.66.0'
5
+ LIB_VERSION: '2.67.0'
6
6
  };
7
7
 
8
8
  // since es6 imports are static and we run unit tests from the console, window won't be defined when importing this file
@@ -3002,8 +3002,9 @@ CONFIG_DEFAULTS[CONFIG_CONTEXT] = {};
3002
3002
  * @constructor
3003
3003
  */
3004
3004
  var FeatureFlagManager = function(initOptions) {
3005
+ this.getFullApiRoute = initOptions.getFullApiRoute;
3005
3006
  this.getMpConfig = initOptions.getConfigFunc;
3006
- this.getDistinctId = initOptions.getDistinctIdFunc;
3007
+ this.getMpProperty = initOptions.getPropertyFunc;
3007
3008
  this.track = initOptions.trackingFunc;
3008
3009
  };
3009
3010
 
@@ -3052,12 +3053,14 @@ FeatureFlagManager.prototype.fetchFlags = function() {
3052
3053
  return;
3053
3054
  }
3054
3055
 
3055
- var distinctId = this.getDistinctId();
3056
+ var distinctId = this.getMpProperty('distinct_id');
3057
+ var deviceId = this.getMpProperty('$device_id');
3056
3058
  logger$3.log('Fetching flags for distinct ID: ' + distinctId);
3057
3059
  var reqParams = {
3058
- 'context': _.extend({'distinct_id': distinctId}, this.getConfig(CONFIG_CONTEXT))
3060
+ 'context': _.extend({'distinct_id': distinctId, 'device_id': deviceId}, this.getConfig(CONFIG_CONTEXT))
3059
3061
  };
3060
- this.fetchPromise = win['fetch'](this.getMpConfig('api_host') + '/' + this.getMpConfig('api_routes')['flags'], {
3062
+ this._fetchInProgressStartTime = Date.now();
3063
+ this.fetchPromise = win['fetch'](this.getFullApiRoute(), {
3061
3064
  'method': 'POST',
3062
3065
  'headers': {
3063
3066
  'Authorization': 'Basic ' + btoa(this.getMpConfig('token') + ':'),
@@ -3065,6 +3068,7 @@ FeatureFlagManager.prototype.fetchFlags = function() {
3065
3068
  },
3066
3069
  'body': JSON.stringify(reqParams)
3067
3070
  }).then(function(response) {
3071
+ this.markFetchComplete();
3068
3072
  return response.json().then(function(responseBody) {
3069
3073
  var responseFlags = responseBody['flags'];
3070
3074
  if (!responseFlags) {
@@ -3079,9 +3083,24 @@ FeatureFlagManager.prototype.fetchFlags = function() {
3079
3083
  });
3080
3084
  this.flags = flags;
3081
3085
  }.bind(this)).catch(function(error) {
3086
+ this.markFetchComplete();
3082
3087
  logger$3.error(error);
3083
- });
3084
- }.bind(this)).catch(function() {});
3088
+ }.bind(this));
3089
+ }.bind(this)).catch(function(error) {
3090
+ this.markFetchComplete();
3091
+ logger$3.error(error);
3092
+ }.bind(this));
3093
+ };
3094
+
3095
+ FeatureFlagManager.prototype.markFetchComplete = function() {
3096
+ if (!this._fetchInProgressStartTime) {
3097
+ logger$3.error('Fetch in progress started time not set, cannot mark fetch complete');
3098
+ return;
3099
+ }
3100
+ this._fetchStartTime = this._fetchInProgressStartTime;
3101
+ this._fetchCompleteTime = Date.now();
3102
+ this._fetchLatency = this._fetchCompleteTime - this._fetchStartTime;
3103
+ this._fetchInProgressStartTime = null;
3085
3104
  };
3086
3105
 
3087
3106
  FeatureFlagManager.prototype.getVariant = function(featureName, fallback) {
@@ -3160,7 +3179,10 @@ FeatureFlagManager.prototype.trackFeatureCheck = function(featureName, feature)
3160
3179
  this.track('$experiment_started', {
3161
3180
  'Experiment name': featureName,
3162
3181
  'Variant name': feature['key'],
3163
- '$experiment_type': 'feature_flag'
3182
+ '$experiment_type': 'feature_flag',
3183
+ 'Variant fetch start time': new Date(this._fetchStartTime).toISOString(),
3184
+ 'Variant fetch complete time': new Date(this._fetchCompleteTime).toISOString(),
3185
+ 'Variant fetch latency (ms)': this._fetchLatency
3164
3186
  });
3165
3187
  };
3166
3188
 
@@ -6171,8 +6193,11 @@ MixpanelLib.prototype._init = function(token, config, name) {
6171
6193
  }
6172
6194
 
6173
6195
  this.flags = new FeatureFlagManager({
6196
+ getFullApiRoute: _.bind(function() {
6197
+ return this.get_api_host('flags') + '/' + this.get_config('api_routes')['flags'];
6198
+ }, this),
6174
6199
  getConfigFunc: _.bind(this.get_config, this),
6175
- getDistinctIdFunc: _.bind(this.get_distinct_id, this),
6200
+ getPropertyFunc: _.bind(this.get_property, this),
6176
6201
  trackingFunc: _.bind(this.track, this)
6177
6202
  });
6178
6203
  this.flags.init();
@@ -6658,11 +6683,10 @@ MixpanelLib.prototype.are_batchers_initialized = function() {
6658
6683
 
6659
6684
  MixpanelLib.prototype.get_batcher_configs = function() {
6660
6685
  var queue_prefix = '__mpq_' + this.get_config('token');
6661
- var api_routes = this.get_config('api_routes');
6662
6686
  this._batcher_configs = this._batcher_configs || {
6663
- events: {type: 'events', endpoint: '/' + api_routes['track'], queue_key: queue_prefix + '_ev'},
6664
- people: {type: 'people', endpoint: '/' + api_routes['engage'], queue_key: queue_prefix + '_pp'},
6665
- groups: {type: 'groups', endpoint: '/' + api_routes['groups'], queue_key: queue_prefix + '_gr'}
6687
+ events: {type: 'events', api_name: 'track', queue_key: queue_prefix + '_ev'},
6688
+ people: {type: 'people', api_name: 'engage', queue_key: queue_prefix + '_pp'},
6689
+ groups: {type: 'groups', api_name: 'groups', queue_key: queue_prefix + '_gr'}
6666
6690
  };
6667
6691
  return this._batcher_configs;
6668
6692
  };
@@ -6676,8 +6700,9 @@ MixpanelLib.prototype.init_batchers = function() {
6676
6700
  libConfig: this['config'],
6677
6701
  errorReporter: this.get_config('error_reporter'),
6678
6702
  sendRequestFunc: _.bind(function(data, options, cb) {
6703
+ var api_routes = this.get_config('api_routes');
6679
6704
  this._send_request(
6680
- this.get_config('api_host') + attrs.endpoint,
6705
+ this.get_api_host(attrs.api_name) + '/' + api_routes[attrs.api_name],
6681
6706
  this._encode_data_for_request(data),
6682
6707
  options,
6683
6708
  this._prepare_callback(cb, data)
@@ -7405,31 +7430,15 @@ MixpanelLib.prototype.identify = function(
7405
7430
  * Useful for clearing data when a user logs out.
7406
7431
  */
7407
7432
  MixpanelLib.prototype.reset = function() {
7408
- var self = this;
7409
-
7410
- var reset = function () {
7411
- self['persistence'].clear();
7412
- self._flags.identify_called = false;
7413
- var uuid = _.UUID();
7414
- self.register_once({
7415
- 'distinct_id': DEVICE_ID_PREFIX + uuid,
7416
- '$device_id': uuid
7417
- }, '');
7418
- };
7419
-
7420
- if (self._recorder) {
7421
- self.stop_session_recording()
7422
- .then(function () {
7423
- reset();
7424
- self._check_and_start_session_recording();
7425
- })
7426
- .catch(_.bind(function (err) {
7427
- reset();
7428
- this.report_error('Error restarting recording session', err);
7429
- }, this));
7430
- } else {
7431
- reset();
7432
- }
7433
+ this.stop_session_recording();
7434
+ this['persistence'].clear();
7435
+ this._flags.identify_called = false;
7436
+ var uuid = _.UUID();
7437
+ this.register_once({
7438
+ 'distinct_id': DEVICE_ID_PREFIX + uuid,
7439
+ '$device_id': uuid
7440
+ }, '');
7441
+ this._check_and_start_session_recording();
7433
7442
  };
7434
7443
 
7435
7444
  /**
@@ -13945,7 +13945,7 @@
13945
13945
 
13946
13946
  var Config = {
13947
13947
  DEBUG: false,
13948
- LIB_VERSION: '2.66.0'
13948
+ LIB_VERSION: '2.67.0'
13949
13949
  };
13950
13950
 
13951
13951
  /* eslint camelcase: "off", eqeqeq: "off" */
@@ -17063,6 +17063,13 @@
17063
17063
  * @property {string} replayStartUrl
17064
17064
  */
17065
17065
 
17066
+ /**
17067
+ * @typedef {Object} UserIdInfo
17068
+ * @property {string} distinct_id
17069
+ * @property {string} user_id
17070
+ * @property {string} device_id
17071
+ */
17072
+
17066
17073
 
17067
17074
  /**
17068
17075
  * This class encapsulates a single session recording and its lifecycle.
@@ -17118,6 +17125,30 @@
17118
17125
  });
17119
17126
  };
17120
17127
 
17128
+ /**
17129
+ * @returns {UserIdInfo}
17130
+ */
17131
+ SessionRecording.prototype.getUserIdInfo = function () {
17132
+ if (this.finalFlushUserIdInfo) {
17133
+ return this.finalFlushUserIdInfo;
17134
+ }
17135
+
17136
+ var userIdInfo = {
17137
+ 'distinct_id': String(this._mixpanel.get_distinct_id()),
17138
+ };
17139
+
17140
+ // send ID management props if they exist
17141
+ var deviceId = this._mixpanel.get_property('$device_id');
17142
+ if (deviceId) {
17143
+ userIdInfo['$device_id'] = deviceId;
17144
+ }
17145
+ var userId = this._mixpanel.get_property('$user_id');
17146
+ if (userId) {
17147
+ userIdInfo['$user_id'] = userId;
17148
+ }
17149
+ return userIdInfo;
17150
+ };
17151
+
17121
17152
  SessionRecording.prototype.unloadPersistedData = function () {
17122
17153
  this.batcher.stop();
17123
17154
  return this.batcher.flush()
@@ -17242,6 +17273,9 @@
17242
17273
  };
17243
17274
 
17244
17275
  SessionRecording.prototype.stopRecording = function (skipFlush) {
17276
+ // store the user ID info in case this is getting called in mixpanel.reset()
17277
+ this.finalFlushUserIdInfo = this.getUserIdInfo();
17278
+
17245
17279
  if (!this.isRrwebStopped()) {
17246
17280
  try {
17247
17281
  this._stopRecording();
@@ -17407,7 +17441,6 @@
17407
17441
  '$current_url': this.batchStartUrl,
17408
17442
  '$lib_version': Config.LIB_VERSION,
17409
17443
  'batch_start_time': batchStartTime / 1000,
17410
- 'distinct_id': String(this._mixpanel.get_distinct_id()),
17411
17444
  'mp_lib': 'web',
17412
17445
  'replay_id': replayId,
17413
17446
  'replay_length_ms': replayLengthMs,
@@ -17416,16 +17449,7 @@
17416
17449
  'seq': this.seqNo
17417
17450
  };
17418
17451
  var eventsJson = JSON.stringify(data);
17419
-
17420
- // send ID management props if they exist
17421
- var deviceId = this._mixpanel.get_property('$device_id');
17422
- if (deviceId) {
17423
- reqParams['$device_id'] = deviceId;
17424
- }
17425
- var userId = this._mixpanel.get_property('$user_id');
17426
- if (userId) {
17427
- reqParams['$user_id'] = userId;
17428
- }
17452
+ Object.assign(reqParams, this.getUserIdInfo());
17429
17453
 
17430
17454
  if (CompressionStream) {
17431
17455
  var jsonStream = new Blob([eventsJson], {type: 'application/json'}).stream();
@@ -18554,8 +18578,9 @@
18554
18578
  * @constructor
18555
18579
  */
18556
18580
  var FeatureFlagManager = function(initOptions) {
18581
+ this.getFullApiRoute = initOptions.getFullApiRoute;
18557
18582
  this.getMpConfig = initOptions.getConfigFunc;
18558
- this.getDistinctId = initOptions.getDistinctIdFunc;
18583
+ this.getMpProperty = initOptions.getPropertyFunc;
18559
18584
  this.track = initOptions.trackingFunc;
18560
18585
  };
18561
18586
 
@@ -18604,12 +18629,14 @@
18604
18629
  return;
18605
18630
  }
18606
18631
 
18607
- var distinctId = this.getDistinctId();
18632
+ var distinctId = this.getMpProperty('distinct_id');
18633
+ var deviceId = this.getMpProperty('$device_id');
18608
18634
  logger.log('Fetching flags for distinct ID: ' + distinctId);
18609
18635
  var reqParams = {
18610
- 'context': _.extend({'distinct_id': distinctId}, this.getConfig(CONFIG_CONTEXT))
18636
+ 'context': _.extend({'distinct_id': distinctId, 'device_id': deviceId}, this.getConfig(CONFIG_CONTEXT))
18611
18637
  };
18612
- this.fetchPromise = win['fetch'](this.getMpConfig('api_host') + '/' + this.getMpConfig('api_routes')['flags'], {
18638
+ this._fetchInProgressStartTime = Date.now();
18639
+ this.fetchPromise = win['fetch'](this.getFullApiRoute(), {
18613
18640
  'method': 'POST',
18614
18641
  'headers': {
18615
18642
  'Authorization': 'Basic ' + btoa(this.getMpConfig('token') + ':'),
@@ -18617,6 +18644,7 @@
18617
18644
  },
18618
18645
  'body': JSON.stringify(reqParams)
18619
18646
  }).then(function(response) {
18647
+ this.markFetchComplete();
18620
18648
  return response.json().then(function(responseBody) {
18621
18649
  var responseFlags = responseBody['flags'];
18622
18650
  if (!responseFlags) {
@@ -18631,9 +18659,24 @@
18631
18659
  });
18632
18660
  this.flags = flags;
18633
18661
  }.bind(this)).catch(function(error) {
18662
+ this.markFetchComplete();
18634
18663
  logger.error(error);
18635
- });
18636
- }.bind(this)).catch(function() {});
18664
+ }.bind(this));
18665
+ }.bind(this)).catch(function(error) {
18666
+ this.markFetchComplete();
18667
+ logger.error(error);
18668
+ }.bind(this));
18669
+ };
18670
+
18671
+ FeatureFlagManager.prototype.markFetchComplete = function() {
18672
+ if (!this._fetchInProgressStartTime) {
18673
+ logger.error('Fetch in progress started time not set, cannot mark fetch complete');
18674
+ return;
18675
+ }
18676
+ this._fetchStartTime = this._fetchInProgressStartTime;
18677
+ this._fetchCompleteTime = Date.now();
18678
+ this._fetchLatency = this._fetchCompleteTime - this._fetchStartTime;
18679
+ this._fetchInProgressStartTime = null;
18637
18680
  };
18638
18681
 
18639
18682
  FeatureFlagManager.prototype.getVariant = function(featureName, fallback) {
@@ -18712,7 +18755,10 @@
18712
18755
  this.track('$experiment_started', {
18713
18756
  'Experiment name': featureName,
18714
18757
  'Variant name': feature['key'],
18715
- '$experiment_type': 'feature_flag'
18758
+ '$experiment_type': 'feature_flag',
18759
+ 'Variant fetch start time': new Date(this._fetchStartTime).toISOString(),
18760
+ 'Variant fetch complete time': new Date(this._fetchCompleteTime).toISOString(),
18761
+ 'Variant fetch latency (ms)': this._fetchLatency
18716
18762
  });
18717
18763
  };
18718
18764
 
@@ -20412,8 +20458,11 @@
20412
20458
  }
20413
20459
 
20414
20460
  this.flags = new FeatureFlagManager({
20461
+ getFullApiRoute: _.bind(function() {
20462
+ return this.get_api_host('flags') + '/' + this.get_config('api_routes')['flags'];
20463
+ }, this),
20415
20464
  getConfigFunc: _.bind(this.get_config, this),
20416
- getDistinctIdFunc: _.bind(this.get_distinct_id, this),
20465
+ getPropertyFunc: _.bind(this.get_property, this),
20417
20466
  trackingFunc: _.bind(this.track, this)
20418
20467
  });
20419
20468
  this.flags.init();
@@ -20899,11 +20948,10 @@
20899
20948
 
20900
20949
  MixpanelLib.prototype.get_batcher_configs = function() {
20901
20950
  var queue_prefix = '__mpq_' + this.get_config('token');
20902
- var api_routes = this.get_config('api_routes');
20903
20951
  this._batcher_configs = this._batcher_configs || {
20904
- events: {type: 'events', endpoint: '/' + api_routes['track'], queue_key: queue_prefix + '_ev'},
20905
- people: {type: 'people', endpoint: '/' + api_routes['engage'], queue_key: queue_prefix + '_pp'},
20906
- groups: {type: 'groups', endpoint: '/' + api_routes['groups'], queue_key: queue_prefix + '_gr'}
20952
+ events: {type: 'events', api_name: 'track', queue_key: queue_prefix + '_ev'},
20953
+ people: {type: 'people', api_name: 'engage', queue_key: queue_prefix + '_pp'},
20954
+ groups: {type: 'groups', api_name: 'groups', queue_key: queue_prefix + '_gr'}
20907
20955
  };
20908
20956
  return this._batcher_configs;
20909
20957
  };
@@ -20917,8 +20965,9 @@
20917
20965
  libConfig: this['config'],
20918
20966
  errorReporter: this.get_config('error_reporter'),
20919
20967
  sendRequestFunc: _.bind(function(data, options, cb) {
20968
+ var api_routes = this.get_config('api_routes');
20920
20969
  this._send_request(
20921
- this.get_config('api_host') + attrs.endpoint,
20970
+ this.get_api_host(attrs.api_name) + '/' + api_routes[attrs.api_name],
20922
20971
  this._encode_data_for_request(data),
20923
20972
  options,
20924
20973
  this._prepare_callback(cb, data)
@@ -21646,31 +21695,15 @@
21646
21695
  * Useful for clearing data when a user logs out.
21647
21696
  */
21648
21697
  MixpanelLib.prototype.reset = function() {
21649
- var self = this;
21650
-
21651
- var reset = function () {
21652
- self['persistence'].clear();
21653
- self._flags.identify_called = false;
21654
- var uuid = _.UUID();
21655
- self.register_once({
21656
- 'distinct_id': DEVICE_ID_PREFIX + uuid,
21657
- '$device_id': uuid
21658
- }, '');
21659
- };
21660
-
21661
- if (self._recorder) {
21662
- self.stop_session_recording()
21663
- .then(function () {
21664
- reset();
21665
- self._check_and_start_session_recording();
21666
- })
21667
- .catch(_.bind(function (err) {
21668
- reset();
21669
- this.report_error('Error restarting recording session', err);
21670
- }, this));
21671
- } else {
21672
- reset();
21673
- }
21698
+ this.stop_session_recording();
21699
+ this['persistence'].clear();
21700
+ this._flags.identify_called = false;
21701
+ var uuid = _.UUID();
21702
+ this.register_once({
21703
+ 'distinct_id': DEVICE_ID_PREFIX + uuid,
21704
+ '$device_id': uuid
21705
+ }, '');
21706
+ this._check_and_start_session_recording();
21674
21707
  };
21675
21708
 
21676
21709
  /**