Skip to content

Commit dcf4c9c

Browse files
committed
Cleanup.
1 parent 37e483c commit dcf4c9c

File tree

4 files changed

+85
-44
lines changed

4 files changed

+85
-44
lines changed

.DS_Store

0 Bytes
Binary file not shown.

demo/main.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ window.onGleapLoaded = () => {
44
console.log("LOADED.");
55
};
66

7-
// Gleap.setApiUrl("http://0.0.0.0:9000");
7+
Gleap.setApiUrl("http://0.0.0.0:9000");
8+
9+
Gleap.identify("123", {
10+
name: "XOXO",
11+
email: "test@gleap.io",
12+
});
813

914
// Sample for feedback type options
1015
Gleap.setMenuOptions([

src/Gleap.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class Gleap {
120120
* Initializes the SDK
121121
* @param {*} sdkKey
122122
*/
123-
static initialize(sdkKey, userId, userHash) {
123+
static initialize(sdkKey) {
124124
const instance = this.getInstance();
125125

126126
if (instance.initialized) {
@@ -131,7 +131,7 @@ class Gleap {
131131

132132
const sessionInstance = Session.getInstance();
133133
sessionInstance.sdkKey = sdkKey;
134-
sessionInstance.startSession(userId, userHash);
134+
sessionInstance.startSession();
135135
sessionInstance.setOnSessionReady(() => {
136136
if (
137137
document.readyState === "complete" ||
@@ -151,11 +151,12 @@ class Gleap {
151151
}
152152

153153
/**
154-
* Update user data
154+
* Indentifies the user session
155+
* @param {string} userId
155156
* @param {*} userData
156157
*/
157-
static identify(userId, userHash, userData) {
158-
Session.getInstance().startSession(userId, userHash, userData);
158+
static identify(userId, userData) {
159+
Session.getInstance().identifySession(userId, userData);
159160
}
160161

161162
/**

src/Session.js

Lines changed: 73 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ export default class Session {
22
apiUrl = "https://api.gleap.io";
33
sdkKey = null;
44
session = {
5-
id: null,
6-
hash: null,
7-
type: null,
5+
gleapId: null,
6+
gleapHash: null,
87
name: "",
98
email: "",
109
};
@@ -33,18 +32,17 @@ export default class Session {
3332
};
3433

3534
injectSession = (http) => {
36-
if (http) {
35+
if (http && this.session) {
3736
http.setRequestHeader("Api-Token", this.sdkKey);
38-
http.setRequestHeader("Gleap-Id", this.session.id);
39-
http.setRequestHeader("Gleap-Hash", this.session.hash);
37+
http.setRequestHeader("Gleap-Id", this.session.gleapId);
38+
http.setRequestHeader("Gleap-Hash", this.session.gleapHash);
4039
}
4140
};
4241

4342
clearSession = (renewSession = true) => {
4443
try {
45-
localStorage.removeItem(`bb-session-id`);
46-
localStorage.removeItem(`bb-session-hash`);
47-
localStorage.removeItem(`bb-session-type`);
44+
localStorage.removeItem(`gleap-id`);
45+
localStorage.removeItem(`gleap-hash`);
4846
} catch (exp) {}
4947

5048
this.session = {
@@ -61,39 +59,22 @@ export default class Session {
6159
}
6260
};
6361

64-
startSession = (userId, userHash, userData) => {
62+
startSession = () => {
6563
const self = this;
6664
return new Promise((resolve, reject) => {
6765
const http = new XMLHttpRequest();
68-
http.open("POST", this.apiUrl + "/sessions");
66+
http.open("POST", self.apiUrl + "/sessions");
6967
http.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
70-
http.setRequestHeader("Api-Token", this.sdkKey);
71-
72-
// Set the guest id & hash
68+
http.setRequestHeader("Api-Token", self.sdkKey);
7369
try {
74-
const sessionType = localStorage.getItem(`bb-session-type`);
75-
const sessionId = localStorage.getItem(`bb-session-id`);
76-
const sessionHash = localStorage.getItem(`bb-session-hash`);
77-
if (sessionType === "GUEST") {
78-
if (sessionId && sessionHash) {
79-
http.setRequestHeader("Guest-Id", sessionId);
80-
http.setRequestHeader("Guest-Hash", sessionHash);
81-
}
82-
} else {
83-
// Existing session from cache.
84-
if (sessionId && sessionHash && !userId && !userHash) {
85-
http.setRequestHeader("User-Id", sessionId);
86-
http.setRequestHeader("User-Hash", sessionHash);
87-
}
70+
const gleapId = localStorage.getItem(`gleap-id`);
71+
const gleapHash = localStorage.getItem(`gleap-hash`);
72+
if (gleapId && gleapHash) {
73+
http.setRequestHeader("Gleap-Id", gleapId);
74+
http.setRequestHeader("Gleap-Hash", gleapHash);
8875
}
8976
} catch (exp) {}
9077

91-
// Additionally set the user id
92-
if (userId && userHash) {
93-
http.setRequestHeader("User-Id", userId);
94-
http.setRequestHeader("User-Hash", userHash);
95-
}
96-
9778
http.onerror = (error) => {
9879
self.clearSession(false);
9980
reject();
@@ -105,9 +86,8 @@ export default class Session {
10586
const sessionData = JSON.parse(http.responseText);
10687

10788
try {
108-
localStorage.setItem(`bb-session-id`, sessionData.id);
109-
localStorage.setItem(`bb-session-hash`, sessionData.hash);
110-
localStorage.setItem(`bb-session-type`, sessionData.type);
89+
localStorage.setItem(`gleap-id`, sessionData.gleapId);
90+
localStorage.setItem(`gleap-hash`, sessionData.gleapHash);
11191
} catch (exp) {}
11292

11393
self.session = sessionData;
@@ -131,7 +111,62 @@ export default class Session {
131111
}
132112
}
133113
};
134-
http.send(JSON.stringify(userData));
114+
http.send(JSON.stringify({}));
115+
});
116+
};
117+
118+
identifySession = (userId, userData) => {
119+
const self = this;
120+
return new Promise((resolve, reject) => {
121+
// Wait for gleap session to be ready.
122+
this.setOnSessionReady(function () {
123+
const http = new XMLHttpRequest();
124+
http.open("POST", self.apiUrl + "/sessions/identify");
125+
http.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
126+
http.setRequestHeader("Api-Token", self.sdkKey);
127+
try {
128+
const gleapId = localStorage.getItem(`gleap-id`);
129+
const gleapHash = localStorage.getItem(`gleap-hash`);
130+
if (gleapId && gleapHash) {
131+
http.setRequestHeader("Gleap-Id", gleapId);
132+
http.setRequestHeader("Gleap-Hash", gleapHash);
133+
}
134+
} catch (exp) {}
135+
136+
http.onerror = () => {
137+
reject();
138+
};
139+
http.onreadystatechange = function (e) {
140+
if (http.readyState === XMLHttpRequest.DONE) {
141+
if (http.status === 200 || http.status === 201) {
142+
try {
143+
const sessionData = JSON.parse(http.responseText);
144+
145+
try {
146+
localStorage.setItem(`gleap-id`, sessionData.gleapId);
147+
localStorage.setItem(`gleap-hash`, sessionData.gleapHash);
148+
} catch (exp) {}
149+
150+
self.session = sessionData;
151+
self.ready = true;
152+
153+
resolve(sessionData);
154+
} catch (exp) {
155+
reject(exp);
156+
}
157+
} else {
158+
self.clearSession(false);
159+
reject();
160+
}
161+
}
162+
};
163+
http.send(
164+
JSON.stringify({
165+
...userData,
166+
userId,
167+
})
168+
);
169+
});
135170
});
136171
};
137172
}

0 commit comments

Comments
 (0)