Skip to content

Commit

Permalink
細かい修正
Browse files Browse the repository at this point in the history
  • Loading branch information
eatski committed Apr 9, 2024
1 parent 4a02d22 commit a9c162c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/common/abtesting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
26 changes: 19 additions & 7 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(), []);
Expand Down Expand Up @@ -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 (
Expand Down

0 comments on commit a9c162c

Please sign in to comment.