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

163 lines
6.9 KiB
TypeScript

'use client';
import React, { useState, useEffect } from 'react';
import Link from 'next/link';
import {
BarChart3,
TrendingUp,
Users,
Eye,
Clock,
ExternalLink,
ShieldCheck,
CheckCircle2,
RefreshCw,
Radio,
ArrowUpRight,
} 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';
export default function GoogleAnalyticsIntegrationPage() {
const [status, setStatus] = useState<any>(null);
const [overview, setOverview] = useState<any>(null);
const [loading, setLoading] = useState(true);
const fetchGA4 = async () => {
setLoading(true);
try {
const [statusRes, overviewRes] = await Promise.all([
apiFetch<any>(API_ENDPOINTS.GOOGLE_ANALYTICS_STATUS),
apiFetch<any>(`${API_ENDPOINTS.GOOGLE_ANALYTICS_OVERVIEW}?days=30`),
]);
setStatus(statusRes);
if (overviewRes && overviewRes.data) {
setOverview(overviewRes.data);
}
} catch (err: any) {
toast.error('Failed to fetch GA4 integration status');
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchGA4();
}, []);
return (
<div className="p-6 max-w-7xl mx-auto space-y-6">
<IntegrationsHeaderNav activeTab="google-analytics" />
{/* Top Banner */}
<div className="p-4 rounded-xl bg-amber-50 dark:bg-amber-950/30 border border-amber-200 dark:border-amber-900/50 flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4">
<div>
<h2 className="text-base font-semibold text-amber-900 dark:text-amber-100 flex items-center gap-2">
<BarChart3 className="w-4 h-4 text-amber-600" />
Google Analytics 4 (GA4) Telemetry Integration
</h2>
<p className="text-xs text-amber-700 dark:text-amber-300 mt-0.5">
Real-time analytics tracking, visitor acquisition, engagement rates, and Google Data API synchronization.
</p>
</div>
<div className="flex items-center gap-2">
<span className="inline-flex items-center gap-1.5 px-3 py-1 rounded-full bg-blue-100 dark:bg-blue-900 text-blue-800 dark:text-blue-200 text-xs font-semibold">
<ShieldCheck className="w-3.5 h-3.5 text-blue-600" />
Configured via Config.py
</span>
<Link
href="/analytics/google"
className="px-3 py-1.5 rounded-lg bg-amber-600 hover:bg-amber-700 text-white text-xs font-semibold flex items-center gap-1.5 shadow-sm transition"
>
Open Full GA4 Board <ArrowUpRight className="w-3.5 h-3.5" />
</Link>
</div>
</div>
{loading ? (
<div className="grid grid-cols-1 md:grid-cols-4 gap-4 animate-pulse">
{[1, 2, 3, 4].map((i) => (
<div key={i} className="h-28 rounded-xl bg-gray-100 dark:bg-gray-800" />
))}
</div>
) : (
<>
{/* Connection Overview */}
<div className="rounded-xl border border-gray-200 dark:border-gray-800 bg-white dark:bg-gray-900 p-6 shadow-sm">
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4 border-b border-gray-100 dark:border-gray-800 pb-4">
<div>
<span className="text-xs font-bold uppercase tracking-wider text-amber-600 dark:text-amber-400">
Active Property
</span>
<h3 className="text-xl font-bold text-gray-900 dark:text-white mt-0.5">
Property ID: {status?.property_id || '521198579'}
</h3>
</div>
<div className="flex items-center gap-2">
<span className="inline-flex items-center gap-1.5 px-3 py-1 rounded-full bg-emerald-50 dark:bg-emerald-950/50 text-emerald-700 dark:text-emerald-300 text-xs font-semibold border border-emerald-200 dark:border-emerald-800">
<CheckCircle2 className="w-3.5 h-3.5 text-emerald-500" />
Service Account Connected
</span>
</div>
</div>
<div className="grid grid-cols-2 md:grid-cols-4 gap-4 mt-6">
<div className="p-4 rounded-xl bg-gray-50 dark:bg-gray-800/50 border border-gray-100 dark:border-gray-800">
<div className="flex items-center gap-2 text-xs text-gray-500 mb-1">
<Radio className="w-3.5 h-3.5 text-emerald-500 animate-pulse" />
Realtime Users (Last 30m)
</div>
<div className="text-2xl font-bold text-gray-900 dark:text-white">
{overview?.realtime_active_users ?? 0}
</div>
</div>
<div className="p-4 rounded-xl bg-gray-50 dark:bg-gray-800/50 border border-gray-100 dark:border-gray-800">
<div className="flex items-center gap-2 text-xs text-gray-500 mb-1">
<Users className="w-3.5 h-3.5 text-blue-500" />
30-Day Active Users
</div>
<div className="text-2xl font-bold text-gray-900 dark:text-white">
{overview?.metrics?.active_users?.toLocaleString() ?? 0}
</div>
</div>
<div className="p-4 rounded-xl bg-gray-50 dark:bg-gray-800/50 border border-gray-100 dark:border-gray-800">
<div className="flex items-center gap-2 text-xs text-gray-500 mb-1">
<Eye className="w-3.5 h-3.5 text-purple-500" />
Screen / Page Views
</div>
<div className="text-2xl font-bold text-gray-900 dark:text-white">
{overview?.metrics?.screen_page_views?.toLocaleString() ?? 0}
</div>
</div>
<div className="p-4 rounded-xl bg-gray-50 dark:bg-gray-800/50 border border-gray-100 dark:border-gray-800">
<div className="flex items-center gap-2 text-xs text-gray-500 mb-1">
<Clock className="w-3.5 h-3.5 text-amber-500" />
Avg Session Duration
</div>
<div className="text-2xl font-bold text-gray-900 dark:text-white">
{overview?.metrics?.average_session_duration
? `${Math.round(overview.metrics.average_session_duration)}s`
: '0s'}
</div>
</div>
</div>
<div className="mt-6 flex justify-end">
<Link
href="/analytics/google"
className="inline-flex items-center gap-2 px-4 py-2 text-sm font-semibold rounded-lg bg-amber-600 hover:bg-amber-700 text-white shadow-sm transition"
>
Launch Deep Telemetry Dashboard <ArrowUpRight className="w-4 h-4" />
</Link>
</div>
</div>
</>
)}
</div>
);
}