-
-
Notifications
You must be signed in to change notification settings - Fork 160
/
mixpanel.js
88 lines (74 loc) · 3.52 KB
/
mixpanel.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
import Ember from 'ember';
import canUseDOM from '../utils/can-use-dom';
import objectTransforms from '../utils/object-transforms';
import BaseAdapter from './base';
const {
assert,
$,
get,
} = Ember;
const {
without,
compact,
isPresent
} = objectTransforms;
const assign = Ember.assign || Ember.merge;
export default BaseAdapter.extend({
toStringExtension() {
return 'Mixpanel';
},
init() {
const config = get(this, 'config');
const { token } = config;
assert(`[ember-metrics] You must pass a valid \`token\` to the ${this.toString()} adapter`, token);
if (canUseDOM) {
/* jshint ignore:start */
(function(e,a){if(!a.__SV){var b=window;try{var c,l,i,j=b.location,g=j.hash;c=function(a,b){return(l=a.match(RegExp(b+"=([^&]*)")))?l[1]:null};g&&c(g,"state")&&(i=JSON.parse(decodeURIComponent(c(g,"state"))),"mpeditor"===i.action&&(b.sessionStorage.setItem("_mpcehash",g),history.replaceState(i.desiredHash||"",e.title,j.pathname+j.search)))}catch(m){}var k,h;window.mixpanel=a;a._i=[];a.init=function(b,c,f){function e(b,a){var c=a.split(".");2==c.length&&(b=b[c[0]],a=c[1]);b[a]=function(){b.push([a].concat(Array.prototype.slice.call(arguments,
0)))}}var d=a;"undefined"!==typeof f?d=a[f]=[]:f="mixpanel";d.people=d.people||[];d.toString=function(b){var a="mixpanel";"mixpanel"!==f&&(a+="."+f);b||(a+=" (stub)");return a};d.people.toString=function(){return d.toString(1)+".people (stub)"};k="disable time_event track track_pageview track_links track_forms register register_once alias unregister identify name_tag set_config reset people.set people.set_once people.increment people.append people.union people.track_charge people.clear_charges people.delete_user".split(" ");
for(h=0;h<k.length;h++)e(d,k[h]);a._i.push([b,c,f])};a.__SV=1.2;b=e.createElement("script");b.type="text/javascript";b.async=!0;b.src="undefined"!==typeof MIXPANEL_CUSTOM_LIB_URL?MIXPANEL_CUSTOM_LIB_URL:"file:"===e.location.protocol&&"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js".match(/^\/\//)?"https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js":"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js";c=e.getElementsByTagName("script")[0];c.parentNode.insertBefore(b,c)}})(document,window.mixpanel||[]);
mixpanel.init(token);
/* jshint ignore:end */
}
},
identify(options = {}) {
const compactedOptions = compact(options);
const { distinctId } = compactedOptions;
const props = without(compactedOptions, 'distinctId');
if (isPresent(props) && canUseDOM) {
window.mixpanel.identify(distinctId);
window.mixpanel.people.set(props);
} else if (canUseDOM){
window.mixpanel.identify(distinctId);
}
},
trackEvent(options = {}) {
const compactedOptions = compact(options);
const { event } = compactedOptions;
const props = without(compactedOptions, 'event');
if (isPresent(props) && canUseDOM) {
window.mixpanel.track(event, props);
} else if (canUseDOM){
window.mixpanel.track(event);
}
},
trackPage(options = {}) {
const event = { event: 'page viewed' };
const mergedOptions = assign(event, options);
this.trackEvent(mergedOptions);
},
alias(options = {}) {
const compactedOptions = compact(options);
const { alias, original } = compactedOptions;
if (original && canUseDOM) {
window.mixpanel.alias(alias, original);
} else if (canUseDOM){
window.mixpanel.alias(alias);
}
},
willDestroy() {
if (canUseDOM) {
$('script[src*="mixpanel"]').remove();
delete window.mixpanel;
}
}
});