Skip to content

Commit 9bb5f63

Browse files
author
Jack Crish
committed
add debug on/off switch. Move funcitonality to useEffect
1 parent 91f129e commit 9bb5f63

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

src/app/containers/MainContainer.tsx

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,6 @@ const mixpanel = require("mixpanel").init("12fa2800ccbf44a5c36c37bc9776e4c0", {
1919
protocol: "https"
2020
});
2121

22-
//console.log("MP ", Object.keys(mixpanel));
23-
//attempt to read cookies
24-
const user = new MPID();
25-
26-
//set current user cookie if it does not exist in cookies;
27-
if(user.checkDocumentCookie(document)) {
28-
console.log("Reactime cookie found ");
29-
user.getCookie();
30-
mixpanel.people.increment(user.get_dId(), "times");
31-
}else{
32-
console.log("No reactime cookie found. Attempting set cookie.");
33-
user.setCookie();
34-
mixpanel.people.set(user.get_dId(), { "times": 1 });
35-
}
36-
37-
38-
39-
function mpClickTrack(e) {
40-
mixpanel.track("click", { distinct_id : user.distinct_id, where : e?.target?.innerText } );
41-
};
42-
43-
document.addEventListener("click", mpClickTrack);
4422

4523

4624
function MainContainer(): any {
@@ -95,6 +73,42 @@ function MainContainer(): any {
9573
dispatch(setPort(port));
9674
});
9775

76+
/**
77+
* get set cookies for mixpanel analytics
78+
**/
79+
useEffect( () => {
80+
//console.log("MP ", Object.keys(mixpanel));
81+
//attempt to read cookies
82+
const user = new MPID();
83+
/**
84+
* If developing turn tracking off by setting user.debug to true;
85+
* End goal: set user.debug variable in npm run dev
86+
*/
87+
user.debug = true;
88+
89+
if (!user.debug) {
90+
//set current user cookie if it does not exist in cookies;
91+
if (user.checkDocumentCookie(document)) {
92+
user.getCookie();
93+
mixpanel.people.increment(user.get_dId(), "times");
94+
} else {
95+
user.setCookie();
96+
mixpanel.people.set(user.get_dId(), { times: 1 });
97+
}
98+
}
99+
100+
function mpClickTrack(e) {
101+
if (!user.debug) {
102+
mixpanel.track("click", {
103+
distinct_id: user.distinct_id,
104+
where: e?.target?.innerText,
105+
});
106+
}
107+
}
108+
109+
document.addEventListener("click", mpClickTrack);
110+
}, [])
111+
98112
if (!tabs[currentTab]) {
99113
return (
100114
<div className="error-container">

src/app/user_id/user_id.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class MPID{
88
constructor(){
99
this.cookie = null;
1010
this.distinct_id = null;
11-
this.hasCookie = false;
11+
this.debug = false;
1212
}
1313

1414
setCookie(){
@@ -20,7 +20,7 @@ class MPID{
2020
if(this.cookie){
2121
return this.cookie;
2222
}else{
23-
throw new Error("Unable to set cookie. Cookie or Distinct Id is falsey");
23+
throw new Error("Unable to set cookie. Cookie is falsey");
2424
}
2525

2626
}

0 commit comments

Comments
 (0)