Skip to content

Commit bab0d2b

Browse files
committed
v8.4.3
1 parent fa572ef commit bab0d2b

File tree

8 files changed

+75
-8
lines changed

8 files changed

+75
-8
lines changed

build/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export namespace Gleap {
8989
function openFeatureRequests(showBackButton?: boolean): void;
9090
function close(): void;
9191
function hide(): void;
92+
function setUseCookies(useCookies: boolean): void;
9293
function setEnvironment(environment: "dev" | "staging" | "prod"): void;
9394
function showFeedbackButton(show: boolean): void;
9495
function startFeedbackFlow(

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gleap",
3-
"version": "8.4.2",
3+
"version": "8.4.3",
44
"main": "build/index.js",
55
"scripts": {
66
"start": "webpack serve",

published/8.4.3/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

published/latest/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Gleap.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ class Gleap {
175175
GleapSession.getInstance().clearSession(0, false);
176176
}
177177

178+
/**
179+
* Enable or disable Gleap session tracking through cookies.
180+
* @param {*} useCookies
181+
*/
182+
static setUseCookies(useCookies) {
183+
GleapSession.getInstance().useCookies = useCookies;
184+
}
185+
178186
/**
179187
* Indentifies the user session
180188
* @param {string} userId

src/GleapHelper.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,39 @@ export const saveToGleapCache = (key, data) => {
102102
}
103103
};
104104

105+
export const setGleapCookie = (name, value, days) => {
106+
try {
107+
var expires = "";
108+
if (days) {
109+
var date = new Date();
110+
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
111+
expires = "; expires=" + date.toUTCString();
112+
}
113+
const host = window.location.host.split(":")[0];
114+
document.cookie = name + "=" + (value || "") + expires + "; path=/; domain=" + host;
115+
} catch (exp) { }
116+
}
117+
118+
export const getGleapCookie = (name) => {
119+
try {
120+
var nameEQ = name + "=";
121+
var ca = document.cookie.split(';');
122+
for (var i = 0; i < ca.length; i++) {
123+
var c = ca[i];
124+
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
125+
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
126+
}
127+
} catch (exp) { }
128+
return null;
129+
}
130+
131+
export const eraseGleapCookie = (name) => {
132+
try {
133+
const host = window.location.host.split(":")[0];
134+
document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT; Domain=' + host;
135+
} catch (exp) { }
136+
}
137+
105138
export const getDOMElementDescription = (element, html = true) => {
106139
var innerText = truncateString(element.innerText || '', 40).replace(/(\r\n|\n|\r)/gm, "").replace(/ +(?= )/g, '');
107140
var elementId = "";

src/GleapSession.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { GleapFrameManager, GleapNotificationManager } from "./Gleap";
2-
import { loadFromGleapCache, saveToGleapCache } from "./GleapHelper";
2+
import { eraseGleapCookie, getGleapCookie, loadFromGleapCache, saveToGleapCache, setGleapCookie } from "./GleapHelper";
33

44
export default class GleapSession {
55
apiUrl = "https://api.gleap.io";
66
sdkKey = null;
77
updatingSession = false;
8+
useCookies = true;
89
session = {
910
gleapId: null,
1011
gleapHash: null,
@@ -94,6 +95,12 @@ export default class GleapSession {
9495
saveToGleapCache(`session-${this.sdkKey}`, null);
9596
} catch (exp) { }
9697

98+
if (this.useCookies) {
99+
try {
100+
eraseGleapCookie(`session-${this.sdkKey}`);
101+
} catch (exp) { }
102+
}
103+
97104
this.ready = false;
98105
this.session = {
99106
id: null,
@@ -128,6 +135,9 @@ export default class GleapSession {
128135
}
129136

130137
saveToGleapCache(`session-${this.sdkKey}`, session);
138+
if (this.useCookies) {
139+
setGleapCookie(`session-${this.sdkKey}`, encodeURIComponent(JSON.stringify(session)), 365);
140+
}
131141

132142
this.session = session;
133143
this.ready = true;
@@ -136,10 +146,24 @@ export default class GleapSession {
136146
};
137147

138148
startSession = (attemp = 0) => {
139-
// Check if session is already ready.
140-
const cachedSession = loadFromGleapCache(`session-${this.sdkKey}`);
141-
if (cachedSession) {
142-
this.validateSession(cachedSession);
149+
// Check if we already have a session cookie.
150+
try {
151+
if (this.useCookies) {
152+
const sessionCookie = getGleapCookie(`session-${this.sdkKey}`);
153+
if (sessionCookie) {
154+
const sessionData = JSON.parse(decodeURIComponent(sessionCookie));
155+
156+
this.validateSession(sessionData);
157+
}
158+
}
159+
} catch (exp) { }
160+
161+
if (!(this.session && this.session.gleapId && this.session.gleapId.length > 0)) {
162+
// Check if session is cached.
163+
const cachedSession = loadFromGleapCache(`session-${this.sdkKey}`);
164+
if (cachedSession) {
165+
this.validateSession(cachedSession);
166+
}
143167
}
144168

145169
const self = this;

0 commit comments

Comments
 (0)