ifixkart-admin/app/(admin)/integrations/google-analytics/page.tsx

163 lines
6.7 KiB
TypeScript

'use client';
import React, { useState, useEffect } from 'react';
import Link from 'next/link';
import {
Users,
Eye,
Clock,
Radio,
ArrowUpRight,
RefreshCw,
} from 'lucide-react';
import { toast } from 'sonner';
import { IntegrationsHeaderNav } from '@/components/integrations/IntegrationsHeaderNav';
import { apiFetch } from '@/services/api/client';
import { API_ENDPOINTS } from '@/services/api/config';
interface GA4Status {
enabled?: boolean;
property_id?: string;
is_ready?: boolean;
credentials_configured?: boolean;
}
interface GA4Overview {
realtime_active_users?: number;
metrics?: {
active_users?: number;
screen_page_views?: number;
average_session_duration?: number;
};
}
export default function GoogleAnalyticsIntegrationPage() {
const [status, setStatus] = useState<GA4Status | null>(null);
const [overview, setOverview] = useState<GA4Overview | null>(null);
const [loading, setLoading] = useState(true);
const fetchGA4 = async () => {
setLoading(true);
try {
const [statusRes, overviewRes] = await Promise.all([
apiFetch<GA4Status>(API_ENDPOINTS.GOOGLE_ANALYTICS_STATUS),
apiFetch<{ data?: GA4Overview }>(`${API_ENDPOINTS.GOOGLE_ANALYTICS_OVERVIEW}?days=30`),
]);
setStatus(statusRes);
if (overviewRes && overviewRes.data) {
setOverview(overviewRes.data);
}
} catch {
toast.error('Failed to fetch GA4 integration status');
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchGA4();
}, []);
const dataCardShell =
'crm-radius-section border border-border bg-card shadow-[0_1px_3px_rgba(16,24,40,0.06)] min-w-0';
const primaryButton =
'inline-flex items-center justify-center gap-2 h-9 px-4 crm-radius-control bg-primary hover:bg-primary/90 text-white text-[13px] font-semibold cursor-pointer';
const secondaryButton =
'inline-flex items-center justify-center gap-2 h-9 px-4 crm-radius-control border border-border bg-card text-foreground text-[13px] font-medium hover:bg-muted cursor-pointer disabled:opacity-50';
const statValue = (value: number | undefined) => (loading ? '—' : (value ?? 0).toLocaleString());
const ready = Boolean(status?.is_ready);
const credentials = Boolean(status?.credentials_configured);
return (
<div className="flex flex-col gap-5 min-w-0">
<div className="flex flex-wrap items-center justify-between gap-3">
<h1 className="text-lg font-semibold text-foreground">Google Analytics</h1>
<div className="flex flex-wrap items-center gap-2">
<button type="button" onClick={fetchGA4} disabled={loading} className={secondaryButton}>
<RefreshCw className={`w-3.5 h-3.5 ${loading ? 'animate-spin' : ''}`} />
Refresh
</button>
<Link href="/analytics/google" className={primaryButton}>
Open analytics
<ArrowUpRight className="w-3.5 h-3.5" />
</Link>
</div>
</div>
<IntegrationsHeaderNav activeTab="google-analytics" />
<div className={`${dataCardShell} p-4 flex flex-wrap items-center justify-between gap-3`}>
<div className="min-w-0">
<p className="text-[13px] text-muted-foreground">Property</p>
<p className="text-[15px] font-semibold text-foreground mt-0.5 truncate">
{loading ? '—' : status?.property_id || '—'}
</p>
</div>
<div className="flex flex-wrap items-center gap-2">
<span className={`inline-flex items-center px-2.5 py-1 crm-radius-badge text-[11px] font-semibold ${
credentials ? 'bg-success/15 text-success' : 'bg-muted text-muted-foreground'
}`}>
{loading ? '—' : credentials ? 'Credentials configured' : 'Credentials missing'}
</span>
<span className={`inline-flex items-center px-2.5 py-1 crm-radius-badge text-[11px] font-semibold ${
ready ? 'bg-success/15 text-success' : 'bg-warning/15 text-warning'
}`}>
{loading ? '—' : ready ? 'Ready' : 'Not ready'}
</span>
</div>
</div>
<div className="grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-4 gap-4">
<div className={`${dataCardShell} p-4`}>
<div className="flex items-start justify-between gap-2">
<div>
<p className="text-[13px] text-muted-foreground">Realtime users</p>
<p className="text-[22px] font-bold text-foreground mt-2 tracking-tight">{statValue(overview?.realtime_active_users)}</p>
<p className="text-[12px] text-muted-foreground mt-2">Last 30 minutes</p>
</div>
<span className="w-9 h-9 rounded-full bg-primary/10 text-primary flex items-center justify-center shrink-0">
<Radio className="w-4 h-4" />
</span>
</div>
</div>
<div className={`${dataCardShell} p-4`}>
<div className="flex items-start justify-between gap-2">
<div>
<p className="text-[13px] text-muted-foreground">Active users</p>
<p className="text-[22px] font-bold text-foreground mt-2 tracking-tight">{statValue(overview?.metrics?.active_users)}</p>
<p className="text-[12px] text-muted-foreground mt-2">Last 30 days</p>
</div>
<span className="w-9 h-9 rounded-full bg-primary/10 text-primary flex items-center justify-center shrink-0">
<Users className="w-4 h-4" />
</span>
</div>
</div>
<div className={`${dataCardShell} p-4`}>
<div className="flex items-start justify-between gap-2">
<div>
<p className="text-[13px] text-muted-foreground">Page views</p>
<p className="text-[22px] font-bold text-foreground mt-2 tracking-tight">{statValue(overview?.metrics?.screen_page_views)}</p>
</div>
<span className="w-9 h-9 rounded-full bg-primary/10 text-primary flex items-center justify-center shrink-0">
<Eye className="w-4 h-4" />
</span>
</div>
</div>
<div className={`${dataCardShell} p-4`}>
<div className="flex items-start justify-between gap-2">
<div>
<p className="text-[13px] text-muted-foreground">Avg session</p>
<p className="text-[22px] font-bold text-foreground mt-2 tracking-tight">
{loading ? '—' : `${Math.round(overview?.metrics?.average_session_duration ?? 0)}s`}
</p>
</div>
<span className="w-9 h-9 rounded-full bg-primary/10 text-primary flex items-center justify-center shrink-0">
<Clock className="w-4 h-4" />
</span>
</div>
</div>
</div>
</div>
);
}