-
Notifications
You must be signed in to change notification settings - Fork 649
Expand file tree
/
Copy pathmixpanel.js
More file actions
80 lines (65 loc) · 1.76 KB
/
mixpanel.js
File metadata and controls
80 lines (65 loc) · 1.76 KB
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
import mixpanel from 'mixpanel-browser';
const MIXPANEL_TOKEN = 'ff3948c5215e478a83d6047952d7302a';
class MixpanelAnalytics {
constructor() {
this.initialized = false;
}
init() {
if (this.initialized) return;
mixpanel.init(MIXPANEL_TOKEN, {
debug: true,
track_pageview: true,
persistence: 'localStorage'
});
this.initialized = true;
console.log('Mixpanel initialized successfully');
}
track(eventName, properties = {}) {
if (!this.initialized) {
console.warn('Mixpanel not initialized. Call init() first.');
return;
}
mixpanel.track(eventName, properties);
console.log(`Mixpanel Event Tracked: ${eventName}`, properties);
}
identify(userId, userProperties = {}) {
if (!this.initialized) {
console.warn('Mixpanel not initialized. Call init() first.');
return;
}
mixpanel.identify(userId);
mixpanel.people.set(userProperties);
console.log(`Mixpanel User Identified: ${userId}`, userProperties);
}
trackPageView(pageName, properties = {}) {
this.track('Page View', {
page: pageName,
url: window.location.href,
path: window.location.pathname,
referrer: document.referrer,
...properties
});
}
trackButtonClick(buttonName, properties = {}) {
this.track('Button Click', {
button_name: buttonName,
page: window.location.pathname,
...properties
});
}
trackFormSubmit(formName, properties = {}) {
this.track('Form Submit', {
form_name: formName,
page: window.location.pathname,
...properties
});
}
reset() {
if (this.initialized) {
mixpanel.reset();
console.log('Mixpanel reset');
}
}
}
const analytics = new MixpanelAnalytics();
export default analytics;