import{r as m,j as e,P as D,C as g,s as V,B as L,t as _,c as C}from"./index-BBjAHUAg.js";const F=[{value:7,label:"7 Tage"},{value:28,label:"28 Tage"},{value:90,label:"90 Tage"},{value:365,label:"12 Monate"}],I=[{value:"users",label:"Besucher"},{value:"sessions",label:"Sitzungen"},{value:"pageviews",label:"Seitenaufrufe"}],Z={Direct:"Direkt (Adresse/Lesezeichen)","Organic Search":"Google & Co. (unbezahlt)","Paid Search":"Suchanzeigen",Referral:"Verweise von anderen Seiten","Organic Social":"Social Media","Paid Social":"Social-Media-Anzeigen",Email:"E-Mail","Organic Maps":"Google Maps","Organic Video":"Video (YouTube & Co.)",Display:"Bannerwerbung",Unassigned:"Nicht zugeordnet"},R={desktop:"Computer",mobile:"Handy",tablet:"Tablet","smart tv":"Smart-TV"},z={generate_lead:"Anfrage gesendet",select_package:"Paket angeklickt",contact_call:"Telefonnummer angetippt",contact_whatsapp:"WhatsApp geöffnet",scroll:"Bis zum Seitenende gescrollt",click:"Link zu anderer Website",file_download:"Datei heruntergeladen",form_start:"Formular angefangen"},H=new Intl.NumberFormat("de-DE"),b=s=>H.format(Math.round(s)),U=s=>`${new Intl.NumberFormat("de-DE",{maximumFractionDigits:1}).format(s*100)} %`,X=s=>{const n=Math.round(s);return`${Math.floor(n/60)}:${String(n%60).padStart(2,"0")} min`},B=(s,n=!1)=>`${s.slice(6,8)}.${s.slice(4,6)}.${n?s.slice(0,4):""}`;function K(s,n){if(!n)return s>0?{label:"neu",tone:"up"}:null;const i=(s-n)/n*100;return Math.abs(i)<.5?{label:"±0 %",tone:"flat"}:{label:`${i>0?"+":"−"}${new Intl.NumberFormat("de-DE",{maximumFractionDigits:Math.abs(i)<10?1:0}).format(Math.abs(i))} %`,tone:i>0?"up":"down"}}const J={up:"bg-brand-100 text-brand-800 dark:bg-brand-900/50 dark:text-brand-300",down:"bg-red-100 text-red-700 dark:bg-red-900/40 dark:text-red-300",flat:"bg-neutral-100 text-neutral-600 dark:bg-neutral-800 dark:text-neutral-300"};function G({current:s,previous:n,className:i=""}){const o=K(s,n);return o?e.jsxs("span",{className:`inline-flex items-center rounded-full px-1.5 py-0.5 text-[11px] font-semibold tabular-nums ${J[o.tone]} ${i}`,children:[o.tone==="up"?"▲ ":o.tone==="down"?"▼ ":"",o.label]}):null}function y({label:s,hint:n,value:i,previous:o,format:t}){return e.jsxs("div",{className:"rounded-xl border border-neutral-200/80 bg-white p-4 shadow-sm shadow-neutral-900/[0.03] dark:border-neutral-800 dark:bg-neutral-900 dark:shadow-none",children:[e.jsx("p",{className:"text-xs font-medium text-neutral-500 dark:text-neutral-400",title:n,children:s}),e.jsx("p",{className:"mt-1 text-2xl font-bold tabular-nums tracking-tight text-neutral-900 dark:text-white",children:t(i)}),e.jsxs("div",{className:"mt-1.5 flex flex-wrap items-center gap-1.5",children:[e.jsx(G,{current:i,previous:o}),e.jsxs("span",{className:"text-[11px] text-neutral-400",children:["vorher ",t(o)]})]})]})}function Q({current:s,previous:n,metric:i,days:o}){const[t,p]=m.useState(null),h=m.useRef(null),[r,l]=m.useState(720);m.useEffect(()=>{const a=h.current;if(!a)return;const x=new ResizeObserver(([S])=>l(Math.max(260,Math.round(S.contentRect.width))));return x.observe(a),()=>x.disconnect()},[]);const u=r<500?200:240,d={top:12,right:8,bottom:26,left:36},M=r-d.left-d.right,v=u-d.top-d.bottom,c=Math.max(1,...s.map(a=>a[i]),...n.map(a=>a[i])),w=(()=>{const a=10**Math.floor(Math.log10(c));return Math.ceil(c/a)*a})(),f=s.length,j=a=>d.left+(f<=1?M/2:a/(f-1)*M),$=a=>d.top+v-a/w*v,E=a=>a.map((x,S)=>`${S?"L":"M"}${j(S).toFixed(1)},${$(x[i]).toFixed(1)}`).join(""),W=`${E(s)}L${j(f-1).toFixed(1)},${d.top+v}L${j(0).toFixed(1)},${d.top+v}Z`,P=[0,.5,1].map(a=>Math.round(w*a)),A=Math.max(1,Math.ceil(f/Math.max(3,Math.floor(r/100)))),T=a=>{const x=a.currentTarget.getBoundingClientRect(),S=(a.clientX-x.left)/x.width*r,O=Math.round((S-d.left)/M*(f-1));p(Math.min(f-1,Math.max(0,O)))},k=t!==null?{cur:s[t],prev:n[t]}:null;return e.jsxs("div",{ref:h,className:"relative",children:[e.jsxs("svg",{width:r,height:u,viewBox:`0 0 ${r} ${u}`,className:"block max-w-full touch-none select-none",onMouseMove:T,onTouchMove:a=>T(a.touches[0]?{...a,clientX:a.touches[0].clientX,currentTarget:a.currentTarget}:a),onMouseLeave:()=>p(null),role:"img","aria-label":"Verlauf im gewählten Zeitraum im Vergleich zum vorherigen",children:[e.jsx("defs",{children:e.jsxs("linearGradient",{id:"traffic-area",x1:"0",x2:"0",y1:"0",y2:"1",children:[e.jsx("stop",{offset:"0%",stopColor:"#6ba626",stopOpacity:"0.28"}),e.jsx("stop",{offset:"100%",stopColor:"#6ba626",stopOpacity:"0"})]})}),P.map(a=>e.jsxs("g",{children:[e.jsx("line",{x1:d.left,x2:r-d.right,y1:$(a),y2:$(a),className:"stroke-neutral-200 dark:stroke-neutral-800",strokeWidth:"1"}),e.jsx("text",{x:d.left-6,y:$(a)+4,textAnchor:"end",className:"fill-neutral-400 text-[11px]",children:b(a)})]},a)),s.map((a,x)=>x%A===0&&f-1-x>=A/2||x===f-1?e.jsx("text",{x:j(x),y:u-6,textAnchor:x===0?"start":x===f-1?"end":"middle",className:"fill-neutral-400 text-[11px]",children:B(a.date)},a.date):null),e.jsx("path",{d:E(n),fill:"none",className:"stroke-neutral-400 dark:stroke-neutral-500",strokeWidth:"1.5",strokeDasharray:"4 4"}),e.jsx("path",{d:W,fill:"url(#traffic-area)"}),e.jsx("path",{d:E(s),fill:"none",stroke:"#6ba626",strokeWidth:"2.25",strokeLinejoin:"round",strokeLinecap:"round"}),k&&e.jsxs("g",{children:[e.jsx("line",{x1:j(t),x2:j(t),y1:d.top,y2:d.top+v,className:"stroke-neutral-300 dark:stroke-neutral-700"}),e.jsx("circle",{cx:j(t),cy:$(k.cur[i]),r:"4",fill:"#6ba626",className:"stroke-white dark:stroke-neutral-900",strokeWidth:"2"})]})]}),k&&e.jsxs("div",{className:"pointer-events-none absolute top-0 z-10 rounded-lg border border-neutral-200 bg-white px-3 py-2 text-xs shadow-lg dark:border-neutral-700 dark:bg-neutral-900",style:{left:`${j(t)/r*100}%`,transform:`translateX(${t>f/2?"-105%":"5%"})`},children:[e.jsxs("p",{className:"font-semibold text-neutral-900 dark:text-white",children:[B(k.cur.date,!0),": ",b(k.cur[i])]}),k.prev&&e.jsxs("p",{className:"text-neutral-500 dark:text-neutral-400",children:[o," Tage davor (",B(k.prev.date),"): ",b(k.prev[i])]})]}),e.jsxs("div",{className:"mt-2 flex flex-wrap gap-4 text-xs text-neutral-500 dark:text-neutral-400",children:[e.jsxs("span",{className:"flex items-center gap-1.5",children:[e.jsx("span",{className:"h-0.5 w-4 rounded bg-brand-500"})," Gewählter Zeitraum"]}),e.jsxs("span",{className:"flex items-center gap-1.5",children:[e.jsx("span",{className:"w-4 border-t-2 border-dashed border-neutral-400"})," Zeitraum davor"]})]})]})}function N({rows:s,label:n=p=>p,mono:i=!1,showTrend:o=!0,empty:t="Noch keine Daten."}){if(!(s!=null&&s.length))return e.jsx("p",{className:"text-sm text-neutral-400",children:t});const p=s.reduce((r,l)=>r+l.current,0)||1,h=Math.max(...s.map(r=>r.current),1);return e.jsx("ul",{className:"space-y-2.5",children:s.map(r=>e.jsxs("li",{children:[e.jsxs("div",{className:"mb-1 flex items-baseline justify-between gap-3 text-sm",children:[e.jsx("span",{className:`min-w-0 truncate text-neutral-700 dark:text-neutral-200 ${i?"font-mono text-xs":""}`,title:r.name,children:n(r.name)||"(nicht gesetzt)"}),e.jsxs("span",{className:"flex shrink-0 items-center gap-2",children:[o&&e.jsx(G,{current:r.current,previous:r.previous}),e.jsx("span",{className:"w-12 text-right font-semibold tabular-nums text-neutral-900 dark:text-white",children:b(r.current)})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"h-1.5 flex-1 overflow-hidden rounded-full bg-neutral-100 dark:bg-neutral-800",children:e.jsx("div",{className:"h-full rounded-full bg-brand-500/80",style:{width:`${r.current/h*100}%`}})}),e.jsxs("span",{className:"w-10 text-right text-[11px] tabular-nums text-neutral-400",children:[Math.round(r.current/p*100)," %"]})]})]},r.name))})}const Y=s=>(s||"").replace(/\s*\|\s*HifiPlanet\s*$/i,"").trim()||"Startseite";function q({initial:s}){var r;const[n,i]=m.useState(s);if(m.useEffect(()=>i(s),[s]),m.useEffect(()=>{const l=setInterval(()=>{document.visibilityState==="visible"&&C.get("/analytics/realtime").then(u=>u.realtime&&i(u.realtime)).catch(()=>{})},6e4);return()=>clearInterval(l)},[]),!n)return null;const o=n.per_minute||[],t=Math.max(1,...o),p=Math.max(1,...n.pages.map(l=>l.users)),h=n.users>0;return e.jsx(g,{className:"mb-6",padded:!1,children:e.jsxs("div",{className:"grid lg:grid-cols-[minmax(0,5fr)_minmax(0,7fr)]",children:[e.jsxs("div",{className:"border-b border-neutral-100 p-5 sm:p-6 lg:border-b-0 lg:border-r dark:border-neutral-800",children:[e.jsxs("div",{className:"flex items-center gap-2 text-xs font-semibold uppercase tracking-wide text-neutral-500 dark:text-neutral-400",children:[e.jsxs("span",{className:"relative flex h-2.5 w-2.5",children:[h&&e.jsx("span",{className:"absolute inline-flex h-full w-full animate-ping rounded-full bg-brand-400 opacity-75"}),e.jsx("span",{className:`relative inline-flex h-2.5 w-2.5 rounded-full ${h?"bg-brand-500":"bg-neutral-300 dark:bg-neutral-600"}`})]}),"Live · letzte 30 Minuten"]}),e.jsx("p",{className:"mt-3 text-4xl font-bold tabular-nums tracking-tight text-neutral-900 dark:text-white",children:b(n.users)}),e.jsx("p",{className:"text-sm text-neutral-500 dark:text-neutral-400",children:"Besucher auf der Website"}),o.length>0&&e.jsxs("div",{className:"mt-5",children:[e.jsx("div",{className:"flex h-16 items-end gap-[3px]","aria-label":"Besucher je Minute",children:o.map((l,u)=>e.jsx("div",{className:`flex-1 rounded-sm ${l>0?"bg-brand-500/80":"bg-neutral-100 dark:bg-neutral-800"}`,style:{height:l>0?`${Math.max(12,l/t*100)}%`:"4px"},title:`${u===29?"Gerade eben":`Vor ${29-u} Min.`}: ${l}`},u))}),e.jsxs("div",{className:"mt-1 flex justify-between text-[11px] text-neutral-400",children:[e.jsx("span",{children:"vor 30 Min."}),e.jsx("span",{children:"jetzt"})]})]}),((r=n.devices)==null?void 0:r.length)>0&&e.jsx("div",{className:"mt-4 flex flex-wrap gap-1.5",children:n.devices.map(l=>e.jsxs("span",{className:"rounded-full bg-neutral-100 px-2.5 py-1 text-xs font-medium text-neutral-700 dark:bg-neutral-800 dark:text-neutral-300",children:[R[l.name]||l.name,": ",l.users]},l.name))})]}),e.jsxs("div",{className:"p-5 sm:p-6",children:[e.jsx("p",{className:"mb-3 text-sm font-semibold text-neutral-900 dark:text-white",children:"Gerade angesehene Seiten"}),n.pages.length===0?e.jsx("p",{className:"text-sm text-neutral-400",children:"Im Moment ist niemand auf der Website."}):e.jsx("ul",{className:"space-y-2",children:n.pages.map(l=>e.jsxs("li",{className:"relative overflow-hidden rounded-lg bg-neutral-50 dark:bg-neutral-800/50",children:[e.jsx("div",{className:"absolute inset-y-0 left-0 bg-brand-500/15 dark:bg-brand-500/20",style:{width:`${l.users/p*100}%`}}),e.jsxs("div",{className:"relative flex items-center justify-between gap-3 px-3 py-2 text-sm",children:[e.jsx("span",{className:"min-w-0 truncate text-neutral-800 dark:text-neutral-100",title:l.name,children:Y(l.name)}),e.jsxs("span",{className:"shrink-0 font-semibold tabular-nums text-neutral-900 dark:text-white",children:[l.users," ",e.jsx("span",{className:"font-normal text-neutral-400",children:l.users===1?"Person":"Personen"})]})]})]},l.name))}),n.fetched_at&&e.jsxs("p",{className:"mt-3 text-[11px] text-neutral-400",children:["Stand ",new Date(n.fetched_at).toLocaleTimeString("de-DE",{hour:"2-digit",minute:"2-digit"})," Uhr · aktualisiert sich jede Minute"]})]})]})})}function te(){const[s,n]=m.useState(28),[i,o]=m.useState("users"),[t,p]=m.useState(null),[h,r]=m.useState(""),[l,u]=m.useState(!1),d=(c=!1)=>{u(!0),r(""),C.get(`/analytics/traffic?days=${s}${c?"&refresh=1":""}`).then(p).catch(w=>r(w.message)).finally(()=>u(!1))};m.useEffect(()=>{d()},[s]);const M=m.useMemo(()=>((t==null?void 0:t.events)||[]).filter(c=>z[c.name]).sort((c,w)=>Object.keys(z).indexOf(c.name)-Object.keys(z).indexOf(w.name)),[t]),v=e.jsx(D,{title:"Besucher",description:"Zahlen aus Google Analytics. Jede Zahl zeigt den Vergleich zum gleich langen Zeitraum davor.",actions:e.jsxs(e.Fragment,{children:[e.jsx(_,{value:s,onChange:n,options:F,size:"sm"}),e.jsx(L,{variant:"secondary",size:"sm",icon:"refresh",onClick:()=>d(!0),disabled:l,children:"Aktualisieren"})]})});return t&&!t.configured?e.jsxs("div",{children:[e.jsx(D,{title:"Besucher"}),e.jsx(g,{children:e.jsx(V,{icon:"gauge",title:"Google Analytics ist noch nicht verbunden",description:"Trage unter System › Google-Dienste die Property-ID und den Service-Account ein, dann erscheinen hier die Besucherzahlen.",actions:e.jsx(L,{to:"/admin/settings/google",children:"Zu den Google-Diensten"})})})]}):e.jsxs("div",{children:[v,h&&e.jsx(g,{className:"mb-6",children:e.jsx("p",{className:"text-sm text-red-600",children:h})}),!t&&!h&&e.jsx("p",{className:"text-sm text-neutral-400",children:"Lädt…"}),t&&e.jsxs("div",{className:l?"opacity-60 transition-opacity":"transition-opacity",children:[e.jsx(q,{initial:t.realtime}),e.jsxs("div",{className:"mb-6 grid grid-cols-2 gap-3 sm:gap-4 lg:grid-cols-3 xl:grid-cols-6",children:[e.jsx(y,{label:"Besucher",hint:"Aktive Nutzer im Zeitraum",value:t.kpis.activeUsers.current,previous:t.kpis.activeUsers.previous,format:b}),e.jsx(y,{label:"Neue Besucher",hint:"Zum ersten Mal auf der Seite",value:t.kpis.newUsers.current,previous:t.kpis.newUsers.previous,format:b}),e.jsx(y,{label:"Sitzungen",hint:"Besuche (ein Besucher kann mehrmals kommen)",value:t.kpis.sessions.current,previous:t.kpis.sessions.previous,format:b}),e.jsx(y,{label:"Seitenaufrufe",value:t.kpis.screenPageViews.current,previous:t.kpis.screenPageViews.previous,format:b}),e.jsx(y,{label:"Ø Besuchsdauer",value:t.kpis.averageSessionDuration.current,previous:t.kpis.averageSessionDuration.previous,format:X}),e.jsx(y,{label:"Interessierte Besuche",hint:"Anteil der Sitzungen über 10 Sekunden, mit 2+ Seiten oder einer Aktion (GA: Engagement-Rate)",value:t.kpis.engagementRate.current,previous:t.kpis.engagementRate.previous,format:U})]}),e.jsx(g,{title:"Verlauf",actions:e.jsx(_,{value:i,onChange:o,options:I,size:"sm"}),className:"mb-6",children:e.jsx(Q,{current:t.daily.current,previous:t.daily.previous,metric:i,days:t.days})}),e.jsxs("div",{className:"grid gap-6 lg:grid-cols-2",children:[e.jsx(g,{title:"Woher die Besucher kommen",description:"Sitzungen nach Kanal",children:e.jsx(N,{rows:t.channels,label:c=>Z[c]||c})}),e.jsx(g,{title:"Quellen",description:"Sitzungen nach Website bzw. Suchmaschine",children:e.jsx(N,{rows:t.sources,label:c=>c==="(direct)"?"direkt":c})}),e.jsx(g,{title:"Meistbesuchte Seiten",description:"Seitenaufrufe",children:e.jsx(N,{rows:t.pages,mono:!0})}),e.jsx(g,{title:"Einstiegsseiten",description:"Auf welcher Seite ein Besuch beginnt (Sitzungen)",children:e.jsx(N,{rows:t.landing_pages,mono:!0,showTrend:!1})}),e.jsx(g,{title:"Was Besucher tun",description:"Anzahl im Zeitraum – die wichtigsten Aktionen auf der Website",children:e.jsx(N,{rows:M,label:c=>z[c],empty:"Noch keine Aktionen erfasst."})}),e.jsxs("div",{className:"grid gap-6",children:[e.jsx(g,{title:"Geräte",description:"Besucher",children:e.jsx(N,{rows:t.devices,label:c=>R[c]||c})}),e.jsx(g,{title:"Orte",description:"Besucher nach Stadt (ungefähr, aus der IP-Adresse)",children:e.jsx(N,{rows:t.cities,showTrend:!1,label:c=>c==="(not set)"?"unbekannt":c})})]})]}),e.jsxs("p",{className:"mt-6 text-xs text-neutral-400",children:["Gezählt werden nur Besucher, die im Cookie-Banner der Statistik zugestimmt haben. Stand"," ",new Date(t.generated_at).toLocaleTimeString("de-DE",{hour:"2-digit",minute:"2-digit"})," Uhr, wird bis zu 5 Minuten zwischengespeichert. Noch mehr Auswertungen gibt es direkt in"," ",e.jsx("a",{href:"https://analytics.google.com/",target:"_blank",rel:"noreferrer",className:"text-brand-600 hover:underline dark:text-brand-400",children:"Google Analytics"}),"."]})]})]})}export{te as default};