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 #17933

Open
wants to merge 4 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
113 changes: 68 additions & 45 deletions packages/core/src/Utils/Analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,77 @@ 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);
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);
}
}, 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');

monitorCookieChanges(flags); // Monitor and reinitialize on changes
await initializeGrowthBook(flags, initialClientInfo); // Initial setup
console.log('previousClientInfo', previousClientInfo);
}
};
13 changes: 13 additions & 0 deletions packages/hooks/src/useOauth2.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -39,6 +42,16 @@ const useOauth2 = ({ handleLogout }: { handleLogout: () => Promise<void> }) => {
};

const logoutHandler = async () => {
Cookies.remove('client_information');
const clientInformation = Cookies.get('client_information');
// Pass the value to GrowthBook if it exists

const analytics_config = {
loggedIn: !!clientInformation,
};

Analytics.setAttributes(analytics_config);

await OAuth2Logout(handleLogout);
};

Expand Down
Loading