1import{a as R,u as G,j as e}from"./query-vendor-_CTnhe7O.js";import{u as Y}from"./useSessionState-ByqGZN7v.js";import{ak as $,aU as j,aC as N,x as ye,y as ve,z as Ne,A as be,E as d,aL as H,aM as Q,a as X,i as J,aN as Z,B as k,h as we,s as ee}from"./index-CSN0_rIN.js";import{C as m,b as u,a as h,d as f}from"./card-qTQsd6Lx.js";import{C as te}from"./calendar-1w1-27TB.js";import{T as re,a as se,b as C,c as l,d as ae,e as c}from"./table-7Snb3dA-.js";import{e as p}from"./endOfDay-DQBzK-rR.js";import{ag as ne,a4 as Se,bt as oe,aQ as ke,bu as Ce,c as De,bv as Te,aA as _e}from"./icons-vendor-DvzEQjW4.js";import{f as D,a as ie,s as Me}from"./format-D9Xo-GGE.js";import{R as T,B as z,C as E,X as P,Y as B,T as _,b as K,P as Fe,e as Oe,d as Le}from"./charts-vendor-k6ViREfG.js";import{s as q}from"./subDays-DoKhsGIU.js";import{s as Ae}from"./subYears-C3SL0459.js";import{s as Re}from"./subMonths-BgxAtiZO.js";import{e as Ye}from"./endOfMonth-DIRV3rPc.js";import{s as le}from"./startOfMonth-B9P38VKF.js";import"./react-vendor-DGf8d9XT.js";import"./radix-vendor-BcAgND1y.js";
1import"./supabase-vendor-CZpx0hCK.js";import"./motion-vendor-CnxG56Tq.js";import"./carousel-vendor-BedNBA4P.js";import"./differenceInCalendarMonths-5t8xcOOg.js";import"./en-US-3LvP7pCn.js";import"./constructFrom-DWjd9ymD.js";function $e(n,a){const s=+$(n),[o,x]=[+$(a.start),+$(a.end)].sort((M,y)=>M-y);return s>=o&&s<=x}const ce=["hsl(var(--primary))","hsl(var(--chart-2))","hsl(var(--chart-3))","hsl(var(--chart-4))","hsl(var(--chart-5))","hsl(var(--muted-foreground))"],ze=["completed","processing","on-hold","shipped","delivered"];function Ee(n){const a=(n.traffic_source||"").trim().toLowerCase();if(a&&a!=="direct"&&a!=="unknown")return a;const s=(n.referrer||"").trim().toLowerCase();if(!s)return"direct";if(/(^|\.)google\./.test(s))return"google / organic";if(/(^|\.)bing\./.test(s))return"bing / organic";if(/(^|\.)duckduckgo\./.test(s))return"duckduckgo / organic";if(/(^|\.)yahoo\./.test(s))return"yahoo / organic";if(/(^|\.)yandex\./.test(s))return"yandex / organic";if(/chatgpt\.com|openai\.com/.test(s))return"chatgpt / referral";if(/perplexity\.ai/.test(s))return"perplexity / referral";if(/(^|\/\/)(mail\.|com\.google\.android\.gm|outlook\.)/.test(s))return"email / referral";if(/android-app:\/\/com\.google\.android\.gm/.test(s))return"gmail app / referral";if(/(^|\.)facebook\.|(^|\.)fb\./.test(s))return"facebook / social";if(/(^|\.)instagram\./.test(s))return"instagram / social";if(/(^|\.)tiktok\./.test(s))return"tiktok / social";if(/(^|\.)youtube\./.test(s))return"youtube / social";if(/(^|\.)twitter\.|(^|\.)x\.com/.test(s))return"x / social";if(/(^|\.)reddit\./.test(s))return"reddit / social";if(/(^|\.)pinterest\./.test(s))return"pinterest / social";if(/(^|\.)linkedin\./.test(s))return"linkedin / social";try{const o=new URL(s.startsWith("http")?s:`https://${s}`).hostname.replace(/^www\./,"");return o?`${o} / referral`:"direct"}catch{return"direct"}}function Pe(n){if(n==="all_time")return null;const a=new Date,s=N(a);switch(n){case"today":return{from:s,to:p(a)};case"7d":return{from:N(q(a,6)),to:p(a)};case"30d":return{from:N(q(a,29)),to:p(a)};case"wtd":return{from:Me(a,{weekStartsOn:1}),to:p(a)};case"mtd":return{from:le(a),to:p(a)};case"qtd":{const o=Math.floor(a.getMonth()/3)*3;return{from:new Date(a.getFullYear(),o,1),to:p(a)}}case"ytd":return{from:ie(a),to:p(a)};case"last_month":{const o=Re(a);return{from:le(o),to:Ye(o)}}case"last_year":{const o=Ae(a);return{from:ie(o),to:new Date(o.getFullYear(),11,31,23,59,59)}}default:return{from:N(q(a,29)),to:p(a)}}}const mt=()=>{const[n,a]=Y("admin-attribution-preset","all_time"),[s,o]=Y("admin-attribution-from",""),[x,M]=Y("admin-attribution-to",""),y=R.useMemo(()=>n==="custom"&&s&&x?{from:N(j(s)),to:p(j(x))}:Pe(n),[n,s,x]),{data:W=[],isLoading:me}=G({queryKey:["admin-attribution-orders"],queryFn:async()=>{const t=[];let r=0;const i=1e3;for(;;){const{data:S,error:V}=await ee.from("orders").select("total, subtotal, created_at, status, origin").order("created_at",{ascending:!0}).range(r,r+i-1);if(V)throw V;if(!S||S.length===0||(t.push(...S),S.length<i))break;r+=i}return t},staleTime:5*6e4,refetchOnWindowFocus:!1}),{data:v=[],isLoading:ue}=G({queryKey:["admin-attribution-journeys"],queryFn:async()=>{const{data:t,error:r}=await ee.from("order_attribution").select("*, orders(total, subtotal, status)").order("created_at",{ascending:!1}).limit(500);if(r)throw r;return t||[]},staleTime:5*6e4,refetchOnWindowFocus:!1}),b=R.useMemo(()=>W.filter(t=>ze.includes(t.status)?y?$e(new Date(t.created_at),{start:y.from,end:y.to}):!0:!1),[W,y]),U=R.useMemo(()=>b.filter(t=>t.origin),[b]),xe=b.length,F=b.reduce((t,r)=>t+(r.total||0),0),he=U.length,O=new Map;U.forEach(t=>{const r=t.origin||"Unknown",i=O.get(r)||{count:0,revenue:0};i.count++,i.revenue+=t.total||0,O.set(r,i)});const g=Array.from(O.entries()).map(([t,r])=>({name:t,...r})).sort((t,r)=>r.revenue-t.revenue),fe=g.length>0?g[0].name:"â",ge=g.length,L=new Map;v.forEach(t=>{const r=t.device_type||"unknown";L.set(r,(L.get(r)||0)+1)});const w=Array.from(L.entries()).map(([t,r])=>({name:t,value:r})).sort((t,r)=>r.value-t.value),A=new Map;v.forEach(t=>{Array.isArray(t.touchpoints)&&new Set(t.touchpoints.map(i=>
1i.type)).forEach(i=>{A.set(i,(A.get(i)||0)+1)})});const I=Array.from(A.entries()).map(([t,r])=>({name:de(t),raw:t,count:r})).sort((t,r)=>r.count-t.count).slice(0,10),pe=({type:t})=>t==="mobile"?e.jsx(oe,{className:"h-4 w-4"}):t==="tablet"?e.jsx(Te,{className:"h-4 w-4"}):e.jsx(_e,{className:"h-4 w-4"}),je=me||ue;return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold text-foreground",children:"Order Attribution"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Track where orders come from and what drives conversions."})]}),e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsxs(ye,{value:n,onValueChange:t=>a(t),children:[e.jsx(ve,{className:"w-[160px]",children:e.jsx(Ne,{})}),e.jsxs(be,{children:[e.jsx(d,{value:"all_time",children:"All Time"}),e.jsx(d,{value:"today",children:"Today"}),e.jsx(d,{value:"7d",children:"Last 7 Days"}),e.jsx(d,{value:"30d",children:"Last 30 Days"}),e.jsx(d,{value:"wtd",children:"Week to Date"}),e.jsx(d,{value:"mtd",children:"Month to Date"}),e.jsx(d,{value:"qtd",children:"Quarter to Date"}),e.jsx(d,{value:"ytd",children:"Year to Date"}),e.jsx(d,{value:"last_month",children:"Last Month"}),e.jsx(d,{value:"last_year",children:"Last Year"}),e.jsx(d,{value:"custom",children:"Custom Range"})]})]}
1),n==="custom"&&e.jsxs(e.Fragment,{children:[e.jsxs(H,{children:[e.jsx(Q,{asChild:!0,children:e.jsxs(X,{variant:"outline",className:J("w-[150px] justify-start text-left font-normal",!s&&"text-muted-foreground"),children:[e.jsx(ne,{className:"mr-2 h-4 w-4"}),s?D(j(s),"MMM d, yyyy"):"Start date"]})}),e.jsx(Z,{className:"w-auto p-0",align:"start",children:e.jsx(te,{mode:"single",selected:s?j(s):void 0,onSelect:t=>t&&o(D(t,"yyyy-MM-dd")),disabled:t=>t>new Date,captionLayout:"dropdown-buttons",fromYear:2015,toYear:new Date().getFullYear(),initialFocus:!0,className:"p-3 pointer-events-auto"})})]}),e.jsx("span",{className:"text-muted-foreground",children:"â"}),e.jsxs(H,{children:[e.jsx(Q,{asChild:!0,children:e.jsxs(X,{variant:"outline",className:J("w-[150px] justify-start text-left font-normal",!x&&"text-muted-foreground"),children:[e.jsx(ne,{className:"mr-2 h-4 w-4"}),x?D(j(x),"MMM d, yyyy"):"End date"]})}),e.jsx(Z,{className:"w-auto p-0",align:"start",children:e.jsx(te,{mode:"single",selected:x?j(x):void 0,onSelect:t=>t&&M(D(t,"yyyy-MM-dd")),disabled:t=>t>new Date||(s?t<j(s):!1),captionLayout:"dropdown-buttons",fromYear:2015,toYear:new Date().getFullYear(),initialFocus:!0,className:"p-3 pointer-events-auto"})})]})]})]})]}),je?e.jsx(m,{className:"border-dashed",children:e.jsx(u,{className:"flex items-center justify-center py-12",children:e.jsxs("div",{className:"flex flex-col items-center gap-3",children:[e.jsx("div",{className:"h-8 w-8 animate-spin rounded-full border-4 border-primary border-t-transparent"}),e.jsx("p",{className:"text-sm font-medium text-muted-foreground",children:"Loading attribution dataâ¦"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"This may take a moment for large date ranges"})]})})}):e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid gap-4 sm:grid-cols-2 lg:grid-cols-4",children:[e.jsxs(m,{children:[e.jsx(h,{className:"pb-2",children:e.jsx(f,{className:"text-sm text-muted-foreground",children:"Total Orders"})}),e.jsx(u,{children:e.jsx("p",{className:"text-2xl font-bold text-foreground",children:xe.toLocaleString()})})]}),e.jsxs(m,{children:[e.jsx(h,{className:"pb-2",children:e.jsx(f,{className:"text-sm text-muted-foreground",children:"Total Revenue"})}),e.jsx(u,{children:e.jsxs("p",{className:"text-2xl font-bold text-foreground",children:["$",F.toLocaleString(void 0,{minimumFractionDigits:2,maximumFractionDigits:2})]})})]}),e.jsxs(m,{children:[e.jsx(h,{className:"pb-2",children:e.jsx(f,{className:"text-sm text-muted-foreground",children:"Top Source"})}),e.jsx(u,{children:e.jsx("p",{className:"text-2xl font-bold text-foreground truncate",children:fe})})]}),e.jsxs(m,{children:[e.jsx(h,{className:"pb-2",children:e.jsx(f,{className:"text-sm text-muted-foreground",children:"Unique Sources"})}),e.jsx(u,{children:e.jsx("p",{className:"text-2xl font-bold text-foreground",children:ge})})]})]}),e.jsxs("div",{className:"grid gap-6 lg:grid-cols-2",children:[e.jsxs(m,{children:[e.jsxs(h,{children:[e.jsxs(f,{className:"flex items-center gap-2 text-foreground",children:[e.jsx(Se,{className:"h-5 w-5 text-primary"})," Traffic Sources"]}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:[he.toLocaleString()," orders with origin data"]})]}),e.jsx(u,{children:g.length===0?e.jsx("p",{className:"text-muted-foreground text-sm py-4",children:"No origin data yet."}):e.jsx(T,{width:"100%",height:280,children:e.jsxs(z,{data:g.slice(0,10),children:[e.jsx(E,{strokeDasharray:"3 3",className:"stroke-border"}),e.jsx(P,{dataKey:"name",tick:{fontSize:11},className:"fill-muted-foreground",angle:-30,textAnchor:"end",height:80}),e.jsx(B,{tick:{fontSize:12},className:"fill-muted-foreground"}),e.jsx(_,{contentStyle:{background:"hsl(var(--card))",border:"1px solid hsl(var(--border))",borderRadius:8},labelStyle:{color:"hsl(var(--foreground))"},formatter:(t,r)=>[r==="revenue"?`$${t.toLocaleString(void 0,{minimumFractionDigits:2})}`:t.toLocaleString(),r==="revenue"?"Revenue":"Orders"]}),e.jsx(K,{dataKey:"count",fill:"hsl(var(--primary))",radius:[4,4,0,0],name:"orders"})]})})})]}),w.length>0?e.jsxs(m,{children:[e.jsxs(h,{children:[e.jsxs(f,{className:"flex items-center gap-2 text-foreground",children:[e.jsx(oe,{className:"h-5 w-5 text-primary"})," Device Breakdown"]}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:["From ",v.length," tracked journeys"]})]}),e.jsx(u,{children:e.jsxs("div",{className:"flex items-center gap-6",children:[e.jsx(T,{width:"50%",height:200,children:e.jsxs(Fe,{children:[e.jsx(Oe,{data:w,dataKey:"value",nameKey:"name",cx:"50%",cy:"50%",outerRadius:80,label:!0,children:w.map((t,r)=>e.jsx(Le,{fill:ce[r%ce.length]},r))}),e.jsx(_,{})]})}),e.jsx("div",{className:"space-y-2",children:w.map(t=>e.jsxs("div",{className:"flex items-center gap-2 text-sm text-foreground",children:[e.jsx(pe,{type:t.name}),e.jsx("span",{className:"capitalize",children:t.name}),e.jsx(k,{variant:"secondary",children:t.value})]},t.name))})]})})]}):e.jsxs(m,{children:[e.jsx(h,{children:e.jsxs(f,{className:"flex items-center gap-2 text-foreground",children:[e.jsx(ke,{className:"h-5 w-5 text-primary"})," Revenue by Source (Top 10)"]})}),e.jsx(u,{children:g.length===0?e.jsx("p",{className:"text-muted-foreground text-sm py-4",children:"No data yet."}):e.jsx(T,{width:"100%",height:280,children:e.jsxs(z,{data:g.slice(0,10),children:[e.jsx(E,{strokeDasharray:"3 3",className:"stroke-border"}),e.jsx(P,{dataKey:"name",tick:{fontSize:11},className:"fill-muted-foreground",angle:-30,textAnchor:"end",height:80}),e.jsx(B,{tick:{fontSize:12},className:"fill-muted-foreground"}),e.jsx(_,{contentStyle:{background:"hsl(var(--card))",border:"1px solid hsl(var(--border))",borderRadius:8},labelStyle:{color:"hsl(var(--foreground))"},formatter:t=>
1[`$${t.toLocaleString(void 0,{minimumFractionDigits:2})}`,"Revenue"]}),e.jsx(K,{dataKey:"revenue",fill:"hsl(var(--chart-2))",radius:[4,4,0,0],name:"revenue"})]})})})]})]}),I.length>0&&e.jsxs(m,{children:[e.jsxs(h,{children:[e.jsxs(f,{className:"flex items-center gap-2 text-foreground",children:[e.jsx(Ce,{className:"h-5 w-5 text-primary"})," Touchpoint Effectiveness"]}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Which on-site interactions appeared in converting orders"})]}),e.jsx(u,{children:e.jsx(T,{width:"100%",height:300,children:e.jsxs(z,{data:I,layout:"vertical",children:[e.jsx(E,{strokeDasharray:"3 3",className:"stroke-border"}),e.jsx(P,{type:"number",tick:{fontSize:12},className:"fill-muted-foreground"}),e.jsx(B,{dataKey:"name",type:"category",width:160,tick:{fontSize:12},className:"fill-muted-foreground"}),e.jsx(_,{contentStyle:{background:"hsl(var(--card))",border:"1px solid hsl(var(--border))",borderRadius:8},labelStyle:{color:"hsl(var(--foreground))"}}),e.jsx(K,{dataKey:"count",fill:"hsl(var(--chart-2))",radius:[0,4,4,0],name:"Orders with touchpoint"})]})})})]}),e.jsxs(m,{children:[e.jsx(h,{children:e.jsxs(f,{className:"flex items-center gap-2 text-foreground",children:[e.jsx(De,{className:"h-5 w-5 text-primary"})," Revenue by Source"]})}),e.jsx(u,{className:"p-0",children:g.length===0?e.jsx("p",{className:"text-muted-foreground text-sm py-8 text-center",children:"No origin data yet. Data will appear after orders are synced with origin info."}):e.jsxs(re,{children:[e.jsx(se,{children:e.jsxs(C,{children:[e.jsx(l,{children:"Source"}),e.jsx(l,{className:"text-right",children:"Orders"}),e.jsx(l,{className:"text-right",children:"Revenue"}),e.jsx(l,{className:"text-right",children:"AOV"}),e.jsx(l,{className:"text-right",children:"% of Revenue"})]})}),e.jsx(ae,{children:g.map(t=>e.jsxs(C,{children:[e.jsx(c,{className:"font-medium text-foreground",children:t.name}),e.jsx(c,{className:"text-right",children:t.count.toLocaleString()}),e.jsxs(c,{className:"text-right",children:["$",t.revenue.toLocaleString(void 0,{minimumFractionDigits:2,maximumFractionDigits:2})]}),e.jsxs(c,{className:"text-right",children:["$",(t.revenue/t.count).toFixed(2)]}),e.jsxs(c,{className:"text-right",children:[F>0?(t.revenue/F*100).toFixed(1):0,"%"]})]},t.name))})]})})]}),v.length>0&&e.jsxs(m,{children:[e.jsxs(h,{children:[e.jsx(f,{className:"text-foreground",children:"Recent Order Journeys"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Detailed journey tracking from new orders (order_attribution)"})]}),e.jsx(u,{className:"p-0",children:e.jsxs(re,{children:[e.jsx(se,{children:e.jsxs(C,{children:[e.jsx(l,{children:"Date"}),e.jsx(l,{children:"Source"}),e.jsx(l,{children:"Device"}),e.jsx(l,{children:"Pages"}),e.jsx(l,{children:"Time"}),e.jsx(l,{children:"Touchpoints"}),e.jsx(l,{className:"text-right",children:"Total"})]})}),e.jsx(ae,{children:v.slice(0,20).map(t=>e.jsxs(C,{children:[e.jsx(c,{className:"text-sm text-foreground",children:we(t.created_at,{month:"short",day:"numeric",year:"numeric"})}),e.jsx(c,{children:e.jsx(k,{variant:"outline",children:Ee(t)})}),e.jsx(c,{className:"capitalize text-foreground",children:t.device_type||"â"}),e.jsx(c,{className:"text-foreground",children:t.pages_viewed}),e.jsx(c,{className:"text-foreground",children:Be(t.time_on_site_seconds)}),e.jsx(c,{children:e.jsxs("div",{className:"flex flex-wrap gap-1",children:[Array.isArray(t.touchpoints)&&[...new Set(t.touchpoints.map(r=>r.type))].slice(0,4).map(r=>e.jsx(k,{variant:"secondary",className:"text-[10px]",children:de(r)},r)),Array.isArray(t.touchpoints)&&new Set(t.touchpoints.map(r=>r.type)).size>4&&e.jsxs(k,{variant:"secondary",className:"text-[10px]",children:["+",new Set(t.touchpoints.map(r=>r.type)).size-4]})]})}),e.jsxs(c,{className:"text-right font-medium text-foreground",children:["$",(t.orders?.total||0).toFixed(2)]})]},t.id))})]})})]})]})]})};function de(n){return{cart_add:"Cart Add",cart_remove:"Cart Remove",social_proof_click:"Social Proof",strain_finder_complete:"Strain Finder",bundle_added:"Bundle Builder",checkout_complete:"Checkout",coupon_attempt:"Coupon",free_gift_unlocked:"Free Gift",recently_viewed_click:"Recently Viewed",compare_add:"Compare"}[n]||n.replace(/_/g," ").replace(/\b\w/g,s=>s.toUpperCase())}function Be(n){if(n<60)return`${n}s`;const a=Math.floor(n/60),s=n%60;return`${a}m ${s}s`}export{mt as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.