Skip to content

Commit e3aa7e0

Browse files
author
Jack Crish
committed
refactor
1 parent f848550 commit e3aa7e0

File tree

3 files changed

+54
-30
lines changed

3 files changed

+54
-30
lines changed

src/app/containers/MainContainer.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ const mixpanel = require("mixpanel").init("12fa2800ccbf44a5c36c37bc9776e4c0", {
2424
const user = new MPID();
2525

2626
//set current user cookie if it does not exist in cookies;
27-
if(!user.checkDocumentCookie(document)) {
28-
console.log(" set user cookie ");
29-
user.setCookie(document);
30-
mixpanel.people.set( user.get_dId(), {"times": 1} );
27+
if(user.checkDocumentCookie(document)) {
28+
console.log("Reactime user cookie defined. Check dId ");
29+
if( user.distinct_id ){
30+
try{
31+
user.setCookie();
32+
}catch(e){
33+
mixpanel.track(`Cannot set user cookie ${e}`);
34+
}
35+
}else{
3136

32-
}else{
33-
//if a user visits increment the visit count;
34-
//probably not necessary because of mixpanel, but .set() was acting strangely so this
35-
// is an experiment to figure it out.
36-
console.log (" increment times ");
37-
mixpanel.people.increment( user.get_dId(), "times" );
37+
}
3838
}
3939

4040
function mpClickTrack(e) {
41-
mixpanel.track( { event: "click" } );
41+
mixpanel.track({ event: "click", properties: { "$distinct_id": user.distinct_id } });
4242
};
4343

4444
document.addEventListener("click", mpClickTrack);

src/app/user_id/user_id.js

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,68 @@
11
const crypto = require("crypto");
22
const cookie = require("cookie");
3-
3+
const mixpanel = require("mixpanel").init("12fa2800ccbf44a5c36c37bc9776e4c0", {
4+
debug: true,
5+
protocol: "https"
6+
});
47
class MPID{
58
constructor(){
69
this.cookie = null;
710
this.distinct_id = null;
811
this.hasCookie = false;
912
}
1013

11-
setCookie(doc){
14+
setCookie(){
1215
console.log(" Set Cookie ");
13-
//create a string of random data
16+
//create a string of random data
1417
this.cookie = cookie.serialize("reactime", crypto.randomBytes(64).toString('hex') );
15-
doc.cookie = this.cookie;
16-
console.log(" Check if Cookie set ", doc.cookie);
17-
return this.cookie;
18+
this.distinct_id = this.cookie?.reactime?.slice(0, 20);
19+
20+
if(this.cookie && this.distinct_id){
21+
return this.cookie;
22+
}else{
23+
throw new Error("Unable to set cookie. Cookie or Distinct Id is falsey");
24+
}
25+
1826
}
1927

2028
getCookie(){
21-
console.log(" get Cookie");
22-
return this.cookie;
29+
console.log(" get Cookie");
30+
31+
if(this.cookie){
32+
return this.cookie;
33+
}else{
34+
throw new Error("Cookie truthy, but unreturnable");
35+
}
36+
2337
}
2438

2539
checkDocumentCookie(doc){
2640
console.log(" Check Document Cookie ", cookie.parse( doc.cookie ));
27-
if( doc.cookie ){
28-
console.log( "doc cookie parsed: ", cookie.parse(doc.cookie)?.reactime );
29-
let a = cookie.parse(doc.cookie)?.reactime;
30-
if(a){
31-
return true;
32-
}else{
33-
return false;
34-
}
41+
let parsedCookie = cookie.parse(doc.cookie);
42+
43+
if( parsedCookie?.reactime ){
44+
console.log( "reactime cookie found" );
45+
this.cookie = parsedCookie?.reactime;
46+
47+
if(!this.distinct_id){
48+
console.log(" set dId");
49+
this.distinct_id = parsedCookie?.reactime?.slice(0, 20);
50+
}
3551
}else{
52+
console.log("No reactime cookie found. Attempting setCookie");
53+
this.setCookie();
3654
return false;
3755
}
3856
}
3957

4058
get_dId(){
41-
return this.cookie ? this.cookie.slice(0, 20) : null;
59+
try{
60+
if(this.distinct_id ) {
61+
return this.distinct_id;
62+
}
63+
}catch( e ){
64+
throw new Error(`unable to set cookie. Reason: ${e}. `);
65+
}
4266
}
4367
}
4468

src/app/user_id/user_id.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ describe("Test new user class constructor", () => {
99
expect(user).toBeInstanceOf(MPID);
1010
})
1111

12-
xit("new user cookie should be set", () => {
13-
12+
it("new user cookie should be set", () => {
13+
expect(user.setCookie()).toHaveLength();
1414
})
1515

1616
it("checkDocumentCookie should return a boolean", () => {

0 commit comments

Comments
 (0)