Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aswathy/fix: Incorrect Logged-In Status and Missing Residence Country in Growthbook #17966

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions packages/core/src/Utils/Analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,53 @@ export const AnalyticsInitializer = async () => {
.then(res => res.json())
.catch(() => FIREBASE_INIT_DATA);
if (process.env.RUDDERSTACK_KEY && flags?.tracking_rudderstack) {
const utm_cookie = Cookies.get('utm_data');
const ppc_campaign_cookies =
Cookies.getJSON('utm_data') === 'null'
utm_cookie === 'null' || !utm_cookie
? {
utm_source: 'no source',
utm_medium: 'no medium',
utm_campaign: 'no campaign',
utm_content: 'no content',
}
: Cookies.getJSON('utm_data');
: JSON.parse(utm_cookie);

const client_information = Cookies.getJSON('client_information');
const client_information = Cookies.get('client_information')
? JSON.parse(Cookies.get('client_information') || '')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are checking if cookie exists in line 31. So do we need || '' in line 32 again?

: null;
const residence_country = client_information?.residence || '';

const analytics_config_config = {
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,
};
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,
...analytics_config_config,
},
},
};
await Analytics?.initialise(config);
Analytics.setAttributes(analytics_config_config);
Copy link
Contributor

@amina-deriv amina-deriv Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the use of setAttributes here? we have initialized growthbook with same config in line 65
and nothing changed in attributes.

}
}
};
61 changes: 59 additions & 2 deletions packages/hooks/src/useOauth2.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { redirectToLogin } from '@deriv/shared';
import Cookies from 'js-cookie';

import FIREBASE_INIT_DATA from '@deriv/api/src/remote_config.json';
import { getAppId, redirectToLogin } from '@deriv/shared';
import { getLanguage } from '@deriv/translations';
import { Analytics } from '@deriv-com/analytics';
import {
OAuth2Logout,
requestOidcAuthentication,
TOAuth2EnabledAppList,
useIsOAuth2Enabled,
} from '@deriv-com/auth-client';
import { CountryUtils } from '@deriv-com/utils';

import useGrowthbookGetFeatureValue from './useGrowthbookGetFeatureValue';

Expand Down Expand Up @@ -39,9 +44,61 @@ const useOauth2 = ({ handleLogout }: { handleLogout: () => Promise<void> }) => {
};

const logoutHandler = async () => {
if (process.env.REMOTE_CONFIG_URL) {
const flags = await fetch(process.env.REMOTE_CONFIG_URL)
.then(res => res.json())
.catch(() => FIREBASE_INIT_DATA);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this whole initialization again?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The attributes are not getting updated without initialization.we can check if there is a way without doing the initialization again.

if (process.env.RUDDERSTACK_KEY && flags?.tracking_rudderstack) {
const utm_cookie = Cookies.get('utm_data');
const ppc_campaign_cookies =
utm_cookie === 'null' || !utm_cookie
? {
utm_source: 'no source',
utm_medium: 'no medium',
utm_campaign: 'no campaign',
utm_content: 'no content',
}
: JSON.parse(utm_cookie);

const analytics_config_config = {
loggedIn: false,
account_type: 'unlogged',
app_id: String(getAppId()),
device_language: navigator?.language || 'en-EN',
user_language: getLanguage().toLowerCase(),
country: undefined,
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: 'undefined',
};
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: {
...analytics_config_config,
},
},
};
await Analytics?.initialise(config);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Q]: why do we need to initialize the whole Analytics package again? 🤔 only updating the attributes would be enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will work only when we re-initialize it for the logout session.can you check it and let me know if it is possible in any other way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm looking into this. will update you on this

Analytics.setAttributes(analytics_config_config);

// Set attributes one final time to ensure the override took effect
}
}
// Proceed with OAuth logout
await OAuth2Logout(handleLogout);
};

return { isOAuth2Enabled, oAuthLogout: logoutHandler, loginHandler };
};

Expand Down