Skip to content

Commit c7a7e3e

Browse files
author
Bernard Gatt
committed
Adds custom attribute store
1 parent aaae828 commit c7a7e3e

File tree

4 files changed

+104
-2
lines changed

4 files changed

+104
-2
lines changed

examples/index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ <h1>Gist for Web</h1>
1919
<a href="#" class="button" onClick="embedBanner()">Embed Banner</a>
2020
<a href="#" class="button" onClick="embedFloatingMessage()">Embed Floating Message</a>
2121
<a href="#" class="button" onClick="embedHTMLFloatingMessage()">Embed HTML Floating Message</a>
22+
<a href="#" class="button" onClick="addAnonymousCustomAttribute()">Set Anonymous Custom Attribute</a>
2223
<a href="#" class="button" onClick="logIn()">Log In</a>
2324
<a href="#" class="button" onClick="logOut()">Log Out</a>
2425
</div>
@@ -32,7 +33,7 @@ <h1>Gist for Web</h1>
3233
Dev Setup Options
3334
{ siteId: "siteid", dataCenter: "us", env: "dev", logging: true }
3435
*/
35-
Gist.setup({ siteId: "a5ec106751ef4b34a0b9", dataCenter: "eu", logging: true, env: "prod" });
36+
Gist.setup({ siteId: "a5ec106751ef4b34a0b9", dataCenter: "eu", logging: true, env: "prod", useAnonymousSession: true });
3637
Gist.setUserToken("ABC123");
3738
Gist.setCurrentRoute("home");
3839

@@ -116,6 +117,10 @@ <h1>Gist for Web</h1>
116117
}
117118
}, "x-gist-floating-bottom-right");
118119
}
120+
121+
function addAnonymousCustomAttribute() {
122+
Gist.setCustomAttribute("cio_anonymous_id", "123456");
123+
}
119124
</script>
120125
</body>
121126
</html>

src/gist.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { startQueueListener, checkMessageQueue, stopSSEListener } from "./manage
44
import { setUserToken, clearUserToken, useGuestSession } from "./managers/user-manager";
55
import { showMessage, embedMessage, hideMessage, removePersistentMessage, fetchMessageByInstanceId, logBroadcastDismissedLocally } from "./managers/message-manager";
66
import { setUserLocale } from "./managers/locale-manager";
7+
import { setCustomAttribute, clearCustomAttributes, removeCustomAttribute } from "./managers/custom-attribute-manager";
78
import { setupPreview } from "./utilities/preview-mode";
89

910
export default class {
@@ -58,6 +59,18 @@ export default class {
5859
setUserLocale(userLocale);
5960
}
6061

62+
static setCustomAttribute(key, value) {
63+
return setCustomAttribute(key, value);
64+
}
65+
66+
static clearCustomAttributes() {
67+
clearCustomAttributes();
68+
}
69+
70+
static removeCustomAttribute(key) {
71+
return removeCustomAttribute(key);
72+
}
73+
6174
static async clearUserToken() {
6275
if (this.config.isPreviewSession) return;
6376
clearUserToken();
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { log } from '../utilities/log';
2+
import { setKeyToLocalStore, getKeyFromLocalStore, clearKeyFromLocalStore } from '../utilities/local-storage';
3+
4+
const customAttributesLocalStoreName = "gist.web.customAttributes";
5+
const defaultExpiryInDays = 30;
6+
7+
// Internal map to store custom attributes in memory
8+
let customAttributesMap = new Map();
9+
10+
function loadCustomAttributesFromStorage() {
11+
const storedAttributes = getKeyFromLocalStore(customAttributesLocalStoreName);
12+
if (storedAttributes) {
13+
try {
14+
customAttributesMap = new Map(storedAttributes);
15+
} catch {
16+
customAttributesMap = new Map();
17+
}
18+
} else {
19+
customAttributesMap = new Map();
20+
}
21+
}
22+
23+
function saveCustomAttributesToStorage() {
24+
const attributesArray = Array.from(customAttributesMap.entries());
25+
const expiryDate = new Date();
26+
expiryDate.setDate(expiryDate.getDate() + defaultExpiryInDays);
27+
28+
setKeyToLocalStore(customAttributesLocalStoreName, attributesArray, expiryDate);
29+
log(`Saved ${customAttributesMap.size} custom attributes to storage with TTL of ${defaultExpiryInDays} days`);
30+
}
31+
32+
loadCustomAttributesFromStorage();
33+
34+
export function setCustomAttribute(key, value) {
35+
if (!key || typeof key !== 'string') {
36+
log(`Invalid key for custom attribute: ${key}`);
37+
return false;
38+
}
39+
40+
customAttributesMap.set(key, value);
41+
saveCustomAttributesToStorage();
42+
log(`Set custom attribute "${key}" to "${value}"`);
43+
return true;
44+
}
45+
46+
export function getCustomAttribute(key) {
47+
if (!key || typeof key !== 'string') {
48+
log(`Invalid key for custom attribute: ${key}`);
49+
return null;
50+
}
51+
52+
return customAttributesMap.get(key) || null;
53+
}
54+
55+
export function getAllCustomAttributes() {
56+
return new Map(customAttributesMap);
57+
}
58+
59+
export function clearCustomAttributes() {
60+
customAttributesMap.clear();
61+
clearKeyFromLocalStore(customAttributesLocalStoreName);
62+
log(`Cleared all custom attributes`);
63+
}
64+
65+
export function removeCustomAttribute(key) {
66+
if (!key || typeof key !== 'string') {
67+
log(`Invalid key for custom attribute: ${key}`);
68+
return false;
69+
}
70+
71+
const existed = customAttributesMap.has(key);
72+
customAttributesMap.delete(key);
73+
74+
if (customAttributesMap.size > 0) {
75+
saveCustomAttributesToStorage();
76+
} else {
77+
clearKeyFromLocalStore(customAttributesLocalStoreName);
78+
}
79+
80+
log(`Removed custom attribute "${key}"`);
81+
return existed;
82+
}

src/managers/message-manager.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from "./message-component-manager";
1919
import { resolveMessageProperties } from "./gist-properties-manager";
2020
import { positions, addPageElement } from "./page-component-manager";
21+
import { getAllCustomAttributes } from "./custom-attribute-manager";
2122
import { checkMessageQueue } from "./queue-manager";
2223
import { isMessageBroadcast, markBroadcastAsSeen, markBroadcastAsDismissed } from './message-broadcast-manager';
2324
import { markUserQueueMessageAsSeen } from './message-user-queue-manager';
@@ -126,7 +127,8 @@ function loadMessageComponent(message, elementId = null) {
126127
messageId: message.messageId,
127128
instanceId: message.instanceId,
128129
livePreview: false,
129-
properties: message.properties
130+
properties: message.properties,
131+
customAttributes: Object.fromEntries(getAllCustomAttributes())
130132
}
131133
var url = `${settings.GIST_VIEW_ENDPOINT[Gist.config.env]}/index.html`
132134
window.addEventListener('message', handleGistEvents);

0 commit comments

Comments
 (0)