'use client' import { useState, useEffect } from 'react' import { Button, Select, Toggle, toast, Spinner } from '@ciphera-net/ui' import { useSite, useSubscription, usePageSpeedConfig } from '@/lib/swr/dashboard' import { updateSite } from '@/lib/api/sites' import { updatePageSpeedConfig } from '@/lib/api/pagespeed' import { getRetentionOptionsForPlan, formatRetentionMonths } from '@/lib/plans' import { generatePrivacySnippet } from '@/lib/utils/privacySnippet' import { Copy, CheckCircle } from '@phosphor-icons/react' import Link from 'next/link' const GEO_OPTIONS = [ { value: 'full', label: 'Full (country, region, city)' }, { value: 'country', label: 'Country only' }, { value: 'none', label: 'Disabled' }, ] export default function SitePrivacyTab({ siteId }: { siteId: string }) { const { data: site, mutate } = useSite(siteId) const { data: subscription, error: subscriptionError, mutate: mutateSubscription } = useSubscription() const { data: psiConfig, mutate: mutatePSIConfig } = usePageSpeedConfig(siteId) const [collectPagePaths, setCollectPagePaths] = useState(true) const [collectReferrers, setCollectReferrers] = useState(true) const [collectDeviceInfo, setCollectDeviceInfo] = useState(true) const [collectScreenRes, setCollectScreenRes] = useState(true) const [collectGeoData, setCollectGeoData] = useState('full') const [hideUnknownLocations, setHideUnknownLocations] = useState(false) const [dataRetention, setDataRetention] = useState(6) const [excludedPaths, setExcludedPaths] = useState('') const [snippetCopied, setSnippetCopied] = useState(false) const [saving, setSaving] = useState(false) useEffect(() => { if (site) { setCollectPagePaths(site.collect_page_paths ?? true) setCollectReferrers(site.collect_referrers ?? true) setCollectDeviceInfo(site.collect_device_info ?? true) setCollectScreenRes(site.collect_screen_resolution ?? true) setCollectGeoData(site.collect_geo_data ?? 'full') setHideUnknownLocations(site.hide_unknown_locations ?? false) setDataRetention(site.data_retention_months ?? 6) setExcludedPaths((site.excluded_paths || []).join('\n')) } }, [site]) const handleSave = async () => { setSaving(true) try { await updateSite(siteId, { name: site?.name || '', collect_page_paths: collectPagePaths, collect_referrers: collectReferrers, collect_device_info: collectDeviceInfo, collect_screen_resolution: collectScreenRes, collect_geo_data: collectGeoData as 'full' | 'country' | 'none', hide_unknown_locations: hideUnknownLocations, data_retention_months: dataRetention, excluded_paths: excludedPaths.split('\n').map(p => p.trim()).filter(Boolean), }) await mutate() toast.success('Privacy settings updated') } catch { toast.error('Failed to save') } finally { setSaving(false) } } if (!site) return
Control what data is collected from your visitors.
{item.label}
{item.desc}
Controls location granularity. "Disabled" collects no geographic data at all.
Plan limits could not be loaded.
Keep raw event data for
Events older than this are automatically deleted. Aggregated daily stats are kept permanently.
Your {subscription.plan_id?.includes('pro') ? 'Pro' : 'Free'} plan supports up to {formatRetentionMonths(Math.max(...getRetentionOptionsForPlan(subscription.plan_id).map(o => o.value)))} of data retention.
)} {(!subscription || subscription.plan_id?.includes('free')) && (Upgrade for longer retention.
)}Check frequency
How often PageSpeed Insights runs automated checks.
Copy the text below into your Privacy Policy. It updates automatically based on your saved settings.
This is provided for convenience and is not legal advice. Consult a lawyer for compliance requirements.