Skip to content

Commit

Permalink
CSIT-1558/Prince/Livechat icon on testlinks (deriv-com#17251)
Browse files Browse the repository at this point in the history
* fix: livechat icon on testlinks

* fix: country data for growthbook
  • Loading branch information
prince-deriv authored Oct 19, 2024
1 parent c1480ce commit 326c82a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ const LiveChat = observer(({ showPopover }: { showPopover?: boolean }) => {

const chat = enable_freshworks_live_chat ? freshChat : null;

if ((enable_freshworks_live_chat && !chat?.isReady) || !is_livechat_available) return null;
const isFreshchatEnabledButNotReady = enable_freshworks_live_chat && !chat?.isReady;
const isNeitherChatNorLiveChatAvailable = !is_livechat_available && !enable_freshworks_live_chat;

if (isFreshchatEnabledButNotReady || isNeitherChatNorLiveChatAvailable) {
return null;
}

// Quick fix for making sure livechat won't popup if feature flag is late to enable.
// We will add a refactor after this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const LoginButton = ({ className }) => (
has_effect
text={localize('Log in')}
onClick={() => {
window.LiveChatWidget.call('hide');
window.LiveChatWidget?.call('hide');
redirectToLogin(false, getLanguage());
}}
tertiary
Expand Down
20 changes: 18 additions & 2 deletions packages/core/src/Utils/Analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ export const AnalyticsInitializer = async () => {
utm_content: 'no content',
}
: Cookies.getJSON('utm_data');

/*
Temp solution to fetch country and handle experiments without causing inconsistencies to the users.
*/

const getCountry = async () => {
try {
const response = await fetch('https://www.cloudflare.com/cdn-cgi/trace').catch(() => null);
const text = response ? await response.text().catch(() => '') : '';
const entries = text ? text.split('\n').map(v => v.split('=', 2)) : [];
const data = entries.length ? Object.fromEntries(entries) : {};
return data?.loc?.toLowerCase() ?? '';
} catch {
return '';
}
};

const config = {
growthbookKey: flags.marketing_growthbook ? process.env.GROWTHBOOK_CLIENT_KEY : undefined,
growthbookDecryptionKey: flags.marketing_growthbook ? process.env.GROWTHBOOK_DECRYPTION_KEY : undefined,
Expand All @@ -34,8 +51,7 @@ export const AnalyticsInitializer = async () => {
device_type: window.innerWidth <= MAX_MOBILE_WIDTH ? 'mobile' : 'desktop',
device_language: navigator?.language || 'en-EN',
user_language: getLanguage().toLowerCase(),
country:
Cookies.getJSON('clients_country') || Cookies?.getJSON('website_status')?.clients_country,
country: await getCountry(),
utm_source: ppc_campaign_cookies?.utm_source,
utm_medium: ppc_campaign_cookies?.utm_medium,
utm_campaign: ppc_campaign_cookies?.utm_campaign,
Expand Down
5 changes: 5 additions & 0 deletions packages/hooks/src/useGrowthbookGetFeatureValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const useGrowthbookGetFeatureValue = <T extends string | boolean>({
const isGBLoaded = useIsGrowthbookIsLoaded();
const isMounted = useIsMounted();

// Required for debugging Growthbook, this will be removed after Freshchat launch
if (typeof window !== 'undefined') {
window.Analytics = Analytics;
}

useEffect(() => {
if (isGBLoaded) {
if (Analytics?.getInstances()?.ab) {
Expand Down
1 change: 1 addition & 0 deletions types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ declare global {
FreshChat: {
initialize: (config: FreshChatConfig) => void;
};
Analytics: any;
}
interface FreshChatConfig {
token: string | null;
Expand Down

0 comments on commit 326c82a

Please sign in to comment.