forked from binary-com/deriv-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Aswathy/fix: test analytics for country screen (binary-com#16410)
* fix: test analytics for country screen * fix: added analytics initializer * fix: add the condition for analytics * fix: async call is added in the analytics initializer * fix: added the console for analytics track * fix: added the await * fix: added the cache storage for the events * fix: added the cache storage in the account country selection page * fix: added the analytics in rudderstack * fix: added the utility function for the track events * fix: review comments * fix: review comments * fix: test failure in the build * fix: added trackeventwithcache in the password page
- Loading branch information
1 parent
ffe6c9f
commit d1325b6
Showing
4 changed files
with
77 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
interface AnalyticsEvent { | ||
name: string; | ||
properties: { | ||
[key: string]: string; | ||
}; | ||
} | ||
|
||
const handleCachedEvents = () => { | ||
let eventQueue: AnalyticsEvent[] = []; | ||
const storedEvents = localStorage.getItem('pending_events'); | ||
try { | ||
if (storedEvents) { | ||
eventQueue = JSON.parse(storedEvents) as AnalyticsEvent[]; | ||
if (eventQueue.length > 0) { | ||
eventQueue.forEach(event => { | ||
window.rudderanalytics.track(event.name, event.properties); | ||
}); | ||
|
||
eventQueue = []; | ||
localStorage.removeItem('pending_events'); | ||
} | ||
} | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.log(error); | ||
} | ||
}; | ||
|
||
const setEvent = (event: AnalyticsEvent): void => { | ||
const storedEvents = localStorage.getItem('pending_events'); | ||
let eventQueue: AnalyticsEvent[] = []; | ||
if (storedEvents) { | ||
eventQueue = JSON.parse(storedEvents) as AnalyticsEvent[]; | ||
} | ||
eventQueue.push(event); | ||
localStorage.setItem('pending_events', JSON.stringify(eventQueue)); | ||
}; | ||
|
||
const trackEventWithCache = (event: AnalyticsEvent): void => { | ||
if (window.rudderanalytics) { | ||
handleCachedEvents(); | ||
window.rudderanalytics.track(event.name, event.properties); | ||
} else { | ||
setEvent(event); | ||
} | ||
}; | ||
|
||
export { trackEventWithCache, handleCachedEvents }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters