forked from mixpanel/mixpanel-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
greenfinch-jslib-snippet.js
124 lines (107 loc) · 5.33 KB
/
greenfinch-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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// ==ClosureCompiler==
// @compilation_level SIMPLE_OPTIMIZATIONS
// @output_file_name greenfinch-jslib-2.2-snippet.min.js
// ==/ClosureCompiler==
/** @define {string} */
var GREENFINCH_LIB_URL = 'https://asset.kcd.co.kr/js/greenfinch.min.js';
(function(document, greenfinch) {
// Only stub out if this is the first time running the snippet.
if (!greenfinch['__SV']) {
var win = window;
// grab the hash params for ce editor immediately in case
// host website changes hash after init
try {
var getHashParam, matches, state, loc = win.location, hash = loc.hash;
getHashParam = function(hash, param) {
matches = hash.match(new RegExp(param + '=([^&]*)'));
return matches ? matches[1] : null;
};
if (hash && getHashParam(hash, 'state')) {
state = JSON.parse(decodeURIComponent(getHashParam(hash, 'state')));
if (state['action'] === 'mpeditor') {
win.sessionStorage.setItem('_mpcehash', hash);
history.replaceState(state['desiredHash'] || '', document.title, loc.pathname + loc.search); // remove ce editor hash
}
}
} catch (e) {}
var script, first_script, gen_fn, functions, i, lib_name = "greenfinch";
window[lib_name] = greenfinch;
greenfinch['_i'] = [];
greenfinch['init'] = function (token, config, name) {
// support multiple greenfinch instances
var target = greenfinch;
if (typeof(name) !== 'undefined') {
target = greenfinch[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 greenfinch interface
// Note: only supports 1 additional level atm, e.g. greenfinch.people.set, not greenfinch.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 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(' ');
functions = "track page register unregister reset".split(' ');
for (i = 0; i < functions.length; i++) {
_set_and_defer(target, functions[i]);
}
// special case for get_group(): chain method calls like greenfinch.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() {
call2_args = arguments;
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 greenfinch instance
greenfinch['_i'].push([token, config, name]);
};
// Snippet version, used to fail on new features w/ old snippet
greenfinch['__SV'] = 1.2;
script = document.createElement("script");
script.type = "text/javascript";
script.async = true;
if (typeof GREENFINCH_CUSTOM_LIB_URL !== 'undefined') {
script.src = GREENFINCH_CUSTOM_LIB_URL;
} else if (document.location.protocol === 'file:' && GREENFINCH_LIB_URL.match(/^\/\//)) {
script.src = 'https:' + GREENFINCH_LIB_URL;
} else {
script.src = GREENFINCH_LIB_URL;
}
first_script = document.getElementsByTagName("script")[0];
first_script.parentNode.insertBefore(script, first_script);
}
})(document, window['greenfinch'] || []);