Skip to content

Commit

Permalink
Merge pull request #217 from shayan-deriv/shayan/fix-deriv-analytics-…
Browse files Browse the repository at this point in the history
…error

fix: fixed cookie issue
  • Loading branch information
ali-hosseini-deriv authored Jul 24, 2024
2 parents bef3f3a + e582321 commit cdbf347
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 52 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-and-deploy-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ jobs:
VITE_GROWTHBOOK_DECRYPTION_KEY: ${{ secrets.VITE_GROWTHBOOK_DECRYPTION_KEY }}
VITE_GROWTHBOOK_CLIENT_KEY: ${{ vars.VITE_GROWTHBOOK_CLIENT_KEY }}
VITE_RUDDERSTACK_KEY: ${{ vars.VITE_RUDDERSTACK_KEY }}
VITE_REMOTE_CONFIG_URL: ${{ vars.VITE_REMOTE_CONFIG_URL }}

- name: Run tests for Eslint
run: npm run test:lint
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build-and-deploy-staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ jobs:
VITE_GROWTHBOOK_DECRYPTION_KEY: ${{ secrets.VITE_GROWTHBOOK_DECRYPTION_KEY }}
VITE_GROWTHBOOK_CLIENT_KEY: ${{ vars.VITE_GROWTHBOOK_CLIENT_KEY }}
VITE_RUDDERSTACK_KEY: ${{ vars.VITE_RUDDERSTACK_KEY }}
VITE_REMOTE_CONFIG_URL: ${{ vars.VITE_REMOTE_CONFIG_URL }}


- name: Run tests for Eslintbuild_to_cloudflare_pages
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"dependencies": {
"@babel/preset-env": "^7.24.5",
"@chakra-ui/react": "^2.8.2",
"@deriv-com/analytics": "^1.9.0",
"@deriv-com/analytics": "^1.10.1",
"@deriv-com/api-hooks": "^1.3.7",
"@deriv-com/translations": "^1.2.4",
"@deriv-com/ui": "^1.29.0",
Expand Down
18 changes: 12 additions & 6 deletions src/hooks/custom-hooks/__tests__/useDerivAnalytics.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,25 @@ jest.mock('@deriv-com/analytics', () => ({
}));

jest.mock('js-cookie', () => ({
getJSON: jest.fn().mockReturnValue({
utm_campaign: 'campaign',
utm_content: 'content',
utm_medium: 'medium',
utm_source: 'source',
}),
get: jest.fn().mockReturnValue(
JSON.stringify({
utm_campaign: 'campaign',
utm_content: 'content',
utm_medium: 'medium',
utm_source: 'source',
})
),
}));

const mockedUseDevice = useDevice as jest.MockedFunction<typeof useDevice>;
const mockUseActiveAccount = api.account.useActiveAccount as jest.MockedFunction<typeof api.account.useActiveAccount>;

describe('useDerivAnalytics', () => {
beforeEach(() => {
Object.defineProperty(window, 'location', {
value: { hostname: 'production' },
writable: true,
});
process.env.VITE_GROWTHBOOK_DECRYPTION_KEY = 'test_decryption_key';
process.env.VITE_GROWTHBOOK_CLIENT_KEY = 'test_client_key';
process.env.VITE_RUDDERSTACK_KEY = 'test_rudderstack_key';
Expand Down
76 changes: 35 additions & 41 deletions src/hooks/custom-hooks/useDerivAnalytics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import Cookies from 'js-cookie';
import { FIREBASE_INIT_DATA } from '@/constants';
import { Analytics } from '@deriv-com/analytics';
Expand All @@ -16,56 +15,51 @@ const useDerivAnalytics = () => {
const { data: websiteStatus } = useWebsiteStatus();
const { isMobile } = useDevice();
const currentLang = localStorage.getItem(LocalStorageConstants.i18nLanguage) || 'EN';

const initialise = async () => {
try {
const isDerivAnalyticsInitialized = Analytics?.getInstances()?.tracking?.has_initialized;
const isLocalHost = location.hostname === 'localhost';

if (!isDerivAnalyticsInitialized) {
if (!isLocalHost && !isDerivAnalyticsInitialized) {
const remoteConfigURL = process.env.VITE_REMOTE_CONFIG_URL;
let services = FIREBASE_INIT_DATA;
if (remoteConfigURL) {
const services = await fetch(remoteConfigURL)
.then(res => res.json())
.catch(() => FIREBASE_INIT_DATA);
services = await fetch(remoteConfigURL).then(res => res.json().catch(() => FIREBASE_INIT_DATA));
}

const ppcCampaignCookies =
// @ts-expect-error
Cookies.getJSON('utm_data') === 'null'
? {
utm_campaign: 'no campaign',
utm_content: 'no content',
utm_medium: 'no medium',
utm_source: 'no source',
}
: // @ts-expect-error
Cookies.getJSON('utm_data');
const utmDataFromCookie = Cookies.get('utm_data');
const ppcCampaignCookies = utmDataFromCookie
? JSON.parse(utmDataFromCookie)
: {
utm_campaign: 'no campaign',
utm_content: 'no content',
utm_medium: 'no medium',
utm_source: 'no source',
};

Analytics.initialise({
growthbookDecryptionKey: services?.marketing_growthbook
? process.env.VITE_GROWTHBOOK_DECRYPTION_KEY
: undefined,
growthbookKey: services?.marketing_growthbook
? process.env.VITE_GROWTHBOOK_CLIENT_KEY
: undefined,
rudderstackKey: services?.tracking_rudderstack ? process.env.VITE_RUDDERSTACK_KEY || '' : '',
});
Analytics.initialise({
growthbookDecryptionKey: services?.marketing_growthbook
? process.env.VITE_GROWTHBOOK_DECRYPTION_KEY
: undefined,
growthbookKey: services?.marketing_growthbook ? process.env.VITE_GROWTHBOOK_CLIENT_KEY : undefined,
rudderstackKey: services?.tracking_rudderstack ? process.env.VITE_RUDDERSTACK_KEY || '' : '',
});

await Analytics?.getInstances()?.ab?.GrowthBook?.init();
await Analytics?.getInstances()?.ab?.GrowthBook?.init();

Analytics.setAttributes({
account_type: activeAccount?.account_type || 'unlogged',
app_id: String(WebSocketUtils.getAppId()),
country: websiteStatus?.clients_country,
device_language: navigator?.language || 'en-EN',
device_type: isMobile ? 'mobile' : 'desktop',
domain: window.location.hostname,
user_language: currentLang.toLowerCase(),
utm_campaign: ppcCampaignCookies?.utm_campaign,
utm_content: ppcCampaignCookies?.utm_content,
utm_medium: ppcCampaignCookies?.utm_medium,
utm_source: ppcCampaignCookies?.utm_source,
});
}
Analytics.setAttributes({
account_type: activeAccount?.account_type || 'unlogged',
app_id: String(WebSocketUtils.getAppId()),
country: websiteStatus?.clients_country,
device_language: navigator?.language || 'en-EN',
device_type: isMobile ? 'mobile' : 'desktop',
domain: window.location.hostname,
user_language: currentLang.toLowerCase(),
utm_campaign: ppcCampaignCookies?.utm_campaign,
utm_content: ppcCampaignCookies?.utm_content,
utm_medium: ppcCampaignCookies?.utm_medium,
utm_source: ppcCampaignCookies?.utm_source,
});
}
} catch (error) {
// eslint-disable-next-line no-console
Expand Down

0 comments on commit cdbf347

Please sign in to comment.