-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabtest.ts
35 lines (28 loc) · 1.11 KB
/
abtest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import type { Context, Config } from "@netlify/edge-functions";
export default async (request: Request, context: Context) => {
// look for existing "test_bucket" cookie
const bucketName = "test_bucket";
const bucket = context.cookies.get(bucketName);
// return here if we find a cookie
if (bucket) {
return new Response(`Welcome back! You were assigned ${bucketName} **${bucket}** when you last visited the site!`);
}
// if no "test_bucket" cookie is found, assign the user to a bucket
// in this example we're using two buckets (a, b) with an equal weighting of 50/50
const weighting = 0.5;
// get a random number between (0-1)
// this is a basic example and you may want to experiment
const random = Math.random();
const newBucketValue = random <= weighting ? "a" : "b";
// set the new "test_bucket" cookie
context.cookies.set({
name: bucketName,
value: newBucketValue,
});
return new Response(
`Congratulations! You have been assigned ${bucketName} **${newBucketValue}**. View your browser cookies to check it out!`,
);
};
export const config: Config = {
path: "/abtest",
};