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