|
| 1 | +// ==ClosureCompiler== |
| 2 | +// @compilation_level SIMPLE_OPTIMIZATIONS |
| 3 | +// @output_file_name mixpanel-jslib-2.2-snippet.min.js |
| 4 | +// ==/ClosureCompiler== |
| 5 | + |
| 6 | +/** @define {string} */ |
| 7 | +var MIXPANEL_LIB_URL = '//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js'; |
| 8 | + |
| 9 | +(function(document, mixpanel) { |
| 10 | + // Only stub out if this is the first time running the snippet. |
| 11 | + if (!mixpanel['__SV']) { |
| 12 | + var script, first_script, gen_fn, functions, i, lib_name = "mixpanel"; |
| 13 | + window[lib_name] = mixpanel; |
| 14 | + |
| 15 | + mixpanel['_i'] = []; |
| 16 | + |
| 17 | + mixpanel['init'] = function (token, config, name) { |
| 18 | + // support multiple mixpanel instances |
| 19 | + var target = mixpanel; |
| 20 | + if (typeof(name) !== 'undefined') { |
| 21 | + target = mixpanel[name] = []; |
| 22 | + } else { |
| 23 | + name = lib_name; |
| 24 | + } |
| 25 | + |
| 26 | + // Pass in current people object if it exists |
| 27 | + target['people'] = target['people'] || []; |
| 28 | + target['toString'] = function(no_stub) { |
| 29 | + var str = lib_name; |
| 30 | + if (name !== lib_name) { |
| 31 | + str += "." + name; |
| 32 | + } |
| 33 | + if (!no_stub) { |
| 34 | + str += " (stub)"; |
| 35 | + } |
| 36 | + return str; |
| 37 | + }; |
| 38 | + target['people']['toString'] = function() { |
| 39 | + // 1 instead of true for minifying |
| 40 | + return target.toString(1) + ".people (stub)"; |
| 41 | + }; |
| 42 | + |
| 43 | + function _set_and_defer(target, fn) { |
| 44 | + var split = fn.split("."); |
| 45 | + if (split.length == 2) { |
| 46 | + target = target[split[0]]; |
| 47 | + fn = split[1]; |
| 48 | + } |
| 49 | + target[fn] = function() { |
| 50 | + target.push([fn].concat(Array.prototype.slice.call(arguments, 0))); |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + // create shallow clone of the public mixpanel interface |
| 55 | + // Note: only supports 1 additional level atm, e.g. mixpanel.people.set, not mixpanel.people.set.do_something_else. |
| 56 | + functions = "disable time_event track track_pageview track_links track_forms track_with_groups add_group set_group remove_group register register_once alias unregister identify name_tag set_config reset opt_in_tracking opt_out_tracking has_opted_in_tracking has_opted_out_tracking clear_opt_in_out_tracking start_batch_senders people.set people.set_once people.unset people.increment people.append people.union people.track_charge people.clear_charges people.delete_user people.remove".split(' '); |
| 57 | + for (i = 0; i < functions.length; i++) { |
| 58 | + _set_and_defer(target, functions[i]); |
| 59 | + } |
| 60 | + |
| 61 | + // special case for get_group(): chain method calls like mixpanel.get_group('foo', 'bar').unset('baz') |
| 62 | + var group_functions = "set set_once union unset remove delete".split(' '); |
| 63 | + target['get_group'] = function() { |
| 64 | + var mock_group = {}; |
| 65 | + |
| 66 | + var call1_args = arguments; |
| 67 | + var call1 = ['get_group'].concat(Array.prototype.slice.call(call1_args, 0)); |
| 68 | + |
| 69 | + function _set_and_defer_chained(fn_name) { |
| 70 | + mock_group[fn_name] = function() { |
| 71 | + var call2_args = arguments; |
| 72 | + var call2 = [fn_name].concat(Array.prototype.slice.call(call2_args, 0)); |
| 73 | + target.push([call1, call2]); |
| 74 | + }; |
| 75 | + } |
| 76 | + for (var i = 0; i < group_functions.length; i++) { |
| 77 | + _set_and_defer_chained(group_functions[i]); |
| 78 | + } |
| 79 | + return mock_group; |
| 80 | + }; |
| 81 | + |
| 82 | + // register mixpanel instance |
| 83 | + mixpanel['_i'].push([token, config, name]); |
| 84 | + }; |
| 85 | + |
| 86 | + // Snippet version, used to fail on new features w/ old snippet |
| 87 | + mixpanel['__SV'] = 1.2; |
| 88 | + |
| 89 | + script = document.createElement("script"); |
| 90 | + script.type = "text/javascript"; |
| 91 | + script.async = true; |
| 92 | + |
| 93 | + if (typeof MIXPANEL_CUSTOM_LIB_URL !== 'undefined') { |
| 94 | + script.src = MIXPANEL_CUSTOM_LIB_URL; |
| 95 | + } else if (document.location.protocol === 'file:' && MIXPANEL_LIB_URL.match(/^\/\//)) { |
| 96 | + script.src = 'https:' + MIXPANEL_LIB_URL; |
| 97 | + } else { |
| 98 | + script.src = MIXPANEL_LIB_URL; |
| 99 | + } |
| 100 | + |
| 101 | + first_script = document.getElementsByTagName("script")[0]; |
| 102 | + first_script.parentNode.insertBefore(script, first_script); |
| 103 | + } |
| 104 | +// Pass in current Mixpanel object if it exists (for ppl like Optimizely) |
| 105 | +})(document, window['mixpanel'] || []); |
0 commit comments