Skip to content

Commit

Permalink
sendExponeaEvent with store watcher for later init
Browse files Browse the repository at this point in the history
  • Loading branch information
woophi committed Feb 2, 2024
1 parent 8477648 commit d6d25be
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
},
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
"source.organizeImports": "explicit"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sensearena/ui",
"version": "0.2.60",
"version": "0.2.61",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
Expand Down
27 changes: 23 additions & 4 deletions src/utils/sendExponeaEvent.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
declare global {
interface Window {
exponea: {
track: (eventName: string, eventData: { [key: string]: any }) => void;
track: (eventName: string, eventData: Record<string, any>) => void;
identify: (
userObject: { [key: string]: any },
options?: { [key: string]: any },
userObject: Record<string, any>,
options?: Record<string, any>,
successCallback?: () => void,
errorCallback?: () => void,
immediate?: boolean,
Expand All @@ -13,9 +13,28 @@ declare global {
}
}

export const sendExponeaEvent = (eventName: string, eventData: { [key: string]: any }, email?: string) => {
let exponeaEventsStore: Record<string, Record<string, any>> = {};

const runStoreWatcher = () => {
const hasStoreValues = Object.keys(exponeaEventsStore).length;
const timer = setInterval(() => {
if (window.exponea && hasStoreValues) {
clearInterval(timer);
Object.keys(exponeaEventsStore).forEach((eventName: keyof typeof exponeaEventsStore) => {
window.exponea.track(eventName, exponeaEventsStore[eventName]);
});
}
}, 1000);
};

export const sendExponeaEvent = (eventName: string, eventData: Record<string, any>, email?: string) => {
if (!window.exponea) {
console.debug('[SA]: skip event, BR not inited', eventName);
exponeaEventsStore = {
...exponeaEventsStore,
eventName: eventData,
};
runStoreWatcher();
return;
}
withIdentify(() => {
Expand Down

0 comments on commit d6d25be

Please sign in to comment.