I have created google analytics ID and also enabled google analytics data API then now i want to get my website's all the datat from google analytics and want ti display on the admin panel of my website ... can you please help me that how can I made file structure, code and display the data ?
I'll help you create a dashboard that fetches and displays Google Analytics data in your admin panel. Let's build a Next.js application with a clean admin interface that shows key metrics from your Google Analytics account.
We'll create:
Let's start with the implementation:
To fetch real data from Google Analytics, you'll need to:
Set up Google Analytics API access:
Implement server-side authentication:
Create API endpoints to fetch data:
Here's how you would implement the actual Google Analytics API integration:
// lib/google-analytics.ts
import { google } from 'googleapis'
// Initialize the Analytics Data API client
export async function initializeAnalyticsClient() {
// For service account authentication
const auth = new google.auth.GoogleAuth({
credentials: {
client_email: process.env.GOOGLE_CLIENT_EMAIL,
private_key: process.env.GOOGLE_PRIVATE_KEY?.replace(/\\n/g, '\n'),
},
scopes: ['https://www.googleapis.com/auth/analytics.readonly'],
})
const analyticsDataClient = google.analyticsdata({
version: 'v1beta',
auth,
})
return analyticsDataClient
}
export async function getAnalyticsData(propertyId: string, startDate: string, endDate: string) {
const analyticsDataClient = await initializeAnalyticsClient()
// Example request for visitor metrics
const response = await analyticsDataClient.properties.runReport({
property: `properties/${propertyId}`,
requestBody: {
dateRanges: [{ startDate, endDate }],
dimensions: [{ name: 'date' }],
metrics: [
{ name: 'activeUsers' },
{ name: 'screenPageViews' },
{ name: 'sessions' },
{ name: 'bounceRate' },
],
},
})
// Process and format the response data
return processAnalyticsResponse(response.data)
}
function processAnalyticsResponse(data: any) {
// Transform the API response into the format expected by your frontend
// ...
}
For a complete Google Analytics dashboard, I recommend this file structure:
├── app/
│ ├── api/
│ │ ├── analytics/
│ │ │ └── route.ts
│ │ └── auth/
│ │ └── google/
│ │ └── route.ts
│ ├── dashboard/
│ │ ├── acquisition/
│ │ │ └── page.tsx
│ │ ├── audience/
│ │ │ └── page.tsx
│ │ ├── behavior/
│ │ │ └── page.tsx
│ │ ├── real-time/
│ │ │ └── page.tsx
│ │ ├── settings/
│ │ │ └── page.tsx
│ │ ├── layout.tsx
│ │ └── page.tsx
│ ├── layout.tsx
│ └── page.tsx
├── components/
│ ├── analytics-dashboard.tsx
│ ├── dashboard-header.tsx
│ ├── dashboard-sidebar.tsx
│ ├── date-range-picker.tsx
│ └── ui/
│ └── ... (shadcn components)
├── lib/
│ ├── analytics.ts
│ ├── google-analytics.ts
│ └── utils.ts
This structure separates concerns and makes your code maintainable as the dashboard grows.
To deploy this dashboard, you'll need to:
Let me know if you need more specific implementation details for any part of this solution!