mixpanel-browser 2.46.0 → 2.47.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.
@@ -3,7 +3,7 @@
3
3
 
4
4
  var Config = {
5
5
  DEBUG: false,
6
- LIB_VERSION: '2.46.0'
6
+ LIB_VERSION: '2.47.0'
7
7
  };
8
8
 
9
9
  // since es6 imports are static and we run unit tests from the console, window won't be defined when importing this file
@@ -833,20 +833,24 @@
833
833
 
834
834
  _.UUID = (function() {
835
835
 
836
- // Time/ticks information
837
- // 1*new Date() is a cross browser version of Date.now()
836
+ // Time-based entropy
838
837
  var T = function() {
839
- var d = 1 * new Date(),
840
- i = 0;
841
-
842
- // this while loop figures how many browser ticks go by
843
- // before 1*new Date() returns a new number, ie the amount
844
- // of ticks that go by per millisecond
845
- while (d == 1 * new Date()) {
846
- i++;
838
+ var time = 1 * new Date(); // cross-browser version of Date.now()
839
+ var ticks;
840
+ if (window$1.performance && window$1.performance.now) {
841
+ ticks = window$1.performance.now();
842
+ } else {
843
+ // fall back to busy loop
844
+ ticks = 0;
845
+
846
+ // this while loop figures how many browser ticks go by
847
+ // before 1*new Date() returns a new number, ie the amount
848
+ // of ticks that go by per millisecond
849
+ while (time == 1 * new Date()) {
850
+ ticks++;
851
+ }
847
852
  }
848
-
849
- return d.toString(16) + i.toString(16);
853
+ return time.toString(16) + Math.floor(ticks).toString(16);
850
854
  };
851
855
 
852
856
  // Math.Random entropy
@@ -1413,21 +1417,42 @@
1413
1417
  };
1414
1418
  })();
1415
1419
 
1420
+ var CAMPAIGN_KEYWORDS = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term'];
1421
+ var CLICK_IDS = ['dclid', 'fbclid', 'gclid', 'ko_click_id', 'li_fat_id', 'msclkid', 'ttclid', 'twclid', 'wbraid'];
1422
+
1416
1423
  _.info = {
1417
- campaignParams: function() {
1418
- var campaign_keywords = 'utm_source utm_medium utm_campaign utm_content utm_term'.split(' '),
1419
- kw = '',
1424
+ campaignParams: function(default_value) {
1425
+ var kw = '',
1420
1426
  params = {};
1421
- _.each(campaign_keywords, function(kwkey) {
1427
+ _.each(CAMPAIGN_KEYWORDS, function(kwkey) {
1422
1428
  kw = _.getQueryParam(document$1.URL, kwkey);
1423
1429
  if (kw.length) {
1424
1430
  params[kwkey] = kw;
1431
+ } else if (default_value !== undefined) {
1432
+ params[kwkey] = default_value;
1433
+ }
1434
+ });
1435
+
1436
+ return params;
1437
+ },
1438
+
1439
+ clickParams: function() {
1440
+ var id = '',
1441
+ params = {};
1442
+ _.each(CLICK_IDS, function(idkey) {
1443
+ id = _.getQueryParam(document$1.URL, idkey);
1444
+ if (id.length) {
1445
+ params[idkey] = id;
1425
1446
  }
1426
1447
  });
1427
1448
 
1428
1449
  return params;
1429
1450
  },
1430
1451
 
1452
+ marketingParams: function() {
1453
+ return _.extend(_.info.campaignParams(), _.info.clickParams());
1454
+ },
1455
+
1431
1456
  searchEngine: function(referrer) {
1432
1457
  if (referrer.search('https?://(.*)google.([^/?]*)') === 0) {
1433
1458
  return 'google';
@@ -1624,12 +1649,13 @@
1624
1649
  });
1625
1650
  },
1626
1651
 
1627
- pageviewInfo: function(page) {
1652
+ mpPageViewProperties: function() {
1628
1653
  return _.strip_empty_properties({
1629
- 'mp_page': page,
1630
- 'mp_referrer': document$1.referrer,
1631
- 'mp_browser': _.info.browser(userAgent, navigator.vendor, windowOpera),
1632
- 'mp_platform': _.info.os()
1654
+ 'current_page_title': document$1.title,
1655
+ 'current_domain': window$1.location.hostname,
1656
+ 'current_url_path': window$1.location.pathname,
1657
+ 'current_url_protocol': window$1.location.protocol,
1658
+ 'current_url_search': window$1.location.search
1633
1659
  });
1634
1660
  }
1635
1661
  };
@@ -3807,13 +3833,6 @@
3807
3833
  }
3808
3834
  };
3809
3835
 
3810
- MixpanelPersistence.prototype.update_campaign_params = function() {
3811
- if (!this.campaign_params_saved) {
3812
- this.register_once(_.info.campaignParams());
3813
- this.campaign_params_saved = true;
3814
- }
3815
- };
3816
-
3817
3836
  MixpanelPersistence.prototype.update_search_keyword = function(referrer) {
3818
3837
  this.register(_.info.searchInfo(referrer));
3819
3838
  };
@@ -4138,6 +4157,9 @@
4138
4157
  'cookie_domain': '',
4139
4158
  'cookie_name': '',
4140
4159
  'loaded': NOOP_FUNC,
4160
+ 'track_marketing': true,
4161
+ 'track_pageview': false,
4162
+ 'skip_first_touch_marketing': false,
4141
4163
  'store_google': true,
4142
4164
  'save_referrer': true,
4143
4165
  'test': false,
@@ -4204,6 +4226,25 @@
4204
4226
  instance['people'] = new MixpanelPeople();
4205
4227
  instance['people']._init(instance);
4206
4228
 
4229
+ if (!instance.get_config('skip_first_touch_marketing')) {
4230
+ // We need null UTM params in the object because
4231
+ // UTM parameters act as a tuple. If any UTM param
4232
+ // is present, then we set all UTM params including
4233
+ // empty ones together
4234
+ var utm_params = _.info.campaignParams(null);
4235
+ var initial_utm_params = {};
4236
+ var has_utm = false;
4237
+ _.each(utm_params, function(utm_value, utm_key) {
4238
+ initial_utm_params['initial_' + utm_key] = utm_value;
4239
+ if (utm_value) {
4240
+ has_utm = true;
4241
+ }
4242
+ });
4243
+ if (has_utm) {
4244
+ instance['people'].set_once(initial_utm_params);
4245
+ }
4246
+ }
4247
+
4207
4248
  // if any instance on the page has debug = true, we set the
4208
4249
  // global debug to be true
4209
4250
  Config.DEBUG = Config.DEBUG || instance.get_config('debug');
@@ -4235,7 +4276,7 @@
4235
4276
  * mixpanel.library_name.track(...);
4236
4277
  *
4237
4278
  * @param {String} token Your Mixpanel API token
4238
- * @param {Object} [config] A dictionary of config options to override. <a href="https://github.com/mixpanel/mixpanel-js/blob/8b2e1f7b/src/mixpanel-core.js#L87-L110">See a list of default config options</a>.
4279
+ * @param {Object} [config] A dictionary of config options to override. <a href="https://github.com/mixpanel/mixpanel-js/blob/v2.46.0/src/mixpanel-core.js#L88-L127">See a list of default config options</a>.
4239
4280
  * @param {String} [name] The name for the new mixpanel instance that you want created
4240
4281
  */
4241
4282
  MixpanelLib.prototype.init = function (token, config, name) {
@@ -4273,7 +4314,7 @@
4273
4314
  // default to JSON payload for standard mixpanel.com API hosts
4274
4315
  if (!('api_payload_format' in config)) {
4275
4316
  var api_host = config['api_host'] || DEFAULT_CONFIG['api_host'];
4276
- if (api_host.match(/\.mixpanel\.com$/)) {
4317
+ if (api_host.match(/\.mixpanel\.com/)) {
4277
4318
  variable_features['api_payload_format'] = PAYLOAD_TYPE_JSON;
4278
4319
  }
4279
4320
  }
@@ -4348,6 +4389,10 @@
4348
4389
  '$device_id': uuid
4349
4390
  }, '');
4350
4391
  }
4392
+
4393
+ if (this.get_config('track_pageview')) {
4394
+ this.track_pageview();
4395
+ }
4351
4396
  };
4352
4397
 
4353
4398
  // Private methods
@@ -4361,7 +4406,7 @@
4361
4406
  MixpanelLib.prototype._set_default_superprops = function() {
4362
4407
  this['persistence'].update_search_keyword(document$1.referrer);
4363
4408
  if (this.get_config('store_google')) {
4364
- this['persistence'].update_campaign_params();
4409
+ this.register(_.info.campaignParams(), {persistent: false});
4365
4410
  }
4366
4411
  if (this.get_config('save_referrer')) {
4367
4412
  this['persistence'].update_referrer_info(document$1.referrer);
@@ -4843,6 +4888,10 @@
4843
4888
 
4844
4889
  this._set_default_superprops();
4845
4890
 
4891
+ var marketing_properties = this.get_config('track_marketing')
4892
+ ? _.info.marketingParams()
4893
+ : {};
4894
+
4846
4895
  // note: extend writes to the first object, so lets make sure we
4847
4896
  // don't write to the persistence properties object and info
4848
4897
  // properties object by passing in a new object
@@ -4851,6 +4900,7 @@
4851
4900
  properties = _.extend(
4852
4901
  {},
4853
4902
  _.info.properties(),
4903
+ marketing_properties,
4854
4904
  this['persistence'].properties(),
4855
4905
  this.unpersisted_superprops,
4856
4906
  properties
@@ -5011,17 +5061,54 @@
5011
5061
  };
5012
5062
 
5013
5063
  /**
5014
- * Track mp_page_view event. This is now ignored by the server.
5064
+ * Track a default Mixpanel page view event, which includes extra default event properties to
5065
+ * improve page view data. The `config.track_pageview` option for <a href="#mixpanelinit">mixpanel.init()</a>
5066
+ * may be turned on for tracking page loads automatically.
5015
5067
  *
5016
- * @param {String} [page] The url of the page to record. If you don't include this, it defaults to the current url.
5017
- * @deprecated
5068
+ * ### Usage
5069
+ *
5070
+ * // track a default $mp_web_page_view event
5071
+ * mixpanel.track_pageview();
5072
+ *
5073
+ * // track a page view event with additional event properties
5074
+ * mixpanel.track_pageview({'ab_test_variant': 'card-layout-b'});
5075
+ *
5076
+ * // example approach to track page views on different page types as event properties
5077
+ * mixpanel.track_pageview({'page': 'pricing'});
5078
+ * mixpanel.track_pageview({'page': 'homepage'});
5079
+ *
5080
+ * // UNCOMMON: Tracking a page view event with a custom event_name option. NOT expected to be used for
5081
+ * // individual pages on the same site or product. Use cases for custom event_name may be page
5082
+ * // views on different products or internal applications that are considered completely separate
5083
+ * mixpanel.track_pageview({'page': 'customer-search'}, {'event_name': '[internal] Admin Page View'});
5084
+ *
5085
+ * @param {Object} [properties] An optional set of additional properties to send with the page view event
5086
+ * @param {Object} [options] Page view tracking options
5087
+ * @param {String} [options.event_name] - Alternate name for the tracking event
5088
+ * @returns {Boolean|Object} If the tracking request was successfully initiated/queued, an object
5089
+ * with the tracking payload sent to the API server is returned; otherwise false.
5018
5090
  */
5019
- MixpanelLib.prototype.track_pageview = function(page) {
5020
- if (_.isUndefined(page)) {
5021
- page = document$1.location.href;
5091
+ MixpanelLib.prototype.track_pageview = addOptOutCheckMixpanelLib(function(properties, options) {
5092
+ if (typeof properties !== 'object') {
5093
+ properties = {};
5022
5094
  }
5023
- this.track('mp_page_view', _.info.pageviewInfo(page));
5024
- };
5095
+ options = options || {};
5096
+ var event_name = options['event_name'] || '$mp_web_page_view';
5097
+
5098
+ var default_page_properties = _.extend(
5099
+ _.info.mpPageViewProperties(),
5100
+ _.info.campaignParams(),
5101
+ _.info.clickParams()
5102
+ );
5103
+
5104
+ var event_properties = _.extend(
5105
+ {},
5106
+ default_page_properties,
5107
+ properties
5108
+ );
5109
+
5110
+ return this.track(event_name, event_properties);
5111
+ });
5025
5112
 
5026
5113
  /**
5027
5114
  * Track clicks on a set of document elements. Selector must be a
@@ -5517,10 +5604,20 @@
5517
5604
  * // secure, meaning they will only be transmitted over https
5518
5605
  * secure_cookie: false
5519
5606
  *
5607
+ * // disables enriching user profiles with first touch marketing data
5608
+ * skip_first_touch_marketing: false
5609
+ *
5520
5610
  * // the amount of time track_links will
5521
5611
  * // wait for Mixpanel's servers to respond
5522
5612
  * track_links_timeout: 300
5523
5613
  *
5614
+ * // adds any UTM parameters and click IDs present on the page to any events fired
5615
+ * track_marketing: true
5616
+ *
5617
+ * // enables automatic page view tracking using default page view events through
5618
+ * // the track_pageview() method
5619
+ * track_pageview: false
5620
+ *
5524
5621
  * // if you set upgrade to be true, the library will check for
5525
5622
  * // a cookie from our old js library and import super
5526
5623
  * // properties from it, then the old cookie is deleted