mixpanel-browser 2.66.0 → 2.68.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.68.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
@@ -2654,6 +2654,38 @@ function shouldTrackValue(value) {
2654
2654
  return true;
2655
2655
  }
2656
2656
 
2657
+ /** @const */ var DEFAULT_RAGE_CLICK_THRESHOLD_PX = 30;
2658
+ /** @const */ var DEFAULT_RAGE_CLICK_TIMEOUT_MS = 1000;
2659
+ /** @const */ var DEFAULT_RAGE_CLICK_CLICK_COUNT = 4;
2660
+
2661
+ function RageClickTracker() {
2662
+ this.clicks = [];
2663
+ }
2664
+
2665
+ RageClickTracker.prototype.isRageClick = function(x, y, options) {
2666
+ options = options || {};
2667
+ var thresholdPx = options['threshold_px'] || DEFAULT_RAGE_CLICK_THRESHOLD_PX;
2668
+ var timeoutMs = options['timeout_ms'] || DEFAULT_RAGE_CLICK_TIMEOUT_MS;
2669
+ var clickCount = options['click_count'] || DEFAULT_RAGE_CLICK_CLICK_COUNT;
2670
+ var timestamp = Date.now();
2671
+
2672
+ var lastClick = this.clicks[this.clicks.length - 1];
2673
+ if (
2674
+ lastClick &&
2675
+ timestamp - lastClick.timestamp < timeoutMs &&
2676
+ Math.sqrt(Math.pow(x - lastClick.x, 2) + Math.pow(y - lastClick.y, 2)) < thresholdPx
2677
+ ) {
2678
+ this.clicks.push({ x: x, y: y, timestamp: timestamp });
2679
+ if (this.clicks.length >= clickCount) {
2680
+ this.clicks = [];
2681
+ return true;
2682
+ }
2683
+ } else {
2684
+ this.clicks = [{ x: x, y: y, timestamp: timestamp }];
2685
+ }
2686
+ return false;
2687
+ };
2688
+
2657
2689
  var AUTOCAPTURE_CONFIG_KEY = 'autocapture';
2658
2690
  var LEGACY_PAGEVIEW_CONFIG_KEY = 'track_pageview';
2659
2691
 
@@ -2675,6 +2707,7 @@ var CONFIG_SCROLL_CHECKPOINTS = 'scroll_depth_percent_checkpoints';
2675
2707
  var CONFIG_TRACK_CLICK = 'click';
2676
2708
  var CONFIG_TRACK_INPUT = 'input';
2677
2709
  var CONFIG_TRACK_PAGEVIEW = 'pageview';
2710
+ var CONFIG_TRACK_RAGE_CLICK = 'rage_click';
2678
2711
  var CONFIG_TRACK_SCROLL = 'scroll';
2679
2712
  var CONFIG_TRACK_SUBMIT = 'submit';
2680
2713
 
@@ -2692,6 +2725,7 @@ CONFIG_DEFAULTS$1[CONFIG_SCROLL_CHECKPOINTS] = [25, 50, 75, 100];
2692
2725
  CONFIG_DEFAULTS$1[CONFIG_TRACK_CLICK] = true;
2693
2726
  CONFIG_DEFAULTS$1[CONFIG_TRACK_INPUT] = true;
2694
2727
  CONFIG_DEFAULTS$1[CONFIG_TRACK_PAGEVIEW] = PAGEVIEW_OPTION_FULL_URL;
2728
+ CONFIG_DEFAULTS$1[CONFIG_TRACK_RAGE_CLICK] = true;
2695
2729
  CONFIG_DEFAULTS$1[CONFIG_TRACK_SCROLL] = true;
2696
2730
  CONFIG_DEFAULTS$1[CONFIG_TRACK_SUBMIT] = true;
2697
2731
 
@@ -2701,6 +2735,7 @@ var DEFAULT_PROPS = {
2701
2735
 
2702
2736
  var MP_EV_CLICK = '$mp_click';
2703
2737
  var MP_EV_INPUT = '$mp_input_change';
2738
+ var MP_EV_RAGE_CLICK = '$mp_rage_click';
2704
2739
  var MP_EV_SCROLL = '$mp_scroll';
2705
2740
  var MP_EV_SUBMIT = '$mp_submit';
2706
2741
 
@@ -2723,6 +2758,7 @@ Autocapture.prototype.init = function() {
2723
2758
  this.initInputTracking();
2724
2759
  this.initScrollTracking();
2725
2760
  this.initSubmitTracking();
2761
+ this.initRageClickTracking();
2726
2762
  };
2727
2763
 
2728
2764
  Autocapture.prototype.getFullConfig = function() {
@@ -2801,6 +2837,11 @@ Autocapture.prototype.trackDomEvent = function(ev, mpEventName) {
2801
2837
  return;
2802
2838
  }
2803
2839
 
2840
+ var isCapturedForHeatMap = this.mp.is_recording_heatmap_data() && (
2841
+ (mpEventName === MP_EV_CLICK && !this.getConfig(CONFIG_TRACK_CLICK)) ||
2842
+ (mpEventName === MP_EV_RAGE_CLICK && !this._getRageClickConfig())
2843
+ );
2844
+
2804
2845
  var props = getPropsForDOMEvent(ev, {
2805
2846
  allowElementCallback: this.getConfig(CONFIG_ALLOW_ELEMENT_CALLBACK),
2806
2847
  allowSelectors: this.getConfig(CONFIG_ALLOW_SELECTORS),
@@ -2809,7 +2850,7 @@ Autocapture.prototype.trackDomEvent = function(ev, mpEventName) {
2809
2850
  blockSelectors: this.getConfig(CONFIG_BLOCK_SELECTORS),
2810
2851
  captureExtraAttrs: this.getConfig(CONFIG_CAPTURE_EXTRA_ATTRS),
2811
2852
  captureTextContent: this.getConfig(CONFIG_CAPTURE_TEXT_CONTENT),
2812
- capturedForHeatMap: mpEventName === MP_EV_CLICK && !this.getConfig(CONFIG_TRACK_CLICK) && this.mp.is_recording_heatmap_data(),
2853
+ capturedForHeatMap: isCapturedForHeatMap,
2813
2854
  });
2814
2855
  if (props) {
2815
2856
  _.extend(props, DEFAULT_PROPS);
@@ -2817,6 +2858,24 @@ Autocapture.prototype.trackDomEvent = function(ev, mpEventName) {
2817
2858
  }
2818
2859
  };
2819
2860
 
2861
+ Autocapture.prototype._getRageClickConfig = function() {
2862
+ var config = this.getConfig(CONFIG_TRACK_RAGE_CLICK);
2863
+
2864
+ if (!config) {
2865
+ return null; // rage click tracking disabled
2866
+ }
2867
+
2868
+ if (config === true) {
2869
+ return {}; // use defaults
2870
+ }
2871
+
2872
+ if (typeof config === 'object') {
2873
+ return config; // use custom configuration
2874
+ }
2875
+
2876
+ return {}; // fallback to defaults for any other truthy value
2877
+ };
2878
+
2820
2879
  Autocapture.prototype.initClickTracking = function() {
2821
2880
  win.removeEventListener(EV_CLICK, this.listenerClick);
2822
2881
 
@@ -2918,6 +2977,36 @@ Autocapture.prototype.initPageviewTracking = function() {
2918
2977
  }.bind(this)));
2919
2978
  };
2920
2979
 
2980
+ Autocapture.prototype.initRageClickTracking = function() {
2981
+ win.removeEventListener(EV_CLICK, this.listenerRageClick);
2982
+
2983
+ var rageClickConfig = this._getRageClickConfig();
2984
+ if (!rageClickConfig && !this.mp.get_config('record_heatmap_data')) {
2985
+ return;
2986
+ }
2987
+
2988
+ logger$4.log('Initializing rage click tracking');
2989
+ if (!this._rageClickTracker) {
2990
+ this._rageClickTracker = new RageClickTracker();
2991
+ }
2992
+
2993
+ this.listenerRageClick = function(ev) {
2994
+ var currentRageClickConfig = this._getRageClickConfig();
2995
+ if (!currentRageClickConfig && !this.mp.is_recording_heatmap_data()) {
2996
+ return;
2997
+ }
2998
+
2999
+ if (this.currentUrlBlocked()) {
3000
+ return;
3001
+ }
3002
+
3003
+ if (this._rageClickTracker.isRageClick(ev['pageX'], ev['pageY'], currentRageClickConfig)) {
3004
+ this.trackDomEvent(ev, MP_EV_RAGE_CLICK);
3005
+ }
3006
+ }.bind(this);
3007
+ win.addEventListener(EV_CLICK, this.listenerRageClick);
3008
+ };
3009
+
2921
3010
  Autocapture.prototype.initScrollTracking = function() {
2922
3011
  win.removeEventListener(EV_SCROLLEND, this.listenerScroll);
2923
3012
 
@@ -3002,8 +3091,10 @@ CONFIG_DEFAULTS[CONFIG_CONTEXT] = {};
3002
3091
  * @constructor
3003
3092
  */
3004
3093
  var FeatureFlagManager = function(initOptions) {
3094
+ this.getFullApiRoute = initOptions.getFullApiRoute;
3005
3095
  this.getMpConfig = initOptions.getConfigFunc;
3006
- this.getDistinctId = initOptions.getDistinctIdFunc;
3096
+ this.setMpConfig = initOptions.setConfigFunc;
3097
+ this.getMpProperty = initOptions.getPropertyFunc;
3007
3098
  this.track = initOptions.trackingFunc;
3008
3099
  };
3009
3100
 
@@ -3040,6 +3131,23 @@ FeatureFlagManager.prototype.isSystemEnabled = function() {
3040
3131
  return !!this.getMpConfig(FLAGS_CONFIG_KEY);
3041
3132
  };
3042
3133
 
3134
+ FeatureFlagManager.prototype.updateContext = function(newContext, options) {
3135
+ if (!this.isSystemEnabled()) {
3136
+ logger$3.critical('Feature Flags not enabled, cannot update context');
3137
+ return Promise.resolve();
3138
+ }
3139
+
3140
+ var ffConfig = this.getMpConfig(FLAGS_CONFIG_KEY);
3141
+ if (!_.isObject(ffConfig)) {
3142
+ ffConfig = {};
3143
+ }
3144
+ var oldContext = (options && options['replace']) ? {} : this.getConfig(CONFIG_CONTEXT);
3145
+ ffConfig[CONFIG_CONTEXT] = _.extend({}, oldContext, newContext);
3146
+
3147
+ this.setMpConfig(FLAGS_CONFIG_KEY, ffConfig);
3148
+ return this.fetchFlags();
3149
+ };
3150
+
3043
3151
  FeatureFlagManager.prototype.areFlagsReady = function() {
3044
3152
  if (!this.isSystemEnabled()) {
3045
3153
  logger$3.error('Feature Flags not enabled');
@@ -3049,15 +3157,17 @@ FeatureFlagManager.prototype.areFlagsReady = function() {
3049
3157
 
3050
3158
  FeatureFlagManager.prototype.fetchFlags = function() {
3051
3159
  if (!this.isSystemEnabled()) {
3052
- return;
3160
+ return Promise.resolve();
3053
3161
  }
3054
3162
 
3055
- var distinctId = this.getDistinctId();
3163
+ var distinctId = this.getMpProperty('distinct_id');
3164
+ var deviceId = this.getMpProperty('$device_id');
3056
3165
  logger$3.log('Fetching flags for distinct ID: ' + distinctId);
3057
3166
  var reqParams = {
3058
- 'context': _.extend({'distinct_id': distinctId}, this.getConfig(CONFIG_CONTEXT))
3167
+ 'context': _.extend({'distinct_id': distinctId, 'device_id': deviceId}, this.getConfig(CONFIG_CONTEXT))
3059
3168
  };
3060
- this.fetchPromise = win['fetch'](this.getMpConfig('api_host') + '/' + this.getMpConfig('api_routes')['flags'], {
3169
+ this._fetchInProgressStartTime = Date.now();
3170
+ this.fetchPromise = win['fetch'](this.getFullApiRoute(), {
3061
3171
  'method': 'POST',
3062
3172
  'headers': {
3063
3173
  'Authorization': 'Basic ' + btoa(this.getMpConfig('token') + ':'),
@@ -3065,6 +3175,7 @@ FeatureFlagManager.prototype.fetchFlags = function() {
3065
3175
  },
3066
3176
  'body': JSON.stringify(reqParams)
3067
3177
  }).then(function(response) {
3178
+ this.markFetchComplete();
3068
3179
  return response.json().then(function(responseBody) {
3069
3180
  var responseFlags = responseBody['flags'];
3070
3181
  if (!responseFlags) {
@@ -3079,9 +3190,26 @@ FeatureFlagManager.prototype.fetchFlags = function() {
3079
3190
  });
3080
3191
  this.flags = flags;
3081
3192
  }.bind(this)).catch(function(error) {
3193
+ this.markFetchComplete();
3082
3194
  logger$3.error(error);
3083
- });
3084
- }.bind(this)).catch(function() {});
3195
+ }.bind(this));
3196
+ }.bind(this)).catch(function(error) {
3197
+ this.markFetchComplete();
3198
+ logger$3.error(error);
3199
+ }.bind(this));
3200
+
3201
+ return this.fetchPromise;
3202
+ };
3203
+
3204
+ FeatureFlagManager.prototype.markFetchComplete = function() {
3205
+ if (!this._fetchInProgressStartTime) {
3206
+ logger$3.error('Fetch in progress started time not set, cannot mark fetch complete');
3207
+ return;
3208
+ }
3209
+ this._fetchStartTime = this._fetchInProgressStartTime;
3210
+ this._fetchCompleteTime = Date.now();
3211
+ this._fetchLatency = this._fetchCompleteTime - this._fetchStartTime;
3212
+ this._fetchInProgressStartTime = null;
3085
3213
  };
3086
3214
 
3087
3215
  FeatureFlagManager.prototype.getVariant = function(featureName, fallback) {
@@ -3160,7 +3288,10 @@ FeatureFlagManager.prototype.trackFeatureCheck = function(featureName, feature)
3160
3288
  this.track('$experiment_started', {
3161
3289
  'Experiment name': featureName,
3162
3290
  'Variant name': feature['key'],
3163
- '$experiment_type': 'feature_flag'
3291
+ '$experiment_type': 'feature_flag',
3292
+ 'Variant fetch start time': new Date(this._fetchStartTime).toISOString(),
3293
+ 'Variant fetch complete time': new Date(this._fetchCompleteTime).toISOString(),
3294
+ 'Variant fetch latency (ms)': this._fetchLatency
3164
3295
  });
3165
3296
  };
3166
3297
 
@@ -3180,6 +3311,7 @@ FeatureFlagManager.prototype['get_variant_value'] = FeatureFlagManager.prototype
3180
3311
  FeatureFlagManager.prototype['get_variant_value_sync'] = FeatureFlagManager.prototype.getVariantValueSync;
3181
3312
  FeatureFlagManager.prototype['is_enabled'] = FeatureFlagManager.prototype.isEnabled;
3182
3313
  FeatureFlagManager.prototype['is_enabled_sync'] = FeatureFlagManager.prototype.isEnabledSync;
3314
+ FeatureFlagManager.prototype['update_context'] = FeatureFlagManager.prototype.updateContext;
3183
3315
 
3184
3316
  // Deprecated method
3185
3317
  FeatureFlagManager.prototype['get_feature_data'] = FeatureFlagManager.prototype.getFeatureData;
@@ -5951,7 +6083,7 @@ var DEFAULT_CONFIG = {
5951
6083
  'batch_autostart': true,
5952
6084
  'hooks': {},
5953
6085
  'record_block_class': new RegExp('^(mp-block|fs-exclude|amp-block|rr-block|ph-no-capture)$'),
5954
- 'record_block_selector': 'img, video',
6086
+ 'record_block_selector': 'img, video, audio',
5955
6087
  'record_canvas': false,
5956
6088
  'record_collect_fonts': false,
5957
6089
  'record_heatmap_data': false,
@@ -6171,8 +6303,12 @@ MixpanelLib.prototype._init = function(token, config, name) {
6171
6303
  }
6172
6304
 
6173
6305
  this.flags = new FeatureFlagManager({
6306
+ getFullApiRoute: _.bind(function() {
6307
+ return this.get_api_host('flags') + '/' + this.get_config('api_routes')['flags'];
6308
+ }, this),
6174
6309
  getConfigFunc: _.bind(this.get_config, this),
6175
- getDistinctIdFunc: _.bind(this.get_distinct_id, this),
6310
+ setConfigFunc: _.bind(this.set_config, this),
6311
+ getPropertyFunc: _.bind(this.get_property, this),
6176
6312
  trackingFunc: _.bind(this.track, this)
6177
6313
  });
6178
6314
  this.flags.init();
@@ -6658,11 +6794,10 @@ MixpanelLib.prototype.are_batchers_initialized = function() {
6658
6794
 
6659
6795
  MixpanelLib.prototype.get_batcher_configs = function() {
6660
6796
  var queue_prefix = '__mpq_' + this.get_config('token');
6661
- var api_routes = this.get_config('api_routes');
6662
6797
  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'}
6798
+ events: {type: 'events', api_name: 'track', queue_key: queue_prefix + '_ev'},
6799
+ people: {type: 'people', api_name: 'engage', queue_key: queue_prefix + '_pp'},
6800
+ groups: {type: 'groups', api_name: 'groups', queue_key: queue_prefix + '_gr'}
6666
6801
  };
6667
6802
  return this._batcher_configs;
6668
6803
  };
@@ -6676,8 +6811,9 @@ MixpanelLib.prototype.init_batchers = function() {
6676
6811
  libConfig: this['config'],
6677
6812
  errorReporter: this.get_config('error_reporter'),
6678
6813
  sendRequestFunc: _.bind(function(data, options, cb) {
6814
+ var api_routes = this.get_config('api_routes');
6679
6815
  this._send_request(
6680
- this.get_config('api_host') + attrs.endpoint,
6816
+ this.get_api_host(attrs.api_name) + '/' + api_routes[attrs.api_name],
6681
6817
  this._encode_data_for_request(data),
6682
6818
  options,
6683
6819
  this._prepare_callback(cb, data)
@@ -7405,31 +7541,15 @@ MixpanelLib.prototype.identify = function(
7405
7541
  * Useful for clearing data when a user logs out.
7406
7542
  */
7407
7543
  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
- }
7544
+ this.stop_session_recording();
7545
+ this['persistence'].clear();
7546
+ this._flags.identify_called = false;
7547
+ var uuid = _.UUID();
7548
+ this.register_once({
7549
+ 'distinct_id': DEVICE_ID_PREFIX + uuid,
7550
+ '$device_id': uuid
7551
+ }, '');
7552
+ this._check_and_start_session_recording();
7433
7553
  };
7434
7554
 
7435
7555
  /**