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 (