Google Analytics 4 Integration
Track user behavior and conversions with Google Analytics 4 event-based tracking and reporting APIs.
Overview
GA4 integration requires adding the gtag.js snippet or using the Google Tag Manager (GTM) container. For SPAs and server-rendered apps, the Measurement Protocol API allows sending events from the server side, enabling accurate tracking without client-side JavaScript. The GA4 Data API provides programmatic access to reports, enabling custom dashboards and automated reporting.
Key GA4 features include enhanced measurement (automatic tracking of scrolls, outbound clicks, site search, video engagement), custom dimensions and metrics, audience segmentation, funnel analysis, and path exploration. GA4 integrates with Google Ads for conversion tracking and remarketing, BigQuery for raw data export, and Looker Studio for custom visualizations. Consent mode supports privacy regulations by adjusting tracking behavior based on user consent choices.
Who Is This For?
- Track page views and custom events in a web application
- Set up conversion tracking for e-commerce purchases
- Build custom reports with the GA4 Data API
- Implement server-side tracking with the Measurement Protocol
Installation
npm install @google-analytics/data Configuration
// Client-side gtag
window.gtag("event", "purchase", {
transaction_id: "T12345",
value: 29.99,
currency: "USD",
items: [{ item_name: "Pro Plan", price: 29.99 }],
})
// Server-side: GA4 Data API
import { BetaAnalyticsDataClient } from "@google-analytics/data"
const analytics = new BetaAnalyticsDataClient()
const [response] = await analytics.runReport({
property: "properties/123456789",
dateRanges: [{ startDate: "30daysAgo", endDate: "today" }],
metrics: [{ name: "activeUsers" }, { name: "sessions" }],
dimensions: [{ name: "country" }],
}) Explore AI Tools
Discover the best AI tools that complement your skills
Read AI & Design Articles
Tips and trends in the world of design and AI
Related Servers
Sentry Error Tracking
Monitor errors and performance issues in production with Sentry. AI agents can triage alerts and suggest fixes based on stack traces.
PostHog Product Analytics
Track product usage, manage feature flags, and analyze user behavior with PostHog, an open-source product analytics platform.
Database Query Builder
Generate and optimize SQL queries with AI assistance. Build complex queries, analyze execution plans, and improve database performance.