From 4b1820ef5cd3f2215d1f1db31aebfc59161d5d08 Mon Sep 17 00:00:00 2001 From: Inokentii Mazhara Date: Wed, 28 Aug 2024 14:24:22 +0300 Subject: [PATCH] TW-1506 Reimplement conversion tracking without using cookies --- src/app/hooks/use-conversion-tracking.ts | 50 ++++++++++++++---------- src/app/pages/Home/Home.tsx | 13 ++++++ 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/app/hooks/use-conversion-tracking.ts b/src/app/hooks/use-conversion-tracking.ts index 897e5cf241..e3d65e6eb4 100644 --- a/src/app/hooks/use-conversion-tracking.ts +++ b/src/app/hooks/use-conversion-tracking.ts @@ -1,20 +1,9 @@ import { useEffect } from 'react'; -import axios from 'axios'; - import { dispatch } from 'app/store'; import { setConversionTrackedAction } from 'app/store/settings/actions'; import { useIsConversionTrackedSelector } from 'app/store/settings/selectors'; import { AnalyticsEventCategory, useAnalytics } from 'lib/analytics'; -import { EnvVars } from 'lib/env'; - -function fetchConversionInformation() { - return axios - .get<{ linkId: string; name: string }>(EnvVars.CONVERSION_VERIFICATION_URL, { - withCredentials: true - }) - .then(response => response.data); -} export const useConversionTracking = () => { const { trackEvent } = useAnalytics(); @@ -22,15 +11,34 @@ export const useConversionTracking = () => { const isConversionTracked = useIsConversionTrackedSelector(); useEffect(() => { - if (!isConversionTracked) { - fetchConversionInformation().then(({ linkId, name }) => { - trackEvent('Conversion Info', AnalyticsEventCategory.General, { - conversionId: linkId, - conversionName: name - }); - }); - - dispatch(setConversionTrackedAction()); + if (isConversionTracked) { + return undefined; } - }, [trackEvent]); + + const conversionIframeListener = (event: MessageEvent) => { + try { + const data = typeof event.data === 'string' ? JSON.parse(event.data) : event.data; + + if (data.type !== 'trackLink') { + return; + } + + dispatch(setConversionTrackedAction()); + window.removeEventListener('message', conversionIframeListener); + + if (data.link) { + const { linkId, name } = data.link; + + trackEvent('Conversion Info', AnalyticsEventCategory.General, { + conversionId: linkId, + conversionName: name + }); + } + } catch {} + }; + + window.addEventListener('message', conversionIframeListener); + + return () => window.removeEventListener('message', conversionIframeListener); + }, [isConversionTracked, trackEvent]); }; diff --git a/src/app/pages/Home/Home.tsx b/src/app/pages/Home/Home.tsx index 5f0ef7616e..757dd560bd 100644 --- a/src/app/pages/Home/Home.tsx +++ b/src/app/pages/Home/Home.tsx @@ -5,8 +5,10 @@ import { isDefined } from '@rnw-community/shared'; import { useAppEnv } from 'app/env'; import PageLayout from 'app/layouts/PageLayout'; import { useMainnetTokensScamlistSelector } from 'app/store/assets/selectors'; +import { useIsConversionTrackedSelector } from 'app/store/settings/selectors'; import { setAnotherSelector, setTestID } from 'lib/analytics'; import { TEZ_TOKEN_SLUG } from 'lib/assets'; +import { EnvVars } from 'lib/env'; import { useAssetMetadata, getAssetSymbol } from 'lib/metadata'; import { useAccount } from 'lib/temple/front'; import { HistoryAction, navigate, useLocation } from 'lib/woozie'; @@ -37,6 +39,8 @@ const Home = memo(({ assetSlug }) => { const assetMetadata = useAssetMetadata(assetSlug || TEZ_TOKEN_SLUG); const assetSymbol = getAssetSymbol(assetMetadata); + const isConversionTracked = useIsConversionTrackedSelector(); + useLayoutEffect(() => { const usp = new URLSearchParams(search); if (assetSlug && usp.get('after_token_added') === 'true') { @@ -63,6 +67,15 @@ const Home = memo(({ assetSlug }) => { attention={true} adShow > + {!isConversionTracked && ( +