mixpanel-browser 2.58.0 → 2.59.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.58.0'
5
+ LIB_VERSION: '2.59.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
@@ -14,11 +14,14 @@ if (typeof(window) === 'undefined') {
14
14
  win = {
15
15
  navigator: { userAgent: '', onLine: true },
16
16
  document: {
17
+ createElement: function() { return {}; },
17
18
  location: loc,
18
19
  referrer: ''
19
20
  },
20
21
  screen: { width: 0, height: 0 },
21
- location: loc
22
+ location: loc,
23
+ addEventListener: function() {},
24
+ removeEventListener: function() {}
22
25
  };
23
26
  } else {
24
27
  win = window;
@@ -493,6 +496,29 @@ var console_with_prefix = function(prefix) {
493
496
  };
494
497
 
495
498
 
499
+ var safewrap = function(f) {
500
+ return function() {
501
+ try {
502
+ return f.apply(this, arguments);
503
+ } catch (e) {
504
+ console.critical('Implementation error. Please turn on debug and contact support@mixpanel.com.');
505
+ if (Config.DEBUG){
506
+ console.critical(e);
507
+ }
508
+ }
509
+ };
510
+ };
511
+
512
+ var safewrapClass = function(klass) {
513
+ var proto = klass.prototype;
514
+ for (var func in proto) {
515
+ if (typeof(proto[func]) === 'function') {
516
+ proto[func] = safewrap(proto[func]);
517
+ }
518
+ }
519
+ };
520
+
521
+
496
522
  // UNDERSCORE
497
523
  // Embed part of the Underscore Library
498
524
  _.bind = function(func, context) {
@@ -1271,6 +1297,7 @@ _.UUID = (function() {
1271
1297
  var BLOCKED_UA_STRS = [
1272
1298
  'ahrefsbot',
1273
1299
  'ahrefssiteaudit',
1300
+ 'amazonbot',
1274
1301
  'baiduspider',
1275
1302
  'bingbot',
1276
1303
  'bingpreview',
@@ -1280,7 +1307,7 @@ var BLOCKED_UA_STRS = [
1280
1307
  'pinterest',
1281
1308
  'screaming frog',
1282
1309
  'yahoo! slurp',
1283
- 'yandexbot',
1310
+ 'yandex',
1284
1311
 
1285
1312
  // a whole bunch of goog-specific crawlers
1286
1313
  // https://developers.google.com/search/docs/advanced/crawling/overview-google-crawlers
@@ -2101,6 +2128,689 @@ _['info']['browserVersion'] = _.info.browserVersion;
2101
2128
  _['info']['properties'] = _.info.properties;
2102
2129
  _['NPO'] = NpoPromise;
2103
2130
 
2131
+ // stateless utils
2132
+
2133
+ var EV_CHANGE = 'change';
2134
+ var EV_CLICK = 'click';
2135
+ var EV_HASHCHANGE = 'hashchange';
2136
+ var EV_MP_LOCATION_CHANGE = 'mp_locationchange';
2137
+ var EV_POPSTATE = 'popstate';
2138
+ // TODO scrollend isn't available in Safari: document or polyfill?
2139
+ var EV_SCROLLEND = 'scrollend';
2140
+ var EV_SUBMIT = 'submit';
2141
+
2142
+ var CLICK_EVENT_PROPS = [
2143
+ 'clientX', 'clientY',
2144
+ 'offsetX', 'offsetY',
2145
+ 'pageX', 'pageY',
2146
+ 'screenX', 'screenY',
2147
+ 'x', 'y'
2148
+ ];
2149
+ var OPT_IN_CLASSES = ['mp-include'];
2150
+ var OPT_OUT_CLASSES = ['mp-no-track'];
2151
+ var SENSITIVE_DATA_CLASSES = OPT_OUT_CLASSES.concat(['mp-sensitive']);
2152
+ var TRACKED_ATTRS = [
2153
+ 'aria-label', 'aria-labelledby', 'aria-describedby',
2154
+ 'href', 'name', 'role', 'title', 'type'
2155
+ ];
2156
+
2157
+ var logger$3 = console_with_prefix('autocapture');
2158
+
2159
+
2160
+ function getClasses(el) {
2161
+ var classes = {};
2162
+ var classList = getClassName(el).split(' ');
2163
+ for (var i = 0; i < classList.length; i++) {
2164
+ var cls = classList[i];
2165
+ if (cls) {
2166
+ classes[cls] = true;
2167
+ }
2168
+ }
2169
+ return classes;
2170
+ }
2171
+
2172
+ /*
2173
+ * Get the className of an element, accounting for edge cases where element.className is an object
2174
+ * @param {Element} el - element to get the className of
2175
+ * @returns {string} the element's class
2176
+ */
2177
+ function getClassName(el) {
2178
+ switch(typeof el.className) {
2179
+ case 'string':
2180
+ return el.className;
2181
+ case 'object': // handle cases where className might be SVGAnimatedString or some other type
2182
+ return el.className.baseVal || el.getAttribute('class') || '';
2183
+ default: // future proof
2184
+ return '';
2185
+ }
2186
+ }
2187
+
2188
+ function getPreviousElementSibling(el) {
2189
+ if (el.previousElementSibling) {
2190
+ return el.previousElementSibling;
2191
+ } else {
2192
+ do {
2193
+ el = el.previousSibling;
2194
+ } while (el && !isElementNode(el));
2195
+ return el;
2196
+ }
2197
+ }
2198
+
2199
+ function getPropertiesFromElement(el) {
2200
+ var props = {
2201
+ '$classes': getClassName(el).split(' '),
2202
+ '$tag_name': el.tagName.toLowerCase()
2203
+ };
2204
+ var elId = el.id;
2205
+ if (elId) {
2206
+ props['$id'] = elId;
2207
+ }
2208
+
2209
+ if (shouldTrackElement(el)) {
2210
+ _.each(TRACKED_ATTRS, function(attr) {
2211
+ if (el.hasAttribute(attr)) {
2212
+ var attrVal = el.getAttribute(attr);
2213
+ if (shouldTrackValue(attrVal)) {
2214
+ props['$attr-' + attr] = attrVal;
2215
+ }
2216
+ }
2217
+ });
2218
+ }
2219
+
2220
+ var nthChild = 1;
2221
+ var nthOfType = 1;
2222
+ var currentElem = el;
2223
+ while (currentElem = getPreviousElementSibling(currentElem)) { // eslint-disable-line no-cond-assign
2224
+ nthChild++;
2225
+ if (currentElem.tagName === el.tagName) {
2226
+ nthOfType++;
2227
+ }
2228
+ }
2229
+ props['$nth_child'] = nthChild;
2230
+ props['$nth_of_type'] = nthOfType;
2231
+
2232
+ return props;
2233
+ }
2234
+
2235
+ function getPropsForDOMEvent(ev, blockSelectors, captureTextContent) {
2236
+ blockSelectors = blockSelectors || [];
2237
+ var props = null;
2238
+
2239
+ var target = typeof ev.target === 'undefined' ? ev.srcElement : ev.target;
2240
+ if (isTextNode(target)) { // defeat Safari bug (see: http://www.quirksmode.org/js/events_properties.html)
2241
+ target = target.parentNode;
2242
+ }
2243
+
2244
+ if (shouldTrackDomEvent(target, ev)) {
2245
+ var targetElementList = [target];
2246
+ var curEl = target;
2247
+ while (curEl.parentNode && !isTag(curEl, 'body')) {
2248
+ targetElementList.push(curEl.parentNode);
2249
+ curEl = curEl.parentNode;
2250
+ }
2251
+
2252
+ var elementsJson = [];
2253
+ var href, explicitNoTrack = false;
2254
+ _.each(targetElementList, function(el) {
2255
+ var shouldTrackEl = shouldTrackElement(el);
2256
+
2257
+ // if the element or a parent element is an anchor tag
2258
+ // include the href as a property
2259
+ if (el.tagName.toLowerCase() === 'a') {
2260
+ href = el.getAttribute('href');
2261
+ href = shouldTrackEl && shouldTrackValue(href) && href;
2262
+ }
2263
+
2264
+ // allow users to programmatically prevent tracking of elements by adding classes such as 'mp-no-track'
2265
+ var classes = getClasses(el);
2266
+ _.each(OPT_OUT_CLASSES, function(cls) {
2267
+ if (classes[cls]) {
2268
+ explicitNoTrack = true;
2269
+ }
2270
+ });
2271
+
2272
+ if (!explicitNoTrack) {
2273
+ // programmatically prevent tracking of elements that match CSS selectors
2274
+ _.each(blockSelectors, function(sel) {
2275
+ try {
2276
+ if (el['matches'](sel)) {
2277
+ explicitNoTrack = true;
2278
+ }
2279
+ } catch (err) {
2280
+ logger$3.critical('Error while checking selector: ' + sel, err);
2281
+ }
2282
+ });
2283
+ }
2284
+
2285
+ elementsJson.push(getPropertiesFromElement(el));
2286
+ }, this);
2287
+
2288
+ if (!explicitNoTrack) {
2289
+ var docElement = document$1['documentElement'];
2290
+ props = {
2291
+ '$event_type': ev.type,
2292
+ '$host': win.location.host,
2293
+ '$pathname': win.location.pathname,
2294
+ '$elements': elementsJson,
2295
+ '$el_attr__href': href,
2296
+ '$viewportHeight': Math.max(docElement['clientHeight'], win['innerHeight'] || 0),
2297
+ '$viewportWidth': Math.max(docElement['clientWidth'], win['innerWidth'] || 0)
2298
+ };
2299
+
2300
+ if (captureTextContent) {
2301
+ elementText = getSafeText(target);
2302
+ if (elementText && elementText.length) {
2303
+ props['$el_text'] = elementText;
2304
+ }
2305
+ }
2306
+
2307
+ if (ev.type === EV_CLICK) {
2308
+ _.each(CLICK_EVENT_PROPS, function(prop) {
2309
+ if (prop in ev) {
2310
+ props['$' + prop] = ev[prop];
2311
+ }
2312
+ });
2313
+ target = guessRealClickTarget(ev);
2314
+ }
2315
+ // prioritize text content from "real" click target if different from original target
2316
+ if (captureTextContent) {
2317
+ var elementText = getSafeText(target);
2318
+ if (elementText && elementText.length) {
2319
+ props['$el_text'] = elementText;
2320
+ }
2321
+ }
2322
+
2323
+ if (target) {
2324
+ var targetProps = getPropertiesFromElement(target);
2325
+ props['$target'] = targetProps;
2326
+ // pull up more props onto main event props
2327
+ props['$el_classes'] = targetProps['$classes'];
2328
+ _.extend(props, _.strip_empty_properties({
2329
+ '$el_id': targetProps['$id'],
2330
+ '$el_tag_name': targetProps['$tag_name']
2331
+ }));
2332
+ }
2333
+ }
2334
+ }
2335
+
2336
+ return props;
2337
+ }
2338
+
2339
+
2340
+ /*
2341
+ * Get the direct text content of an element, protecting against sensitive data collection.
2342
+ * Concats textContent of each of the element's text node children; this avoids potential
2343
+ * collection of sensitive data that could happen if we used element.textContent and the
2344
+ * element had sensitive child elements, since element.textContent includes child content.
2345
+ * Scrubs values that look like they could be sensitive (i.e. cc or ssn number).
2346
+ * @param {Element} el - element to get the text of
2347
+ * @returns {string} the element's direct text content
2348
+ */
2349
+ function getSafeText(el) {
2350
+ var elText = '';
2351
+
2352
+ if (shouldTrackElement(el) && el.childNodes && el.childNodes.length) {
2353
+ _.each(el.childNodes, function(child) {
2354
+ if (isTextNode(child) && child.textContent) {
2355
+ elText += _.trim(child.textContent)
2356
+ // scrub potentially sensitive values
2357
+ .split(/(\s+)/).filter(shouldTrackValue).join('')
2358
+ // normalize whitespace
2359
+ .replace(/[\r\n]/g, ' ').replace(/[ ]+/g, ' ')
2360
+ // truncate
2361
+ .substring(0, 255);
2362
+ }
2363
+ });
2364
+ }
2365
+
2366
+ return _.trim(elText);
2367
+ }
2368
+
2369
+ function guessRealClickTarget(ev) {
2370
+ var target = ev.target;
2371
+ var composedPath = ev['composedPath']();
2372
+ for (var i = 0; i < composedPath.length; i++) {
2373
+ var node = composedPath[i];
2374
+ if (
2375
+ isTag(node, 'a') ||
2376
+ isTag(node, 'button') ||
2377
+ isTag(node, 'input') ||
2378
+ isTag(node, 'select') ||
2379
+ (node.getAttribute && node.getAttribute('role') === 'button')
2380
+ ) {
2381
+ target = node;
2382
+ break;
2383
+ }
2384
+ if (node === target) {
2385
+ break;
2386
+ }
2387
+ }
2388
+ return target;
2389
+ }
2390
+
2391
+ /*
2392
+ * Check whether a DOM node has nodeType Node.ELEMENT_NODE
2393
+ * @param {Node} node - node to check
2394
+ * @returns {boolean} whether node is of the correct nodeType
2395
+ */
2396
+ function isElementNode(node) {
2397
+ return node && node.nodeType === 1; // Node.ELEMENT_NODE - use integer constant for browser portability
2398
+ }
2399
+
2400
+ /*
2401
+ * Check whether an element is of a given tag type.
2402
+ * Due to potential reference discrepancies (such as the webcomponents.js polyfill),
2403
+ * we want to match tagNames instead of specific references because something like
2404
+ * element === document.body won't always work because element might not be a native
2405
+ * element.
2406
+ * @param {Element} el - element to check
2407
+ * @param {string} tag - tag name (e.g., "div")
2408
+ * @returns {boolean} whether el is of the given tag type
2409
+ */
2410
+ function isTag(el, tag) {
2411
+ return el && el.tagName && el.tagName.toLowerCase() === tag.toLowerCase();
2412
+ }
2413
+
2414
+ /*
2415
+ * Check whether a DOM node is a TEXT_NODE
2416
+ * @param {Node} node - node to check
2417
+ * @returns {boolean} whether node is of type Node.TEXT_NODE
2418
+ */
2419
+ function isTextNode(node) {
2420
+ return node && node.nodeType === 3; // Node.TEXT_NODE - use integer constant for browser portability
2421
+ }
2422
+
2423
+ function minDOMApisSupported() {
2424
+ try {
2425
+ var testEl = document$1.createElement('div');
2426
+ return !!testEl['matches'];
2427
+ } catch (err) {
2428
+ return false;
2429
+ }
2430
+ }
2431
+
2432
+ /*
2433
+ * Check whether a DOM event should be "tracked" or if it may contain sensitive data
2434
+ * using a variety of heuristics.
2435
+ * @param {Element} el - element to check
2436
+ * @param {Event} ev - event to check
2437
+ * @returns {boolean} whether the event should be tracked
2438
+ */
2439
+ function shouldTrackDomEvent(el, ev) {
2440
+ if (!el || isTag(el, 'html') || !isElementNode(el)) {
2441
+ return false;
2442
+ }
2443
+ var tag = el.tagName.toLowerCase();
2444
+ switch (tag) {
2445
+ case 'form':
2446
+ return ev.type === EV_SUBMIT;
2447
+ case 'input':
2448
+ if (['button', 'submit'].indexOf(el.getAttribute('type')) === -1) {
2449
+ return ev.type === EV_CHANGE;
2450
+ } else {
2451
+ return ev.type === EV_CLICK;
2452
+ }
2453
+ case 'select':
2454
+ case 'textarea':
2455
+ return ev.type === EV_CHANGE;
2456
+ default:
2457
+ return ev.type === EV_CLICK;
2458
+ }
2459
+ }
2460
+
2461
+ /*
2462
+ * Check whether a DOM element should be "tracked" or if it may contain sensitive data
2463
+ * using a variety of heuristics.
2464
+ * @param {Element} el - element to check
2465
+ * @returns {boolean} whether the element should be tracked
2466
+ */
2467
+ function shouldTrackElement(el) {
2468
+ var i;
2469
+
2470
+ for (var curEl = el; curEl.parentNode && !isTag(curEl, 'body'); curEl = curEl.parentNode) {
2471
+ var classes = getClasses(curEl);
2472
+ for (i = 0; i < SENSITIVE_DATA_CLASSES.length; i++) {
2473
+ if (classes[SENSITIVE_DATA_CLASSES[i]]) {
2474
+ return false;
2475
+ }
2476
+ }
2477
+ }
2478
+
2479
+ var elClasses = getClasses(el);
2480
+ for (i = 0; i < OPT_IN_CLASSES.length; i++) {
2481
+ if (elClasses[OPT_IN_CLASSES[i]]) {
2482
+ return true;
2483
+ }
2484
+ }
2485
+
2486
+ // don't send data from inputs or similar elements since there will always be
2487
+ // a risk of clientside javascript placing sensitive data in attributes
2488
+ if (
2489
+ isTag(el, 'input') ||
2490
+ isTag(el, 'select') ||
2491
+ isTag(el, 'textarea') ||
2492
+ el.getAttribute('contenteditable') === 'true'
2493
+ ) {
2494
+ return false;
2495
+ }
2496
+
2497
+ // don't include hidden or password fields
2498
+ var type = el.type || '';
2499
+ if (typeof type === 'string') { // it's possible for el.type to be a DOM element if el is a form with a child input[name="type"]
2500
+ switch(type.toLowerCase()) {
2501
+ case 'hidden':
2502
+ return false;
2503
+ case 'password':
2504
+ return false;
2505
+ }
2506
+ }
2507
+
2508
+ // filter out data from fields that look like sensitive fields
2509
+ var name = el.name || el.id || '';
2510
+ if (typeof name === 'string') { // it's possible for el.name or el.id to be a DOM element if el is a form with a child input[name="name"]
2511
+ var sensitiveNameRegex = /^cc|cardnum|ccnum|creditcard|csc|cvc|cvv|exp|pass|pwd|routing|seccode|securitycode|securitynum|socialsec|socsec|ssn/i;
2512
+ if (sensitiveNameRegex.test(name.replace(/[^a-zA-Z0-9]/g, ''))) {
2513
+ return false;
2514
+ }
2515
+ }
2516
+
2517
+ return true;
2518
+ }
2519
+
2520
+
2521
+ /*
2522
+ * Check whether a string value should be "tracked" or if it may contain sensitive data
2523
+ * using a variety of heuristics.
2524
+ * @param {string} value - string value to check
2525
+ * @returns {boolean} whether the element should be tracked
2526
+ */
2527
+ function shouldTrackValue(value) {
2528
+ if (value === null || _.isUndefined(value)) {
2529
+ return false;
2530
+ }
2531
+
2532
+ if (typeof value === 'string') {
2533
+ value = _.trim(value);
2534
+
2535
+ // check to see if input value looks like a credit card number
2536
+ // see: https://www.safaribooksonline.com/library/view/regular-expressions-cookbook/9781449327453/ch04s20.html
2537
+ var ccRegex = /^(?:(4[0-9]{12}(?:[0-9]{3})?)|(5[1-5][0-9]{14})|(6(?:011|5[0-9]{2})[0-9]{12})|(3[47][0-9]{13})|(3(?:0[0-5]|[68][0-9])[0-9]{11})|((?:2131|1800|35[0-9]{3})[0-9]{11}))$/;
2538
+ if (ccRegex.test((value || '').replace(/[- ]/g, ''))) {
2539
+ return false;
2540
+ }
2541
+
2542
+ // check to see if input value looks like a social security number
2543
+ var ssnRegex = /(^\d{3}-?\d{2}-?\d{4}$)/;
2544
+ if (ssnRegex.test(value)) {
2545
+ return false;
2546
+ }
2547
+ }
2548
+
2549
+ return true;
2550
+ }
2551
+
2552
+ var AUTOCAPTURE_CONFIG_KEY = 'autocapture';
2553
+ var LEGACY_PAGEVIEW_CONFIG_KEY = 'track_pageview';
2554
+
2555
+ var PAGEVIEW_OPTION_FULL_URL = 'full-url';
2556
+ var PAGEVIEW_OPTION_URL_WITH_PATH_AND_QUERY_STRING = 'url-with-path-and-query-string';
2557
+ var PAGEVIEW_OPTION_URL_WITH_PATH = 'url-with-path';
2558
+
2559
+ var CONFIG_BLOCK_SELECTORS = 'block_selectors';
2560
+ var CONFIG_BLOCK_URL_REGEXES = 'block_url_regexes';
2561
+ var CONFIG_CAPTURE_TEXT_CONTENT = 'capture_text_content';
2562
+ var CONFIG_TRACK_CLICK = 'click';
2563
+ var CONFIG_TRACK_INPUT = 'input';
2564
+ var CONFIG_TRACK_PAGEVIEW = 'pageview';
2565
+ var CONFIG_TRACK_SCROLL = 'scroll';
2566
+ var CONFIG_TRACK_SUBMIT = 'submit';
2567
+
2568
+ var CONFIG_DEFAULTS = {};
2569
+ CONFIG_DEFAULTS[CONFIG_CAPTURE_TEXT_CONTENT] = false;
2570
+ CONFIG_DEFAULTS[CONFIG_TRACK_CLICK] = true;
2571
+ CONFIG_DEFAULTS[CONFIG_TRACK_INPUT] = true;
2572
+ CONFIG_DEFAULTS[CONFIG_TRACK_PAGEVIEW] = PAGEVIEW_OPTION_FULL_URL;
2573
+ CONFIG_DEFAULTS[CONFIG_TRACK_SCROLL] = true;
2574
+ CONFIG_DEFAULTS[CONFIG_TRACK_SUBMIT] = true;
2575
+
2576
+ var DEFAULT_PROPS = {
2577
+ '$mp_autocapture': true
2578
+ };
2579
+
2580
+ var MP_EV_CLICK = '$mp_click';
2581
+ var MP_EV_INPUT = '$mp_input_change';
2582
+ var MP_EV_SCROLL = '$mp_scroll';
2583
+ var MP_EV_SUBMIT = '$mp_submit';
2584
+
2585
+ /**
2586
+ * Autocapture: manages automatic event tracking
2587
+ * @constructor
2588
+ */
2589
+ var Autocapture = function(mp) {
2590
+ this.mp = mp;
2591
+ };
2592
+
2593
+ Autocapture.prototype.init = function() {
2594
+ if (!minDOMApisSupported()) {
2595
+ logger$3.critical('Autocapture unavailable: missing required DOM APIs');
2596
+ return;
2597
+ }
2598
+
2599
+ this.initPageviewTracking();
2600
+ this.initClickTracking();
2601
+ this.initInputTracking();
2602
+ this.initScrollTracking();
2603
+ this.initSubmitTracking();
2604
+ };
2605
+
2606
+ Autocapture.prototype.getFullConfig = function() {
2607
+ var autocaptureConfig = this.mp.get_config(AUTOCAPTURE_CONFIG_KEY);
2608
+ if (!autocaptureConfig) {
2609
+ // Autocapture is completely off
2610
+ return {};
2611
+ } else if (_.isObject(autocaptureConfig)) {
2612
+ return _.extend({}, CONFIG_DEFAULTS, autocaptureConfig);
2613
+ } else {
2614
+ // Autocapture config is non-object truthy value, return default
2615
+ return CONFIG_DEFAULTS;
2616
+ }
2617
+ };
2618
+
2619
+ Autocapture.prototype.getConfig = function(key) {
2620
+ return this.getFullConfig()[key];
2621
+ };
2622
+
2623
+ Autocapture.prototype.currentUrlBlocked = function() {
2624
+ var blockUrlRegexes = this.getConfig(CONFIG_BLOCK_URL_REGEXES) || [];
2625
+ if (!blockUrlRegexes || !blockUrlRegexes.length) {
2626
+ return false;
2627
+ }
2628
+
2629
+ var currentUrl = _.info.currentUrl();
2630
+ for (var i = 0; i < blockUrlRegexes.length; i++) {
2631
+ try {
2632
+ if (currentUrl.match(blockUrlRegexes[i])) {
2633
+ return true;
2634
+ }
2635
+ } catch (err) {
2636
+ logger$3.critical('Error while checking block URL regex: ' + blockUrlRegexes[i], err);
2637
+ return true;
2638
+ }
2639
+ }
2640
+ return false;
2641
+ };
2642
+
2643
+ Autocapture.prototype.pageviewTrackingConfig = function() {
2644
+ // supports both autocapture config and old track_pageview config
2645
+ if (this.mp.get_config(AUTOCAPTURE_CONFIG_KEY)) {
2646
+ return this.getConfig(CONFIG_TRACK_PAGEVIEW);
2647
+ } else {
2648
+ return this.mp.get_config(LEGACY_PAGEVIEW_CONFIG_KEY);
2649
+ }
2650
+ };
2651
+
2652
+ // helper for event handlers
2653
+ Autocapture.prototype.trackDomEvent = function(ev, mpEventName) {
2654
+ if (this.currentUrlBlocked()) {
2655
+ return;
2656
+ }
2657
+
2658
+ var props = getPropsForDOMEvent(
2659
+ ev,
2660
+ this.getConfig(CONFIG_BLOCK_SELECTORS),
2661
+ this.getConfig(CONFIG_CAPTURE_TEXT_CONTENT)
2662
+ );
2663
+ if (props) {
2664
+ _.extend(props, DEFAULT_PROPS);
2665
+ this.mp.track(mpEventName, props);
2666
+ }
2667
+ };
2668
+
2669
+ Autocapture.prototype.initClickTracking = function() {
2670
+ win.removeEventListener(EV_CLICK, this.listenerClick);
2671
+
2672
+ if (!this.getConfig(CONFIG_TRACK_CLICK)) {
2673
+ return;
2674
+ }
2675
+ logger$3.log('Initializing click tracking');
2676
+
2677
+ this.listenerClick = win.addEventListener(EV_CLICK, function(ev) {
2678
+ if (!this.getConfig(CONFIG_TRACK_CLICK)) {
2679
+ return;
2680
+ }
2681
+ this.trackDomEvent(ev, MP_EV_CLICK);
2682
+ }.bind(this));
2683
+ };
2684
+
2685
+ Autocapture.prototype.initInputTracking = function() {
2686
+ win.removeEventListener(EV_CHANGE, this.listenerChange);
2687
+
2688
+ if (!this.getConfig(CONFIG_TRACK_INPUT)) {
2689
+ return;
2690
+ }
2691
+ logger$3.log('Initializing input tracking');
2692
+
2693
+ this.listenerChange = win.addEventListener(EV_CHANGE, function(ev) {
2694
+ if (!this.getConfig(CONFIG_TRACK_INPUT)) {
2695
+ return;
2696
+ }
2697
+ this.trackDomEvent(ev, MP_EV_INPUT);
2698
+ }.bind(this));
2699
+ };
2700
+
2701
+ Autocapture.prototype.initPageviewTracking = function() {
2702
+ win.removeEventListener(EV_POPSTATE, this.listenerPopstate);
2703
+ win.removeEventListener(EV_HASHCHANGE, this.listenerHashchange);
2704
+ win.removeEventListener(EV_MP_LOCATION_CHANGE, this.listenerLocationchange);
2705
+
2706
+ if (!this.pageviewTrackingConfig()) {
2707
+ return;
2708
+ }
2709
+ logger$3.log('Initializing pageview tracking');
2710
+
2711
+ var previousTrackedUrl = '';
2712
+ var tracked = false;
2713
+ if (!this.currentUrlBlocked()) {
2714
+ tracked = this.mp.track_pageview(DEFAULT_PROPS);
2715
+ }
2716
+ if (tracked) {
2717
+ previousTrackedUrl = _.info.currentUrl();
2718
+ }
2719
+
2720
+ this.listenerPopstate = win.addEventListener(EV_POPSTATE, function() {
2721
+ win.dispatchEvent(new Event(EV_MP_LOCATION_CHANGE));
2722
+ });
2723
+ this.listenerHashchange = win.addEventListener(EV_HASHCHANGE, function() {
2724
+ win.dispatchEvent(new Event(EV_MP_LOCATION_CHANGE));
2725
+ });
2726
+ var nativePushState = win.history.pushState;
2727
+ if (typeof nativePushState === 'function') {
2728
+ win.history.pushState = function(state, unused, url) {
2729
+ nativePushState.call(win.history, state, unused, url);
2730
+ win.dispatchEvent(new Event(EV_MP_LOCATION_CHANGE));
2731
+ };
2732
+ }
2733
+ var nativeReplaceState = win.history.replaceState;
2734
+ if (typeof nativeReplaceState === 'function') {
2735
+ win.history.replaceState = function(state, unused, url) {
2736
+ nativeReplaceState.call(win.history, state, unused, url);
2737
+ win.dispatchEvent(new Event(EV_MP_LOCATION_CHANGE));
2738
+ };
2739
+ }
2740
+ this.listenerLocationchange = win.addEventListener(EV_MP_LOCATION_CHANGE, safewrap(function() {
2741
+ if (this.currentUrlBlocked()) {
2742
+ return;
2743
+ }
2744
+
2745
+ var currentUrl = _.info.currentUrl();
2746
+ var shouldTrack = false;
2747
+ var trackPageviewOption = this.pageviewTrackingConfig();
2748
+ if (trackPageviewOption === PAGEVIEW_OPTION_FULL_URL) {
2749
+ shouldTrack = currentUrl !== previousTrackedUrl;
2750
+ } else if (trackPageviewOption === PAGEVIEW_OPTION_URL_WITH_PATH_AND_QUERY_STRING) {
2751
+ shouldTrack = currentUrl.split('#')[0] !== previousTrackedUrl.split('#')[0];
2752
+ } else if (trackPageviewOption === PAGEVIEW_OPTION_URL_WITH_PATH) {
2753
+ shouldTrack = currentUrl.split('#')[0].split('?')[0] !== previousTrackedUrl.split('#')[0].split('?')[0];
2754
+ }
2755
+
2756
+ if (shouldTrack) {
2757
+ var tracked = this.mp.track_pageview(DEFAULT_PROPS);
2758
+ if (tracked) {
2759
+ previousTrackedUrl = currentUrl;
2760
+ }
2761
+ }
2762
+ }.bind(this)));
2763
+ };
2764
+
2765
+ Autocapture.prototype.initScrollTracking = function() {
2766
+ win.removeEventListener(EV_SCROLLEND, this.listenerScroll);
2767
+
2768
+ if (!this.getConfig(CONFIG_TRACK_SCROLL)) {
2769
+ return;
2770
+ }
2771
+ logger$3.log('Initializing scroll tracking');
2772
+
2773
+ this.listenerScroll = win.addEventListener(EV_SCROLLEND, safewrap(function() {
2774
+ if (!this.getConfig(CONFIG_TRACK_SCROLL)) {
2775
+ return;
2776
+ }
2777
+ if (this.currentUrlBlocked()) {
2778
+ return;
2779
+ }
2780
+
2781
+ var scrollTop = win.scrollY;
2782
+ var props = _.extend({'$scroll_top': scrollTop}, DEFAULT_PROPS);
2783
+ try {
2784
+ var scrollHeight = document$1.body.scrollHeight;
2785
+ var scrollPercentage = Math.round((scrollTop / (scrollHeight - win.innerHeight)) * 100);
2786
+ props['$scroll_height'] = scrollHeight;
2787
+ props['$scroll_percentage'] = scrollPercentage;
2788
+ } catch (err) {
2789
+ logger$3.critical('Error while calculating scroll percentage', err);
2790
+ }
2791
+ this.mp.track(MP_EV_SCROLL, props);
2792
+ }.bind(this)));
2793
+ };
2794
+
2795
+ Autocapture.prototype.initSubmitTracking = function() {
2796
+ win.removeEventListener(EV_SUBMIT, this.listenerSubmit);
2797
+
2798
+ if (!this.getConfig(CONFIG_TRACK_SUBMIT)) {
2799
+ return;
2800
+ }
2801
+ logger$3.log('Initializing submit tracking');
2802
+
2803
+ this.listenerSubmit = win.addEventListener(EV_SUBMIT, function(ev) {
2804
+ if (!this.getConfig(CONFIG_TRACK_SUBMIT)) {
2805
+ return;
2806
+ }
2807
+ this.trackDomEvent(ev, MP_EV_SUBMIT);
2808
+ }.bind(this));
2809
+ };
2810
+
2811
+ // TODO integrate error_reporter from mixpanel instance
2812
+ safewrapClass(Autocapture);
2813
+
2104
2814
  /* eslint camelcase: "off" */
2105
2815
 
2106
2816
  /**
@@ -4703,6 +5413,7 @@ var DEFAULT_CONFIG = {
4703
5413
  'api_transport': 'XHR',
4704
5414
  'api_payload_format': PAYLOAD_TYPE_BASE64,
4705
5415
  'app_host': 'https://mixpanel.com',
5416
+ 'autocapture': false,
4706
5417
  'cdn': 'https://cdn.mxpnl.com',
4707
5418
  'cross_site_cookie': false,
4708
5419
  'cross_subdomain_cookie': true,
@@ -4962,10 +5673,8 @@ MixpanelLib.prototype._init = function(token, config, name) {
4962
5673
  }, '');
4963
5674
  }
4964
5675
 
4965
- var track_pageview_option = this.get_config('track_pageview');
4966
- if (track_pageview_option) {
4967
- this._init_url_change_tracking(track_pageview_option);
4968
- }
5676
+ this.autocapture = new Autocapture(this);
5677
+ this.autocapture.init();
4969
5678
 
4970
5679
  if (this.get_config('record_sessions_percent') > 0 && Math.random() * 100 <= this.get_config('record_sessions_percent')) {
4971
5680
  this.start_session_recording();
@@ -5090,55 +5799,6 @@ MixpanelLib.prototype._track_dom = function(DomClass, args) {
5090
5799
  return dt.track.apply(dt, args);
5091
5800
  };
5092
5801
 
5093
- MixpanelLib.prototype._init_url_change_tracking = function(track_pageview_option) {
5094
- var previous_tracked_url = '';
5095
- var tracked = this.track_pageview();
5096
- if (tracked) {
5097
- previous_tracked_url = _.info.currentUrl();
5098
- }
5099
-
5100
- if (_.include(['full-url', 'url-with-path-and-query-string', 'url-with-path'], track_pageview_option)) {
5101
- win.addEventListener('popstate', function() {
5102
- win.dispatchEvent(new Event('mp_locationchange'));
5103
- });
5104
- win.addEventListener('hashchange', function() {
5105
- win.dispatchEvent(new Event('mp_locationchange'));
5106
- });
5107
- var nativePushState = win.history.pushState;
5108
- if (typeof nativePushState === 'function') {
5109
- win.history.pushState = function(state, unused, url) {
5110
- nativePushState.call(win.history, state, unused, url);
5111
- win.dispatchEvent(new Event('mp_locationchange'));
5112
- };
5113
- }
5114
- var nativeReplaceState = win.history.replaceState;
5115
- if (typeof nativeReplaceState === 'function') {
5116
- win.history.replaceState = function(state, unused, url) {
5117
- nativeReplaceState.call(win.history, state, unused, url);
5118
- win.dispatchEvent(new Event('mp_locationchange'));
5119
- };
5120
- }
5121
- win.addEventListener('mp_locationchange', function() {
5122
- var current_url = _.info.currentUrl();
5123
- var should_track = false;
5124
- if (track_pageview_option === 'full-url') {
5125
- should_track = current_url !== previous_tracked_url;
5126
- } else if (track_pageview_option === 'url-with-path-and-query-string') {
5127
- should_track = current_url.split('#')[0] !== previous_tracked_url.split('#')[0];
5128
- } else if (track_pageview_option === 'url-with-path') {
5129
- should_track = current_url.split('#')[0].split('?')[0] !== previous_tracked_url.split('#')[0].split('?')[0];
5130
- }
5131
-
5132
- if (should_track) {
5133
- var tracked = this.track_pageview();
5134
- if (tracked) {
5135
- previous_tracked_url = current_url;
5136
- }
5137
- }
5138
- }.bind(this));
5139
- }
5140
- };
5141
-
5142
5802
  /**
5143
5803
  * _prepare_callback() should be called by callers of _send_request for use
5144
5804
  * as the callback argument.
@@ -6396,6 +7056,10 @@ MixpanelLib.prototype.set_config = function(config) {
6396
7056
  this['persistence'].update_config(this['config']);
6397
7057
  }
6398
7058
  Config.DEBUG = Config.DEBUG || this.get_config('debug');
7059
+
7060
+ if ('autocapture' in config && this.autocapture) {
7061
+ this.autocapture.init();
7062
+ }
6399
7063
  }
6400
7064
  };
6401
7065