Skip to content

Commit d6b58ef

Browse files
committed
Working AB test for landing url
1 parent 636ac5d commit d6b58ef

File tree

5 files changed

+52
-21
lines changed

5 files changed

+52
-21
lines changed

astro.config.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export default defineConfig({
2525
sitemap({
2626
filter: (page) =>
2727
page !== 'https://www.throughputfocus.com/contact_problem' &&
28-
page !== 'https://www.throughputfocus.com/index-test' &&
28+
page !== 'https://www.throughputfocus.com/test-a' &&
29+
page !== 'https://www.throughputfocus.com/test-b' &&
2930
page !== 'https://www.throughputfocus.com/elements' &&
3031
page !== 'https://www.throughputfocus.com/contact_success',
3132
}),

public/_worker.js

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,41 @@
55
export default {
66
async fetch(request, env) {
77
const url = new URL(request.url);
8-
const cookieName = "ab-homepage-test-cookie";
9-
const newHomepagePathName = "/index-test";
10-
let cookie = request.headers.get("cookie");
11-
if (url.pathname === ('/api/fred')) {
12-
// TODO: Add custom logic here.
13-
url.pathname = newHomepagePathName;
8+
const cookieName = "test-ab-cookie";
9+
const PathNameA = "/test-a";
10+
const PathNameB = "/test-b";
1411

15-
const percentage = Math.floor(Math.random() * 100);
16-
let version = "current"; // default version
17-
// change pathname and version name for 50% of traffic
18-
if (percentage < 50) {
19-
url.pathname = newHomepagePathName;
20-
version = "new";
12+
if (url.pathname === ('/landing')) {
13+
14+
let cookie = request.headers.get("cookie");
15+
if (cookie && cookie.includes(`${cookieName}=A`)) {
16+
url.pathname = PathNameA
17+
return env.ASSETS.fetch(url)
18+
}
19+
else if (cookie && cookie.includes(`${cookieName}=B`)) {
20+
url.pathname = PathNameB
21+
return env.ASSETS.fetch(url)
2122
}
22-
// get the static file from ASSETS, and attach a cookie
23-
const asset = await env.ASSETS.fetch(url);
24-
let response = new Response(asset.body, asset);
25-
let expires = (new Date(Date.now()+ 86400*1000)).toUTCString(); // Valid for 1 Day = 24 Hrs = 24*60*60 = 86400.
26-
response.headers.append("Set-Cookie", `${cookieName}=${version}; expires= ${expires} ;path=/`);
27-
return response;
23+
else {
24+
url.pathname = PathNameB;
25+
26+
const percentage = Math.floor(Math.random() * 100);
27+
let version = "A"; // default version
2828

29+
// change pathname and version name for 50% of traffic
30+
if (percentage < 50) {
31+
url.pathname = PathNameB;
32+
version = "B";
33+
}
34+
// get the static file from ASSETS, and attach a cookie
35+
const asset = await env.ASSETS.fetch(url);
36+
let response = new Response(asset.body, asset);
37+
let expires = (new Date(Date.now()+ 86400*1000)).toUTCString(); // Valid for 1 Day = 24 Hrs = 24*60*60 = 86400.
38+
response.headers.append("Set-Cookie", `${cookieName}=${version}; expires= ${expires} ;path=/`);
39+
return response;
40+
}
2941
}
30-
// Otherwise, serve the static assets.
42+
// Otherwise, serve the static assets for the current page being requested.
3143
// Without this, the Worker will error and no assets will be served.
3244
return env.ASSETS.fetch(request);
3345
},

src/content/pages/privacy-policy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Privacy"
3-
description: "this is meta description"
3+
description: "Privacy policies"
44
draft: false
55
---
66

src/content/pages/test-a.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: "Landing Page A"
3+
description: "This is meta description for the Landing Page A"
4+
draft: false
5+
---
6+
7+
## Introduction
8+
9+
Landing page A

src/content/pages/test-b.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: "Landing Page B"
3+
description: "This is meta description for the Landing Page B"
4+
draft: false
5+
---
6+
7+
## Introduction
8+
9+
Landing page B

0 commit comments

Comments
 (0)