mixpanel-browser 2.72.0 → 2.73.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.
@@ -18,7 +18,9 @@
18
18
  screen: { width: 0, height: 0 },
19
19
  location: loc,
20
20
  addEventListener: function() {},
21
- removeEventListener: function() {}
21
+ removeEventListener: function() {},
22
+ dispatchEvent: function() {},
23
+ CustomEvent: function () {}
22
24
  };
23
25
  } else {
24
26
  win = window;
@@ -14533,7 +14535,7 @@
14533
14535
 
14534
14536
  var Config = {
14535
14537
  DEBUG: false,
14536
- LIB_VERSION: '2.72.0'
14538
+ LIB_VERSION: '2.73.0'
14537
14539
  };
14538
14540
 
14539
14541
  /* eslint camelcase: "off", eqeqeq: "off" */
@@ -21801,8 +21803,6 @@
21801
21803
  var INIT_MODULE = 0;
21802
21804
  var INIT_SNIPPET = 1;
21803
21805
 
21804
- var IDENTITY_FUNC = function(x) {return x;};
21805
-
21806
21806
  /** @const */ var PRIMARY_INSTANCE_NAME = 'mixpanel';
21807
21807
  /** @const */ var PAYLOAD_TYPE_BASE64 = 'base64';
21808
21808
  /** @const */ var PAYLOAD_TYPE_JSON = 'json';
@@ -21968,6 +21968,17 @@
21968
21968
  // global debug to be true
21969
21969
  Config.DEBUG = Config.DEBUG || instance.get_config('debug');
21970
21970
 
21971
+ var source = init_type === INIT_MODULE ? 'module' : 'snippet';
21972
+ win.dispatchEvent(new win.CustomEvent('$mp_sdk_to_extension_event', {
21973
+ 'detail': {
21974
+ 'instance': instance,
21975
+ 'source': source,
21976
+ 'token': token,
21977
+ 'name': name,
21978
+ 'info': _.info
21979
+ }
21980
+ }));
21981
+
21971
21982
  // if target is not defined, we called init after the lib already
21972
21983
  // loaded, so there won't be an array of things to execute
21973
21984
  if (!_.isUndefined(target) && _.isArray(target)) {
@@ -22038,6 +22049,8 @@
22038
22049
  }
22039
22050
  }
22040
22051
 
22052
+ this.hooks = {};
22053
+
22041
22054
  this.set_config(_.extend({}, DEFAULT_CONFIG, variable_features, config, {
22042
22055
  'name': name,
22043
22056
  'token': token,
@@ -22638,7 +22651,12 @@
22638
22651
  );
22639
22652
  }, this),
22640
22653
  beforeSendHook: _.bind(function(item) {
22641
- return this._run_hook('before_send_' + attrs.type, item);
22654
+ var ret = this._run_hook('before_send_' + attrs.type, item);
22655
+ if (ret) {
22656
+ return ret[0];
22657
+ } else {
22658
+ return null;
22659
+ }
22642
22660
  }, this),
22643
22661
  stopAllBatchingFunc: _.bind(this.stop_batch_senders, this),
22644
22662
  usePersistence: true,
@@ -22731,6 +22749,9 @@
22731
22749
  var send_request_immediately = _.bind(function() {
22732
22750
  if (!send_request_options.skip_hooks) {
22733
22751
  truncated_data = this._run_hook('before_send_' + options.type, truncated_data);
22752
+ if (truncated_data) {
22753
+ truncated_data = truncated_data[0];
22754
+ }
22734
22755
  }
22735
22756
  if (truncated_data) {
22736
22757
  console$1.log('MIXPANEL REQUEST:');
@@ -22785,6 +22806,17 @@
22785
22806
  * with the tracking payload sent to the API server is returned; otherwise false.
22786
22807
  */
22787
22808
  MixpanelLib.prototype.track = addOptOutCheckMixpanelLib(function(event_name, properties, options, callback) {
22809
+ var ret;
22810
+ if (!(options && options.skip_hooks)) {
22811
+ ret = this._run_hook('before_track', event_name, properties);
22812
+ if (ret === null) {
22813
+ return;
22814
+ } else {
22815
+ event_name = ret[0];
22816
+ properties = ret[1];
22817
+ }
22818
+ }
22819
+
22788
22820
  if (!callback && typeof options === 'function') {
22789
22821
  callback = options;
22790
22822
  options = null;
@@ -22854,7 +22886,7 @@
22854
22886
  'event': event_name,
22855
22887
  'properties': properties
22856
22888
  };
22857
- var ret = this._track_or_batch({
22889
+ ret = this._track_or_batch({
22858
22890
  type: 'events',
22859
22891
  data: data,
22860
22892
  endpoint: this.get_api_host('events') + '/' + this.get_config('api_routes')['track'],
@@ -23200,6 +23232,14 @@
23200
23232
  * @param {boolean} [days_or_options.persistent=true] - whether to put in persistent storage (cookie/localStorage)
23201
23233
  */
23202
23234
  MixpanelLib.prototype.register = function(props, days_or_options) {
23235
+ var ret = this._run_hook('before_register', props, days_or_options);
23236
+ if (ret === null) {
23237
+ return;
23238
+ } else {
23239
+ props = ret[0];
23240
+ days_or_options = ret[1];
23241
+ }
23242
+
23203
23243
  var options = options_for_register(days_or_options);
23204
23244
  if (options['persistent']) {
23205
23245
  this['persistence'].register(props, options['days']);
@@ -23236,6 +23276,15 @@
23236
23276
  * @param {boolean} [days_or_options.persistent=true] - whether to put in persistent storage (cookie/localStorage)
23237
23277
  */
23238
23278
  MixpanelLib.prototype.register_once = function(props, default_value, days_or_options) {
23279
+ var ret = this._run_hook('before_register_once', props, default_value, days_or_options);
23280
+ if (ret === null) {
23281
+ return;
23282
+ } else {
23283
+ props = ret[0];
23284
+ default_value = ret[1];
23285
+ days_or_options = ret[2];
23286
+ }
23287
+
23239
23288
  var options = options_for_register(days_or_options);
23240
23289
  if (options['persistent']) {
23241
23290
  this['persistence'].register_once(props, default_value, options['days']);
@@ -23259,6 +23308,14 @@
23259
23308
  * @param {boolean} [options.persistent=true] - whether to look in persistent storage (cookie/localStorage)
23260
23309
  */
23261
23310
  MixpanelLib.prototype.unregister = function(property, options) {
23311
+ var ret = this._run_hook('before_unregister', property, options);
23312
+ if (ret === null) {
23313
+ return;
23314
+ } else {
23315
+ property = ret[0];
23316
+ options = ret[1];
23317
+ }
23318
+
23262
23319
  options = options_for_register(options);
23263
23320
  if (options['persistent']) {
23264
23321
  this['persistence'].unregister(property);
@@ -23307,6 +23364,13 @@
23307
23364
  // _set_once_callback:function A callback to be run if and when the People set_once queue is flushed
23308
23365
  // _union_callback:function A callback to be run if and when the People union queue is flushed
23309
23366
  // _unset_callback:function A callback to be run if and when the People unset queue is flushed
23367
+ var ret = this._run_hook('before_identify', new_distinct_id);
23368
+
23369
+ if (ret === null) {
23370
+ return -1;
23371
+ } else {
23372
+ new_distinct_id = ret[0];
23373
+ }
23310
23374
 
23311
23375
  var previous_distinct_id = this.get_distinct_id();
23312
23376
  if (new_distinct_id && previous_distinct_id !== new_distinct_id) {
@@ -23631,6 +23695,25 @@
23631
23695
  if (('autocapture' in config || 'record_heatmap_data' in config) && this.autocapture) {
23632
23696
  this.autocapture.init();
23633
23697
  }
23698
+
23699
+ if (_.isObject(config['hooks'])) {
23700
+ this.hooks = {};
23701
+ _.each(config['hooks'], function(hook_value, hook_name) {
23702
+ if (_.isFunction(hook_value)) {
23703
+ this.hooks[hook_name] = [hook_value];
23704
+ } else if (_.isArray(hook_value)) {
23705
+ this.hooks[hook_name] = [];
23706
+ for (var i = 0; i < hook_value.length; i++) {
23707
+ if (!_.isFunction(hook_value[i])) {
23708
+ console$1.critical('Invalid hook added. Hook is not a function');
23709
+ }
23710
+ this.hooks[hook_name].push(hook_value[i]);
23711
+ }
23712
+ } else {
23713
+ console$1.critical('Invalid hooks added. Ensure that the hook values passed into config.hooks are functions or arrays of functions.');
23714
+ }
23715
+ }, this);
23716
+ }
23634
23717
  }
23635
23718
  };
23636
23719
 
@@ -23648,12 +23731,26 @@
23648
23731
  * @returns {any|null} return value of user-provided hook, or null if nothing was returned
23649
23732
  */
23650
23733
  MixpanelLib.prototype._run_hook = function(hook_name) {
23651
- var ret = (this['config']['hooks'][hook_name] || IDENTITY_FUNC).apply(this, slice.call(arguments, 1));
23652
- if (typeof ret === 'undefined') {
23653
- this.report_error(hook_name + ' hook did not return a value');
23654
- ret = null;
23655
- }
23656
- return ret;
23734
+ var hook_data = slice.call(arguments, 1);
23735
+ _.each(this.hooks[hook_name], function(hook) {
23736
+ if (hook_data === null) {
23737
+ return null;
23738
+ }
23739
+
23740
+ var ret = hook.apply(this, hook_data);
23741
+
23742
+ if (typeof ret === 'undefined') {
23743
+ this.report_error(hook_name + ' hook did not return a valid value');
23744
+ hook_data = null;
23745
+ } else {
23746
+ if (!_.isArray(ret)) {
23747
+ ret = [ret];
23748
+ }
23749
+ hook_data.splice.apply(hook_data, [0, ret.length].concat(ret));
23750
+ }
23751
+ }, this);
23752
+
23753
+ return hook_data;
23657
23754
  };
23658
23755
 
23659
23756
  /**
@@ -23964,6 +24061,25 @@
23964
24061
  }
23965
24062
  };
23966
24063
 
24064
+ MixpanelLib.prototype.add_hook = function(hook_name, hook_fn) {
24065
+ if (!this.hooks[hook_name]) {
24066
+ this.hooks[hook_name] = [];
24067
+ }
24068
+ this.hooks[hook_name].push(hook_fn);
24069
+ };
24070
+
24071
+ MixpanelLib.prototype.remove_hook = function(hook_name, hook_fn) {
24072
+ var fn_index;
24073
+ if (this.hooks[hook_name]) {
24074
+ fn_index = this.hooks[hook_name].indexOf(hook_fn);
24075
+ if (fn_index !== -1) {
24076
+ this.hooks[hook_name].splice(fn_index, 1);
24077
+ } else {
24078
+ console$1.log('remove_hook failed. Matching hook was not found');
24079
+ }
24080
+ }
24081
+ };
24082
+
23967
24083
  // EXPORTS (for closure compiler)
23968
24084
 
23969
24085
  // MixpanelLib Exports
@@ -23996,6 +24112,8 @@
23996
24112
  MixpanelLib.prototype['set_group'] = MixpanelLib.prototype.set_group;
23997
24113
  MixpanelLib.prototype['add_group'] = MixpanelLib.prototype.add_group;
23998
24114
  MixpanelLib.prototype['remove_group'] = MixpanelLib.prototype.remove_group;
24115
+ MixpanelLib.prototype['add_hook'] = MixpanelLib.prototype.add_hook;
24116
+ MixpanelLib.prototype['remove_hook'] = MixpanelLib.prototype.remove_hook;
23999
24117
  MixpanelLib.prototype['track_with_groups'] = MixpanelLib.prototype.track_with_groups;
24000
24118
  MixpanelLib.prototype['start_batch_senders'] = MixpanelLib.prototype.start_batch_senders;
24001
24119
  MixpanelLib.prototype['stop_batch_senders'] = MixpanelLib.prototype.stop_batch_senders;
@@ -2,7 +2,7 @@ export type Persistence = "cookie" | "localStorage";
2
2
 
3
3
  export type ApiPayloadFormat = "base64" | "json";
4
4
 
5
- export type PushItem = Array<string | Dict>;
5
+ export type PushItem = Array<string | Dict | ((this: Mixpanel) => void)>;
6
6
 
7
7
  export type Query = string | Element | Element[];
8
8
 
@@ -155,6 +155,11 @@ export interface FlagsConfig {
155
155
  context: Dict;
156
156
  }
157
157
 
158
+ export interface BeforeSendHookPayload {
159
+ event: string;
160
+ properties: Record<string, any>;
161
+ }
162
+
158
163
  export interface Config {
159
164
  api_host: string;
160
165
  api_routes: {
@@ -167,10 +172,13 @@ export interface Config {
167
172
  app_host: string;
168
173
  api_payload_format: ApiPayloadFormat;
169
174
  autotrack: boolean;
175
+ batch_autostart: boolean;
176
+ batch_requests: boolean;
170
177
  cdn: string;
171
178
  cookie_domain: string;
172
179
  cross_site_cookie: boolean;
173
180
  cross_subdomain_cookie: boolean;
181
+ error_reporter: (msg: string, err?: Error) => void;
174
182
  flags: boolean | FlagsConfig;
175
183
  persistence: Persistence;
176
184
  persistence_name: string;
@@ -207,10 +215,10 @@ export interface Config {
207
215
  inapp_protocol: string;
208
216
  inapp_link_new_window: boolean;
209
217
  ignore_dnt: boolean;
210
- batch_requests: boolean;
211
218
  batch_size: number;
212
219
  batch_flush_interval_ms: number;
213
220
  batch_request_timeout_ms: number;
221
+ recorder_src: string;
214
222
  record_block_class: string | RegExp;
215
223
  record_block_selector: string;
216
224
  record_collect_fonts: boolean;
@@ -223,6 +231,29 @@ export interface Config {
223
231
  record_sessions_percent: number;
224
232
  record_canvas: boolean;
225
233
  record_heatmap_data: boolean;
234
+ hooks: {
235
+ before_identify?: (new_distinct_id: string) => string | null;
236
+ before_register?: (
237
+ props: Dict,
238
+ days_or_options?: number | Partial<RegisterOptions>
239
+ ) => Dict | Array<Dict | number | Partial<RegisterOptions>> | null;
240
+ before_register_once?: (
241
+ props: Dict,
242
+ default_value?: any,
243
+ days_or_options?: number | Partial<RegisterOptions>
244
+ ) => Dict | Array<any | Dict | number | Partial<RegisterOptions>> | null;
245
+ before_send_events?: (
246
+ event: BeforeSendHookPayload
247
+ ) => BeforeSendHookPayload | null;
248
+ before_track?: (
249
+ event_name: string,
250
+ properties: Dict
251
+ ) => string | Array<string | Dict> | null;
252
+ before_unregister?: (
253
+ property: string,
254
+ options?: Partial<RegisterOptions>
255
+ ) => string | Partial<RegisterOptions> | null;
256
+ };
226
257
  }
227
258
 
228
259
  export type VerboseResponse =
@@ -323,10 +354,11 @@ export interface Mixpanel {
323
354
  get_distinct_id(): any;
324
355
  get_group(group_key: string, group_id: string): Group;
325
356
  get_property(property_name: string): any;
357
+ get_session_replay_url(): string;
326
358
  has_opted_in_tracking(options?: Partial<HasOptedInOutOptions>): boolean;
327
359
  has_opted_out_tracking(options?: Partial<HasOptedInOutOptions>): boolean;
328
360
  identify(unique_id?: string): any;
329
- init(token: string, config: Partial<Config>, name: string): Mixpanel;
361
+ init(token: string, config: Partial<Config>, name?: string): Mixpanel;
330
362
  opt_in_tracking(options?: Partial<InTrackingOptions>): void;
331
363
  opt_out_tracking(options?: Partial<OutTrackingOptions>): void;
332
364
  push(item: PushItem): void;
@@ -351,6 +383,7 @@ export interface Mixpanel {
351
383
  group_ids: string | string[] | number | number[],
352
384
  callback?: Callback
353
385
  ): void;
386
+ start_batch_senders(): void;
354
387
  time_event(event_name: string): void;
355
388
  track(
356
389
  event_name: string,
@@ -380,6 +413,7 @@ export interface Mixpanel {
380
413
  ): void;
381
414
  unregister(property: string, options?: Partial<RegisterOptions>): void;
382
415
  people: People;
416
+ start_batch_senders(): void;
383
417
  start_session_recording(): void;
384
418
  stop_session_recording(): void;
385
419
  get_session_recording_properties(): { $mp_replay_id?: string } | {};