Skip to content

Commit

Permalink
Aswathy/fix: testing the loggedin status (#18041)
Browse files Browse the repository at this point in the history
* fix: testing the loggedin status

* chore: empty commit

* fix: upgraded the analytics version

* fix: logged out loading issue
  • Loading branch information
aswathy-deriv authored Feb 13, 2025
1 parent c31f2e8 commit 21f0a3f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 39 deletions.
92 changes: 56 additions & 36 deletions packages/core/src/Stores/client-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,26 @@ export default class ClientStore extends BaseStore {
const is_virtual = account.is_virtual;
const account_type = !is_virtual && currency ? currency : this.account_title;

setTimeout(async () => {
const analytics_config = await this.getAnalyticsConfig();
if (this.user_id) analytics_config.user_id = this.user_id;
Analytics.setAttributes(analytics_config);
}, 4);

return {
loginid,
is_disabled,
is_virtual,
icon: account_type.toLowerCase(), // TODO: display the icon
title: account_type.toLowerCase() === 'virtual' ? localize('DEMO') : account_type,
};
}

async getAnalyticsConfig(isLoggedOut = false) {
const broker = LocalStore?.get('active_loginid')
?.match(/[a-zA-Z]+/g)
?.join('');

const ppc_campaign_cookies =
Cookies.getJSON('utm_data') === 'null'
? {
Expand All @@ -1831,11 +1851,9 @@ export default class ClientStore extends BaseStore {
utm_content: 'no content',
}
: Cookies.getJSON('utm_data');
const broker = LocalStore?.get('active_loginid')
?.match(/[a-zA-Z]+/g)
?.join('');
setTimeout(async () => {
let residence_country = '';

let residence_country = '';
if (!isLoggedOut) {
if (this.residence) {
residence_country = this.residence;
} else {
Expand All @@ -1849,35 +1867,28 @@ export default class ClientStore extends BaseStore {
console.error('Error getting residence country', error);
}
}

const analytics_config = {
loggedIn: this.is_logged_in,
account_type: broker === 'null' ? 'unlogged' : broker,
residence_country,
app_id: String(getAppId()),
device_type: isMobile() ? 'mobile' : 'desktop',
language: getLanguage(),
device_language: navigator?.language || 'en-EN',
user_language: getLanguage().toLowerCase(),
country: await CountryUtils.getCountry(),
utm_source: ppc_campaign_cookies?.utm_source,
utm_medium: ppc_campaign_cookies?.utm_medium,
utm_campaign: ppc_campaign_cookies?.utm_campaign,
utm_content: ppc_campaign_cookies?.utm_content,
domain: window.location.hostname,
url: window.location.href,
};

if (this.user_id) analytics_config.user_id = this.user_id;
Analytics.setAttributes(analytics_config);
}, 4);
}
let login_status = false;
if (!isLoggedOut && this.is_logged_in) {
login_status = true;
}

return {
loginid,
is_disabled,
is_virtual,
icon: account_type.toLowerCase(), // TODO: display the icon
title: account_type.toLowerCase() === 'virtual' ? localize('DEMO') : account_type,
loggedIn: login_status,
account_type: broker === 'null' ? 'unlogged' : broker,
residence_country,
app_id: String(getAppId()),
device_type: isMobile() ? 'mobile' : 'desktop',
language: getLanguage(),
device_language: navigator?.language || 'en-EN',
user_language: getLanguage().toLowerCase(),
country: await CountryUtils.getCountry(),
utm_source: ppc_campaign_cookies?.utm_source,
utm_medium: ppc_campaign_cookies?.utm_medium,
utm_campaign: ppc_campaign_cookies?.utm_campaign,
utm_content: ppc_campaign_cookies?.utm_content,
domain: window.location.hostname,
url: window.location.href,
};
}

Expand Down Expand Up @@ -1914,7 +1925,7 @@ export default class ClientStore extends BaseStore {
}

async switchAccountHandler() {
if (!this.switched || !this.switched.length || !this.getAccount(this.switched)?.token) {
if (!this.switched || !this.switched.length) {
if (this.isUnableToFindLoginId()) {
this.handleNotFoundLoginId();
return;
Expand All @@ -1932,6 +1943,12 @@ export default class ClientStore extends BaseStore {
return;
}

const switched_account = this.getAccount(this.switched);
if (!switched_account?.token) {
this.handleNotFoundLoginId();
return;
}

runInAction(() => (this.is_switching = true));
this.setIsAuthorize(false);
const from_login_id = this.loginid;
Expand All @@ -1948,7 +1965,7 @@ export default class ClientStore extends BaseStore {
await BinarySocket?.wait('authorize');
} else {
await WS.forgetAll('balance');
await BinarySocket.authorize(this.getToken());
await BinarySocket.authorize(switched_account.token);
}
if (this.root_store.common.has_error) this.root_store.common.setError(false, null);
sessionStorage.setItem('active_tab', '1');
Expand Down Expand Up @@ -2093,7 +2110,7 @@ export default class ClientStore extends BaseStore {
this.initialized_broadcast = is_initialized;
}

cleanUp() {
async cleanUp() {
// delete all notifications keys for this account when logout
const notification_messages = LocalStore.getObject('notification_messages');
if (notification_messages && this.loginid) {
Expand All @@ -2102,6 +2119,9 @@ export default class ClientStore extends BaseStore {
...notification_messages,
});
}
// update growthbook
const analytics_config = await this.getAnalyticsConfig(true);
Analytics.setAttributes(analytics_config);

this.root_store.gtm.pushDataLayer({
event: 'log_out',
Expand Down Expand Up @@ -2135,7 +2155,7 @@ export default class ClientStore extends BaseStore {
const response = await requestLogout();

if (response?.logout === 1) {
this.cleanUp();
await this.cleanUp();

this.setLogout(true);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/Utils/Analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ export const AnalyticsInitializer = async () => {
.catch(() => FIREBASE_INIT_DATA);
if (process.env.RUDDERSTACK_KEY && flags?.tracking_rudderstack) {
const ppc_campaign_cookies =
Cookies.getJSON('utm_data') === 'null'
JSON.parse(Cookies.get('utm_data') || 'null') === 'null'
? {
utm_source: 'no source',
utm_medium: 'no medium',
utm_campaign: 'no campaign',
utm_content: 'no content',
}
: Cookies.getJSON('utm_data');
: JSON.parse(Cookies.get('utm_data') || 'null');

const client_information = Cookies.getJSON('client_information');
const client_information = JSON.parse(Cookies.get('client_information') || 'null');

const config = {
growthbookKey: flags.marketing_growthbook ? process.env.GROWTHBOOK_CLIENT_KEY : undefined,
Expand Down

0 comments on commit 21f0a3f

Please sign in to comment.