Skip to content

Commit 167da73

Browse files
committed
fix: enabled logic, add scheduling logic
1 parent 9d7fd49 commit 167da73

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

app/api/ab-config/route.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ type MatomoExperiment = {
44
idexperiment: string
55
name: string
66
status: string
7+
start_date?: string
8+
end_date?: string
79
variations: Array<{
810
name: string
911
percentage: number
@@ -19,6 +21,30 @@ type ABTestConfig = {
1921
}>
2022
}
2123

24+
function isExperimentActive(experiment: MatomoExperiment): boolean {
25+
const now = new Date()
26+
27+
// Check start date - if scheduled for future, not active yet
28+
if (experiment.start_date) {
29+
const startDate = new Date(experiment.start_date)
30+
if (now < startDate) {
31+
return false // Not started yet
32+
}
33+
}
34+
35+
// Check end date - if past end date, not active anymore
36+
if (experiment.end_date) {
37+
const endDate = new Date(experiment.end_date)
38+
if (now > endDate) {
39+
return false // Already ended
40+
}
41+
}
42+
43+
// If no scheduling constraints, enabled if created or running
44+
// If within time window, enabled if running
45+
return ["created", "running"].includes(experiment.status)
46+
}
47+
2248
export async function GET() {
2349
try {
2450
const matomoUrl = process.env.NEXT_PUBLIC_MATOMO_URL
@@ -91,12 +117,8 @@ export async function GET() {
91117
const config: Record<string, ABTestConfig> = {}
92118

93119
experiments.forEach((exp) => {
94-
// Include running experiments (Matomo uses "running" not "active")
95-
if (
96-
exp.status === "running" &&
97-
exp.variations &&
98-
exp.variations.length > 0
99-
) {
120+
// Include all experiments with variations (let scheduling handle timing)
121+
if (exp.variations && exp.variations.length > 0) {
100122
// Calculate Original variant weight (100% - sum of all variations)
101123
const variationsTotalWeight = exp.variations.reduce(
102124
(sum, variation) => {
@@ -119,7 +141,7 @@ export async function GET() {
119141

120142
config[exp.name] = {
121143
id: exp.idexperiment,
122-
enabled: true,
144+
enabled: isExperimentActive(exp),
123145
variants: variants,
124146
}
125147
}

0 commit comments

Comments
 (0)