Skip to content

Commit 69ee7f3

Browse files
committed
refactor: migrate analytics from Universal Analytics to Mixpanel
- Remove deprecated Google Analytics (UA-1211588-20) code - Update trackEvent() to send events via Mixpanel /track endpoint - Update trackPageView() to use Mixpanel - Make trackGaSetField() a no-op (GA-specific) All existing trackEvent calls now automatically go to Mixpanel.
1 parent 12e7ba5 commit 69ee7f3

File tree

1 file changed

+15
-104
lines changed

1 file changed

+15
-104
lines changed

src/analytics.js

Lines changed: 15 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,23 @@
1+
import mixpanel from './services/mixpanel';
2+
13
function log() {
24
if (window.DEBUG) {
35
console.log(Date.now(), ...arguments);
46
}
57
}
68

7-
/* global ga */
8-
99
// eslint-disable-next-line max-params
1010
export function trackEvent(category, action, label, value) {
1111
if (window.DEBUG) {
1212
log('trackevent', category, action, label, value);
1313
return;
1414
}
15-
sendTrackMessage('event', category, action, label, value);
16-
}
17-
18-
async function sendTrackMessage(type, category, action, label, value) {
19-
if (location.href.indexOf('chrome-extension://') !== -1) {
20-
await postAnalyticsDataByApi(type, category, action, label);
21-
} else {
22-
if (window.ga) {
23-
ga('send', type, category, action, label, value);
24-
} else {
25-
setTimeout(
26-
() => sendTrackMessage(type, category, action, label, value),
27-
100,
28-
);
29-
}
30-
}
31-
}
32-
33-
function getCustomerId() {
34-
let cid = localStorage.getItem('cid');
35-
if (cid) {
36-
return cid;
37-
}
38-
cid =
39-
Math.floor(Math.random() * 0x7fffffff) +
40-
'.' +
41-
Math.floor(Date.now() / 1000);
42-
localStorage.setItem('cid', cid);
43-
return cid;
44-
}
45-
46-
async function postAnalyticsDataByApi(type, category, action, label) {
47-
const url = 'https://www.google-analytics.com/collect';
48-
const gaParams = new URLSearchParams();
49-
gaParams.append('v', 1);
50-
gaParams.append('tid', 'UA-1211588-20');
51-
gaParams.append('cid', getCustomerId());
52-
gaParams.append('dl', window.location.host);
53-
gaParams.append('ul', navigator.language);
54-
gaParams.append('de', document.characterSet);
55-
gaParams.append('dt', document.title);
56-
gaParams.append('el', label ? label : null);
57-
gaParams.append('t', type);
58-
gaParams.append('ec', category);
59-
gaParams.append('ea', action);
60-
61-
await fetch(url, {
62-
method: 'POST',
63-
mode: 'no-cors',
64-
cache: 'no-cache',
65-
credentials: 'same-origin',
66-
headers: {
67-
'Content-Type': 'application/x-www-form-urlencoded',
68-
},
69-
redirect: 'follow',
70-
referrerPolicy: 'no-referrer',
71-
body: gaParams,
15+
// Send to Mixpanel
16+
mixpanel.track({
17+
event: action,
18+
category,
19+
label,
20+
value,
7221
});
7322
}
7423

@@ -77,54 +26,16 @@ export function trackPageView(pageName = null) {
7726
log('trackPageView', pageName);
7827
return;
7928
}
80-
sendTrackMessage('pageview', pageName);
29+
mixpanel.track({
30+
event: 'pageView',
31+
category: 'navigation',
32+
label: pageName,
33+
});
8134
}
8235

8336
export function trackGaSetField(fieldName, fieldValue) {
37+
// No-op - GA-specific function, not needed for Mixpanel
8438
if (window.DEBUG) {
85-
log('trackGaSetField', fieldName, fieldValue);
86-
return;
39+
log('trackGaSetField (deprecated)', fieldName, fieldValue);
8740
}
88-
if (window.ga) {
89-
ga('set', fieldName, fieldValue);
90-
} else {
91-
setTimeout(() => trackGaSetField(fieldName, fieldValue), 100);
92-
}
93-
}
94-
95-
// if online, load after sometime
96-
if (
97-
navigator.onLine &&
98-
!window.DEBUG &&
99-
location.href.indexOf('chrome-extension://') === -1
100-
) {
101-
/* eslint-disable */
102-
103-
// prettier-ignore
104-
setTimeout(function () {
105-
(function (i, s, o, g, r, a, m) {
106-
i['GoogleAnalyticsObject'] = r;
107-
i[r] = i[r] || function () {
108-
(i[r].q = i[r].q || []).push(arguments);
109-
}, i[r].l = 1 * new Date();
110-
a = s.createElement(o),
111-
m = s.getElementsByTagName(o)[0];
112-
a.async = 1;
113-
a.src = g;
114-
m.parentNode.insertBefore(a, m);
115-
})(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');
116-
117-
if (location.href.indexOf('chrome-extension://') === -1) {
118-
ga('create', 'UA-1211588-20');
119-
} else {
120-
ga('create', 'UA-1211588-20', {
121-
'cookieDomain': 'none'
122-
});
123-
// required for chrome extension protocol
124-
ga('set', 'checkProtocolTask', function () { /* nothing */
125-
});
126-
}
127-
}, 100);
128-
129-
/* eslint-enable */
13041
}

0 commit comments

Comments
 (0)