From a8a26a52998e71d5bf287f91700d21a52fadee45 Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Sat, 29 Jul 2023 17:44:03 +0800 Subject: [PATCH] refactor(console): report reddit conversion (#4263) * refactor(console): report reddit conversion * refactor(console): update pixel id --- packages/console/jest.setup.ts | 11 +++++ .../src/components/ReportConversion/index.tsx | 28 ++++++++++++ .../components/ReportConversion/utils.test.ts | 24 ++++++++++ .../src/components/ReportConversion/utils.ts | 45 +++++++++++++++++-- packages/console/src/include.d/tags.d.ts | 4 ++ 5 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 packages/console/src/components/ReportConversion/utils.test.ts diff --git a/packages/console/jest.setup.ts b/packages/console/jest.setup.ts index a14c86087..233334651 100644 --- a/packages/console/jest.setup.ts +++ b/packages/console/jest.setup.ts @@ -1,3 +1,6 @@ +import { webcrypto } from 'node:crypto'; +import { TextEncoder, TextDecoder } from 'node:util'; + import i18next from 'i18next'; import { initReactI18next } from 'react-i18next'; @@ -7,3 +10,11 @@ void i18next.use(initReactI18next).init({ lng: 'en', react: { useSuspense: false }, }); + +/* eslint-disable @silverhand/fp/no-mutation */ +// @ts-expect-error monkey-patch for `crypto` +crypto.subtle = webcrypto.subtle; +global.TextEncoder = TextEncoder; +// @ts-expect-error monkey-patch for `TextEncoder`/`TextDecoder` +global.TextDecoder = TextDecoder; +/* eslint-enable @silverhand/fp/no-mutation */ diff --git a/packages/console/src/components/ReportConversion/index.tsx b/packages/console/src/components/ReportConversion/index.tsx index 2ed523127..6fc8a209d 100644 --- a/packages/console/src/components/ReportConversion/index.tsx +++ b/packages/console/src/components/ReportConversion/index.tsx @@ -1,6 +1,8 @@ import { useEffect } from 'react'; import { Helmet } from 'react-helmet'; +import useCurrentUser from '@/hooks/use-current-user'; + import { shouldReport, lintrk, @@ -8,6 +10,9 @@ import { linkedInConversionId, gtag, gtagSignUpConversionId, + rdt, + redditPixelId, + hashEmail, } from './utils'; /** @@ -15,6 +20,28 @@ import { * Insight Tag, then reports a sign-up conversion to them. */ export default function ReportConversion() { + const { user, isLoading } = useCurrentUser(); + + /** + * Initiate Reddit Pixel and report a sign-up conversion to it when user is loaded. + * Use user email to prevent duplicate conversion, and it is hashed before sending + * to protect user privacy. + */ + useEffect(() => { + const report = async () => { + rdt('init', redditPixelId, { + optOut: false, + useDecimalCurrencyValues: true, + email: await hashEmail(user?.primaryEmail ?? undefined), + }); + rdt('track', 'SignUp'); + }; + + if (shouldReport && !isLoading) { + void report(); + } + }, [user, isLoading]); + /** * This `useEffect()` initiates Google Tag and report a sign-up conversion to it. * It may run multiple times (e.g. a user visit multiple times to finish the onboarding process, @@ -49,6 +76,7 @@ export default function ReportConversion() { src={`https://www.googletagmanager.com/gtag/js?id=${gtagAwTrackingId}`} />