From a8b05c26395b4dd1e3b7098bff477cd88eeccd3c Mon Sep 17 00:00:00 2001 From: aswathy Date: Thu, 2 Jan 2025 19:04:49 +0400 Subject: [PATCH 1/4] fix: loggedin attribute issue in logout button --- packages/core/src/Utils/Analytics/index.ts | 108 ++++++++++++--------- packages/hooks/src/useOauth2.ts | 16 +++ 2 files changed, 79 insertions(+), 45 deletions(-) diff --git a/packages/core/src/Utils/Analytics/index.ts b/packages/core/src/Utils/Analytics/index.ts index 411e15a12d1a..d7af3fce6e88 100644 --- a/packages/core/src/Utils/Analytics/index.ts +++ b/packages/core/src/Utils/Analytics/index.ts @@ -8,54 +8,72 @@ import { CountryUtils } from '@deriv-com/utils'; import { MAX_MOBILE_WIDTH } from '../../Constants'; -export const AnalyticsInitializer = async () => { +const getGrowthBookAttributes = async client_information => { const account_type = LocalStore?.get('active_loginid') ?.match(/[a-zA-Z]+/g) ?.join(''); - if (process.env.REMOTE_CONFIG_URL) { - const flags = await fetch(process.env.REMOTE_CONFIG_URL) - .then(res => res.json()) - .catch(() => FIREBASE_INIT_DATA); - if (process.env.RUDDERSTACK_KEY && flags?.tracking_rudderstack) { - const ppc_campaign_cookies = - Cookies.getJSON('utm_data') === 'null' - ? { - utm_source: 'no source', - utm_medium: 'no medium', - utm_campaign: 'no campaign', - utm_content: 'no content', - } - : Cookies.getJSON('utm_data'); - - const client_information = Cookies.getJSON('client_information'); - - const config = { - growthbookKey: flags.marketing_growthbook ? process.env.GROWTHBOOK_CLIENT_KEY : undefined, - growthbookDecryptionKey: flags.marketing_growthbook ? process.env.GROWTHBOOK_DECRYPTION_KEY : undefined, - rudderstackKey: process.env.RUDDERSTACK_KEY, - growthbookOptions: { - attributes: { - loggedIn: !!client_information, - account_type: account_type === 'null' ? 'unlogged' : account_type, - app_id: String(getAppId()), - device_type: window.innerWidth <= MAX_MOBILE_WIDTH ? 'mobile' : 'desktop', - 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, - network_type: navigator.connection?.effectiveType, - network_rtt: navigator.connection?.rtt, - network_downlink: navigator.connection?.downlink, - residence_country: client_information?.residence, - }, - }, - }; - await Analytics?.initialise(config); + const ppc_campaign_cookies = + Cookies.getJSON('utm_data') === 'null' + ? { + utm_source: 'no source', + utm_medium: 'no medium', + utm_campaign: 'no campaign', + utm_content: 'no content', + } + : Cookies.getJSON('utm_data'); + + return { + loggedIn: !!client_information, + account_type: account_type || 'unlogged', + app_id: String(getAppId()), + device_type: window.innerWidth <= MAX_MOBILE_WIDTH ? 'mobile' : 'desktop', + 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, + network_type: navigator.connection?.effectiveType, + network_rtt: navigator.connection?.rtt, + network_downlink: navigator.connection?.downlink, + residence_country: client_information?.residence || null, + }; +}; + +const initializeGrowthBook = async (flags, client_information) => { + const attributes = await getGrowthBookAttributes(client_information); + const config = { + growthbookKey: flags.marketing_growthbook ? process.env.GROWTHBOOK_CLIENT_KEY : undefined, + growthbookDecryptionKey: flags.marketing_growthbook ? process.env.GROWTHBOOK_DECRYPTION_KEY : undefined, + rudderstackKey: process.env.RUDDERSTACK_KEY, + growthbookOptions: { attributes }, + }; + + await Analytics?.initialise(config); +}; + +const monitorCookieChanges = flags => { + let previousClientInfo = Cookies.getJSON('client_information'); + setInterval(async () => { + const currentClientInfo = Cookies.getJSON('client_information'); + if (JSON.stringify(previousClientInfo) !== JSON.stringify(currentClientInfo)) { + previousClientInfo = currentClientInfo; + await initializeGrowthBook(flags, currentClientInfo); } + }, 1000); // Check every second for changes +}; + +export const AnalyticsInitializer = async () => { + const flags = await fetch(process.env.REMOTE_CONFIG_URL) + .then(res => res.json()) + .catch(() => FIREBASE_INIT_DATA); + + if (process.env.RUDDERSTACK_KEY && flags?.tracking_rudderstack) { + const initialClientInfo = Cookies.getJSON('client_information'); + await initializeGrowthBook(flags, initialClientInfo); // Initial setup + monitorCookieChanges(flags); // Monitor and reinitialize on changes } }; diff --git a/packages/hooks/src/useOauth2.ts b/packages/hooks/src/useOauth2.ts index 762c209cf461..d55b0cbc86f5 100644 --- a/packages/hooks/src/useOauth2.ts +++ b/packages/hooks/src/useOauth2.ts @@ -1,5 +1,8 @@ +import Cookies from 'js-cookie'; + import { redirectToLogin } from '@deriv/shared'; import { getLanguage } from '@deriv/translations'; +import { Analytics } from '@deriv-com/analytics'; import { OAuth2Logout, requestOidcAuthentication, @@ -39,6 +42,19 @@ const useOauth2 = ({ handleLogout }: { handleLogout: () => Promise }) => { }; const logoutHandler = async () => { + const clientInformation = Cookies.get('client_information'); + + // Remove the 'client_information' cookie + Cookies.remove('client_information'); + + // Pass the value to GrowthBook if it exists + if (clientInformation) { + const analytics_config = { + loggedIn: !!clientInformation, + }; + + Analytics.setAttributes(analytics_config); + } await OAuth2Logout(handleLogout); }; From 4cf52149062344e1ec40d68f0af2f61da0e15f93 Mon Sep 17 00:00:00 2001 From: aswathy Date: Fri, 3 Jan 2025 10:08:53 +0400 Subject: [PATCH 2/4] fix: added the condition --- packages/core/src/Utils/Analytics/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/src/Utils/Analytics/index.ts b/packages/core/src/Utils/Analytics/index.ts index d7af3fce6e88..5bfc77f6a5bc 100644 --- a/packages/core/src/Utils/Analytics/index.ts +++ b/packages/core/src/Utils/Analytics/index.ts @@ -56,9 +56,9 @@ const initializeGrowthBook = async (flags, client_information) => { }; const monitorCookieChanges = flags => { - let previousClientInfo = Cookies.getJSON('client_information'); + let previousClientInfo = !!Cookies.getJSON('client_information'); setInterval(async () => { - const currentClientInfo = Cookies.getJSON('client_information'); + const currentClientInfo = !!Cookies.getJSON('client_information'); if (JSON.stringify(previousClientInfo) !== JSON.stringify(currentClientInfo)) { previousClientInfo = currentClientInfo; await initializeGrowthBook(flags, currentClientInfo); From 3e3a75e90a82095d1bf24bf14a6a4444712445d7 Mon Sep 17 00:00:00 2001 From: aswathy Date: Fri, 3 Jan 2025 12:29:49 +0400 Subject: [PATCH 3/4] fix: added the console items to check the issue --- packages/core/src/Utils/Analytics/index.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/core/src/Utils/Analytics/index.ts b/packages/core/src/Utils/Analytics/index.ts index 5bfc77f6a5bc..7bc09b35f601 100644 --- a/packages/core/src/Utils/Analytics/index.ts +++ b/packages/core/src/Utils/Analytics/index.ts @@ -53,12 +53,15 @@ const initializeGrowthBook = async (flags, client_information) => { }; await Analytics?.initialise(config); + console.log('config', config); }; const monitorCookieChanges = flags => { let previousClientInfo = !!Cookies.getJSON('client_information'); + console.log('previousClientInfo', previousClientInfo); setInterval(async () => { const currentClientInfo = !!Cookies.getJSON('client_information'); + console.log('currentClientInfo', currentClientInfo); if (JSON.stringify(previousClientInfo) !== JSON.stringify(currentClientInfo)) { previousClientInfo = currentClientInfo; await initializeGrowthBook(flags, currentClientInfo); @@ -72,8 +75,10 @@ export const AnalyticsInitializer = async () => { .catch(() => FIREBASE_INIT_DATA); if (process.env.RUDDERSTACK_KEY && flags?.tracking_rudderstack) { - const initialClientInfo = Cookies.getJSON('client_information'); - await initializeGrowthBook(flags, initialClientInfo); // Initial setup + const initialClientInfo = !!Cookies.getJSON('client_information'); + monitorCookieChanges(flags); // Monitor and reinitialize on changes + await initializeGrowthBook(flags, initialClientInfo); // Initial setup + console.log('previousClientInfo', previousClientInfo); } }; From e6f04b6b99ce0322289813fa69b0e8663ca55576 Mon Sep 17 00:00:00 2001 From: aswathy Date: Fri, 3 Jan 2025 15:43:16 +0400 Subject: [PATCH 4/4] fix: loggedin issues in testlink --- packages/hooks/src/useOauth2.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/hooks/src/useOauth2.ts b/packages/hooks/src/useOauth2.ts index d55b0cbc86f5..230885bc4bd4 100644 --- a/packages/hooks/src/useOauth2.ts +++ b/packages/hooks/src/useOauth2.ts @@ -42,19 +42,16 @@ const useOauth2 = ({ handleLogout }: { handleLogout: () => Promise }) => { }; const logoutHandler = async () => { + Cookies.remove('client_information'); const clientInformation = Cookies.get('client_information'); + // Pass the value to GrowthBook if it exists - // Remove the 'client_information' cookie - Cookies.remove('client_information'); + const analytics_config = { + loggedIn: !!clientInformation, + }; - // Pass the value to GrowthBook if it exists - if (clientInformation) { - const analytics_config = { - loggedIn: !!clientInformation, - }; + Analytics.setAttributes(analytics_config); - Analytics.setAttributes(analytics_config); - } await OAuth2Logout(handleLogout); };