diff --git a/.github/workflows/build-and-deploy-staging.yml b/.github/workflows/build-and-deploy-staging.yml index bd60fb8..84b76c1 100644 --- a/.github/workflows/build-and-deploy-staging.yml +++ b/.github/workflows/build-and-deploy-staging.yml @@ -44,3 +44,5 @@ jobs: project_name: "traders-hub" branch_name: staging output_dir: dist + env: + RUDDERSTACK_KEY: ${{ vars.VITE_RUDDERSTACK_KEY }} diff --git a/.github/workflows/build-and-deploy-test.yml b/.github/workflows/build-and-deploy-test.yml index 4c3761c..514b865 100644 --- a/.github/workflows/build-and-deploy-test.yml +++ b/.github/workflows/build-and-deploy-test.yml @@ -63,6 +63,8 @@ jobs: project_name: "traders-hub" branch_name: "pr-${{github.event.number}}" output_dir: dist + env: + RUDDERSTACK_KEY: ${{ vars.VITE_RUDDERSTACK_KEY }} - name: "Generate preview link comment" uses: "deriv-com/shared-actions/.github/actions/post_preview_link_comment@v1" diff --git a/package-lock.json b/package-lock.json index 76857b2..31c8a8d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1343,6 +1343,12 @@ "node": ">=v14" } }, + "node_modules/@commitlint/load/node_modules/@types/node": { + "version": "20.5.1", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.5.1.tgz", + "integrity": "sha512-4tT2UrL5LBqDwoed9wZ6N3umC4Yhz3W3FloMmiiG4JwmUJWpie0c7lcnUNd4gtMKuDEO4wRVS8B6Xa0uMRsMKg==", + "dev": true + }, "node_modules/@commitlint/load/node_modules/ansi-styles": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", @@ -4018,10 +4024,13 @@ "dev": true }, "node_modules/@types/node": { - "version": "20.5.1", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.5.1.tgz", - "integrity": "sha512-4tT2UrL5LBqDwoed9wZ6N3umC4Yhz3W3FloMmiiG4JwmUJWpie0c7lcnUNd4gtMKuDEO4wRVS8B6Xa0uMRsMKg==", - "dev": true + "version": "20.12.7", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.7.tgz", + "integrity": "sha512-wq0cICSkRLVaf3UGLMGItu/PtdY7oaXaI/RVU+xliKVOtRna3PRY57ZDfztpDL0n11vfymMUnXv8QwYCO7L1wg==", + "dev": true, + "dependencies": { + "undici-types": "~5.26.4" + } }, "node_modules/@types/normalize-package-data": { "version": "2.4.4", @@ -16833,6 +16842,12 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "dev": true + }, "node_modules/unicode-canonical-property-names-ecmascript": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-2.0.0.tgz", diff --git a/src/main.tsx b/src/main.tsx index 88a2bf3..1c39be4 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -1,33 +1,72 @@ -import React from 'react'; +import React, { useEffect } from 'react'; import ReactDOM from 'react-dom/client'; -import { AppDataProvider } from '@deriv-com/api-hooks'; +// eslint-disable-next-line import/no-extraneous-dependencies +import { Analytics } from '@deriv-com/analytics'; +import { AppDataProvider, useWebsiteStatus } from '@deriv-com/api-hooks'; +import { useDevice } from '@deriv-com/ui'; +import { WebSocketUtils } from '@deriv-com/utils'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import { ReactQueryDevtools } from '@tanstack/react-query-devtools'; import { CFDProvider, RealAccountCreationProvider, UIProvider } from '@/providers'; import { startInitPerformanceTimers } from '@/utils'; -import { Header } from './components/Header/Header.tsx'; -import App from './App.tsx'; +import { Header } from './components/Header/Header'; +import { useActiveDerivTradingAccount } from './hooks/useActiveDerivTradingAccount'; +import App from './App'; import './index.css'; const queryClient = new QueryClient(); -// function to start the timer for login/signup/redirect +const AnalyticsConfigurator = () => { + const { data: activeTradingAccount } = useActiveDerivTradingAccount(); + const { data: websiteStatusData } = useWebsiteStatus(); + const { isDesktop, isMobile, isTablet } = useDevice(); + const { getAppId } = WebSocketUtils; + + useEffect(() => { + if (websiteStatusData?.clients_country) { + const accountType = activeTradingAccount?.is_virtual ? 'demo' : 'real'; + const clientCountry = websiteStatusData.clients_country; + + if (import.meta.env.VITE_RUDDERSTACK_KEY) { + const config = { + rudderstackKey: import.meta.env.VITE_RUDDERSTACK_KEY, + }; + Analytics.initialise(config); + const attributes = { + account_type: accountType, + app_id: getAppId(), + device_type: + (isDesktop && 'Desktop') || (isMobile && 'Mobile') || (isTablet && 'Tablet') || 'Mobile', + device_language: navigator.language || 'en-EN', + user_language: 'en-EN', + country: clientCountry, + }; + Analytics.setAttributes(attributes); + } + } + }, [activeTradingAccount, websiteStatusData]); + + return null; +}; + +const container = document.getElementById('root'); +const root = container ? ReactDOM.createRoot(container) : null; startInitPerformanceTimers(); -ReactDOM.createRoot(document.getElementById('root')!).render( +root?.render( - {/* Temporary Header */}
+ diff --git a/tsconfig.json b/tsconfig.json index dd955d1..e03164e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -20,7 +20,7 @@ "noUnusedLocals": true, "noUnusedParameters": true, "noFallthroughCasesInSwitch": true, - "types": ["@testing-library/jest-dom"], + "types": ["node", "@testing-library/jest-dom"], "paths": { "@/*": ["./src/*"], "@cfd/*": ["./src/cfd/*"]