Skip to content

Commit

Permalink
fix: review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
aswathy-deriv committed Aug 13, 2024
1 parent 885a637 commit 9abc9a9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import QuestionnaireModal from '../QuestionnaireModal';
import ResidenceForm from '../SetResidenceModal/set-residence-form.jsx';
import validateSignupFields from './validate-signup-fields.jsx';
import 'Sass/app/modules/account-signup.scss';
import { trackEvent } from 'Utils/Analytics/analytics.ts';
import { trackEventWithCache } from 'Utils/Analytics/analytics.ts';

const AccountSignup = ({
enableApp,
Expand Down Expand Up @@ -56,14 +56,14 @@ const AccountSignup = ({

// didMount lifecycle hook
React.useEffect(() => {
trackEvent({
trackEventWithCache({
name: 'ce_virtual_signup_form',
properties: {
action: 'signup_confirmed',
form_name: is_mobile ? 'virtual_signup_web_mobile_default' : 'virtual_signup_web_desktop_default',
},
});
trackEvent({
trackEventWithCache({
name: 'ce_virtual_signup_form',
properties: {
action: 'country_selection_screen_opened',
Expand Down
30 changes: 13 additions & 17 deletions packages/core/src/Utils/Analytics/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
interface AnalyticsEvent {
name: string;
properties: {
[key: string]: any;
[key: string]: string;
};
}

Expand All @@ -12,24 +12,24 @@ const setEvent = (event: AnalyticsEvent): void => {
localStorage.setItem('pending_events', JSON.stringify(eventQueue));
};

const trackEvent = (event: AnalyticsEvent): void => {
const trackEventWithCache = (event: AnalyticsEvent): void => {
if (window.rudderanalytics) {
window.rudderanalytics.track(event.name, event.properties);
} else {
setEvent(event);
handleCachedEvents();
}
};
const handleCachedEvents = () => {
const loadPendingEvents = () => {
const storedEvents = localStorage.getItem('pending_events');

const loadPendingEvents = (): void => {
const storedEvents = localStorage.getItem('pending_events');

if (storedEvents) {
eventQueue = JSON.parse(storedEvents) as AnalyticsEvent[];
}
};

const initializeAnalytics = async (): Promise<void> => {
if (storedEvents) {
eventQueue = JSON.parse(storedEvents) as AnalyticsEvent[];
}
};
try {
loadPendingEvents();
if (eventQueue.length > 0) {
eventQueue.forEach(event => {
window.rudderanalytics.track(event.name, event.properties);
Expand All @@ -39,12 +39,8 @@ const initializeAnalytics = async (): Promise<void> => {
localStorage.removeItem('pending_events');
}
} catch (error) {
console.log('Error initializing analytics:', error);
console.log(error);
}
};

// Initialize the module
loadPendingEvents();
initializeAnalytics();

export { trackEvent };
export { trackEventWithCache, handleCachedEvents };

0 comments on commit 9abc9a9

Please sign in to comment.