-
Notifications
You must be signed in to change notification settings - Fork 13
/
web.ts
112 lines (91 loc) · 2.91 KB
/
web.ts
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
import { WebPlugin } from '@capacitor/core';
import mixpanel from 'mixpanel-browser';
import type { MixpanelPlugin } from './definitions';
declare global {
interface Window {
mixpanel: any;
}
}
export class MixpanelWeb extends WebPlugin implements MixpanelPlugin {
private superProperties = {};
constructor() {
super();
window.mixpanel = mixpanel;
}
async initialize(options: {
token: string;
autotrack?: boolean;
optOutByDefault?: boolean;
debug?: boolean;
ipCollection?: boolean;
}): Promise<void> {
mixpanel.init(options.token, {
autotrack: options.autotrack ?? true,
opt_out_tracking_by_default: options.optOutByDefault ?? false,
debug: options.debug ?? false,
ip: options.ipCollection ?? true,
});
return Promise.resolve();
}
async distinctId(): Promise<{ value: string }> {
return Promise.resolve({ value: mixpanel.get_distinct_id() });
}
async track(options: { event: string; properties: any }): Promise<void> {
mixpanel.track(options.event, options.properties);
return Promise.resolve();
}
async identify(options: { distinctId: string }): Promise<void> {
mixpanel.identify(options.distinctId);
return Promise.resolve();
}
async alias(options: { alias: string; distinctId: string }): Promise<void> {
mixpanel.alias(options.alias, options.distinctId);
return Promise.resolve();
}
async reset(): Promise<void> {
mixpanel.reset();
return Promise.resolve();
}
async clearSuperProperties(): Promise<void> {
for (const k of Object.keys(this.superProperties)) {
mixpanel.unregister(k);
}
this.superProperties = {};
return Promise.resolve();
}
async currentSuperProperties(): Promise<{ properties: any }> {
return Promise.resolve({
properties: this.superProperties,
});
}
async registerSuperProperties(options: { properties: any }): Promise<void> {
mixpanel.register(options.properties);
return Promise.resolve();
}
async setProfile(options: { properties: any }): Promise<void> {
mixpanel.people.set(options.properties);
return Promise.resolve();
}
async setProfileUnion(options: { properties: any }): Promise<void> {
mixpanel.people.union(options.properties);
return Promise.resolve();
}
async deleteProfile(): Promise<void> {
mixpanel.people.delete_user();
}
async trackCharge(options: { amount: number; properties: any }): Promise<void> {
mixpanel.people.track_charge(options.amount, options.properties);
}
async flush(): Promise<void> {
// NOT IMPLEMENTED FOR WEB
}
async optInTracking(options: { distinctId?: string; properties?: any }): Promise<void> {
mixpanel.opt_in_tracking(options.properties);
}
async optOutTracking(): Promise<void> {
mixpanel.opt_out_tracking();
}
async hasOptedOutTracking(): Promise<{ value: boolean }> {
return { value: mixpanel.has_opted_out_tracking() };
}
}