Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Add Web Vitals toggle option to ScriptBuilder component

Bill Yangcf6b40892ff4
diff --git a/client/src/components/SiteSettings/ScriptBuilder.tsx b/client/src/components/SiteSettings/ScriptBuilder.tsx
index b6cf315eb..c06ffddbc 100644
--- a/client/src/components/SiteSettings/ScriptBuilder.tsx
+++ b/client/src/components/SiteSettings/ScriptBuilder.tsx
@@ -1,12 +1,11 @@
"use client";
-import { useState } from "react";
import { CodeSnippet } from "@/components/CodeSnippet";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
-import { BACKEND_URL } from "@/lib/const";
import { Textarea } from "@/components/ui/textarea";
+import { useState } from "react";
interface ScriptBuilderProps {
siteId: number;
@@ -19,6 +18,7 @@ export function ScriptBuilder({ siteId }: ScriptBuilderProps) {
const [autoTrack, setAutoTrack] = useState(true);
const [trackQuery, setTrackQuery] = useState(true);
const [trackOutbound, setTrackOutbound] = useState(true);
+ const [webVitals, setWebVitals] = useState(false);
const [skipPatterns, setSkipPatterns] = useState<string[]>([]);
const [skipPatternsText, setSkipPatternsText] = useState("");
const [maskPatterns, setMaskPatterns] = useState<string[]>([]);
@@ -98,19 +98,14 @@ export function ScriptBuilder({ siteId }: ScriptBuilderProps) {
}${
!autoTrackPageview
? `
- data-auto-track-pageview="false"`
+ data-auto-track-pageview="false"`
: ""
}${
!autoTrack
? `
- data-track-spa="false"`
- : ""
- }${
- !trackQuery
- ? `
- data-track-query="false"`
+ data-track-spa="false"`
: ""
- }${
+ }${!trackQuery ? `data-track-query="false"` : ""}${
!trackOutbound
? `
data-track-outbound="false"`
@@ -125,6 +120,11 @@ export function ScriptBuilder({ siteId }: ScriptBuilderProps) {
? `
data-mask-patterns='${JSON.stringify(maskPatterns)}'`
: ""
+ }${
+ webVitals
+ ? `
+ data-web-vitals="true"`
+ : ""
}
defer
></script>`;
@@ -234,6 +234,29 @@ export function ScriptBuilder({ siteId }: ScriptBuilderProps) {
</div>
</div> */}
+ {/* Web Vitals Option */}
+ <div className="space-y-2">
+ <div className="flex items-center justify-between">
+ <div>
+ <Label
+ htmlFor="webVitals"
+ className="text-sm font-medium text-foreground block"
+ >
+ Enable Web Vitals performance metrics
+ </Label>
+ <p className="text-xs text-muted-foreground mt-1">
+ Collect Core Web Vitals (LCP, CLS, INP) and additional metrics
+ (FCP, TTFB)
+ </p>
+ </div>
+ <Switch
+ id="webVitals"
+ checked={webVitals}
+ onCheckedChange={setWebVitals}
+ />
+ </div>
+ </div>
+
{/* Skip Patterns Option */}
<div className="space-y-2">
<div>
diff --git a/memory-bank/progress.md b/memory-bank/progress.md
index 69fef8bfa..c0b40ab7d 100644
--- a/memory-bank/progress.md
+++ b/memory-bank/progress.md
@@ -214,3 +214,22 @@ This file tracks the project's progress using a task list format.
- Reduced default script overhead by not loading Web Vitals library unless needed
- Enhanced documentation with clear usage examples and explanations
- **Impact**: Improved default performance while giving users explicit control over Web Vitals collection
+
+[2025-06-13 23:46:38] - **COMPLETED: Added Web Vitals Option to ScriptBuilder Component**
+
+- **Task**: Add Web Vitals toggle option to the ScriptBuilder component in site settings
+- **Status**: ✅ COMPLETED
+- **Work Done**:
+ - Added `webVitals` state variable (defaults to `false` to match opt-in behavior)
+ - Added `data-web-vitals="true"` attribute to generated script when enabled
+ - Created Web Vitals toggle section in the UI with proper labeling and description
+ - Positioned the option logically after the track query parameters section
+ - Used consistent styling and layout patterns matching existing options
+- **Files Modified**:
+ - [`client/src/components/SiteSettings/ScriptBuilder.tsx`](client/src/components/SiteSettings/ScriptBuilder.tsx:1) - Added Web Vitals configuration option
+- **Features Added**:
+ - Toggle switch for enabling/disabling Web Vitals collection
+ - Clear description explaining what metrics are collected (LCP, CLS, INP, FCP, TTFB)
+ - Conditional script attribute generation (`data-web-vitals="true"` only when enabled)
+ - Consistent with existing opt-in behavior (disabled by default)
+- **Impact**: Users can now easily enable Web Vitals performance metrics collection through the ScriptBuilder UI, matching the functionality documented in the script documentation