From a9c162c8517a53f7036d83d4493378f35dcd9ed6 Mon Sep 17 00:00:00 2001 From: hagaitski Date: Wed, 10 Apr 2024 05:01:33 +0900 Subject: [PATCH] =?UTF-8?q?=E7=B4=B0=E3=81=8B=E3=81=84=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/abtesting.ts | 4 ++-- src/pages/_app.tsx | 26 +++++++++++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/common/abtesting.ts b/src/common/abtesting.ts index 0999b70a..589f6f84 100644 --- a/src/common/abtesting.ts +++ b/src/common/abtesting.ts @@ -15,8 +15,8 @@ export const validateABTestingVariant = ( : null; }; -export const getAorBRandom = (): AB_TETSTING_VARIANT => { - return Math.random() < 0.5 +export const getAorBRandom = (rate: number): AB_TETSTING_VARIANT => { + return Math.random() < rate ? AB_TESTING_VARIANTS.ONLY_SONNET : AB_TESTING_VARIANTS.WITH_HAIKU; }; diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx index af7be636..43ca5864 100644 --- a/src/pages/_app.tsx +++ b/src/pages/_app.tsx @@ -16,7 +16,11 @@ import { gtagEvent } from "@/common/util/gtag"; import { keysOverride } from "@/components/headMeta"; import { ConfirmModal } from "@/components/confirmModal"; import { Toast } from "@/components/toast"; -import { AB_TESTING_COOKIE_NAME, getAorBRandom } from "@/common/abtesting"; +import { + AB_TESTING_COOKIE_NAME, + getAorBRandom, + validateABTestingVariant, +} from "@/common/abtesting"; export default function App({ Component, pageProps }: AppProps) { const queryClient = useMemo(() => new QueryClient(), []); @@ -46,13 +50,21 @@ export default function App({ Component, pageProps }: AppProps) { }, [router]); useEffect(() => { + if (!process.env.NEXT_PUBLIC_AB_TEST_RATE) { + return; + } + const rate = Number(process.env.NEXT_PUBLIC_AB_TEST_RATE); + if (rate < 0 || rate > 1 || isNaN(rate)) { + console.error("NEXT_PUBLIC_AB_TEST_RATE must be between 0 and 1."); + return; + } //ABテストのためのクッキーを付与 - process.env.NEXT_PUBLIC_ENABLE_AB_TEST && - import("js-cookie").then((jsCookie) => { - if (!jsCookie.default.get(AB_TESTING_COOKIE_NAME)) { - jsCookie.default.set(AB_TESTING_COOKIE_NAME, getAorBRandom()); - } - }); + import("js-cookie").then((jsCookie) => { + const cookieValue = jsCookie.default.get(AB_TESTING_COOKIE_NAME); + if (!cookieValue || !validateABTestingVariant(cookieValue)) { + jsCookie.default.set(AB_TESTING_COOKIE_NAME, getAorBRandom(rate)); + } + }); }, []); return (