The GA4 SQL Driver allows you to query your Google Analytics 4 property using familiar SQL syntax. This integration transforms GA4’s Data API into a SQL-queryable interface, making it easy to analyze your analytics data alongside other databases.
-- Explore available dataSELECT * FROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-07'LIMIT 5;-- Get top countries by user countSELECT country, totalUsers, sessionsFROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31'GROUP BY countryORDER BY totalUsers DESCLIMIT 10;
The GA4 driver supports a focused SQL subset optimized for analytics queries:
Copy
SELECT <dimensions..., metrics...>FROM ga4 [WHERE date BETWEEN 'YYYY-MM-DD' AND 'YYYY-MM-DD' [AND <dimension> = 'value' ...]][GROUP BY <dimensions...>][ORDER BY <field> [ASC|DESC]][LIMIT N [OFFSET M]]
-- Required date rangeWHERE date BETWEEN '2024-01-01' AND '2024-01-31'-- Combined with other filtersWHERE date BETWEEN '2024-01-01' AND '2024-01-31' AND country = 'United States' AND deviceCategory = 'mobile'
-- List all dimensionsLIST DIMENSIONS;-- List all metrics LIST METRICS;-- List both dimensions and metricsLIST FIELDS;-- Limit resultsLIST DIMENSIONS LIMIT 10;
-- Daily active users over timeSELECT date, activeUsers, newUsersFROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31'ORDER BY date;-- Top traffic sourcesSELECT source, medium, totalUsers, sessionsFROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31'GROUP BY source, mediumORDER BY totalUsers DESCLIMIT 10;
-- Device category breakdownSELECT deviceCategory, operatingSystem, activeUsers, bounceRateFROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31' AND country = 'United States'GROUP BY deviceCategory, operatingSystemORDER BY activeUsers DESC;-- Browser performanceSELECT browser, browserVersion, totalUsers, engagementRateFROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31'GROUP BY browser, browserVersionHAVING totalUsers > 100ORDER BY engagementRate DESC;
-- Country performanceSELECT country, region, totalUsers, sessions, engagementRateFROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31'GROUP BY country, regionORDER BY totalUsers DESCLIMIT 20;-- City-level analysisSELECT city, country, activeUsers, averageSessionDurationFROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31' AND country IN ('United States', 'Canada', 'United Kingdom')GROUP BY city, countryORDER BY activeUsers DESC;
-- Top pages by viewsSELECT pagePath, pageTitle, screenPageViews, uniquePageviewsFROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31'GROUP BY pagePath, pageTitleORDER BY screenPageViews DESCLIMIT 15;-- Landing page analysisSELECT landingPage, totalUsers, bounceRate, engagementRateFROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31'GROUP BY landingPageORDER BY totalUsers DESC;
-- Campaign performanceSELECT campaignName, source, medium, totalUsers, conversions, totalRevenueFROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31' AND campaignName IS NOT NULLGROUP BY campaignName, source, mediumORDER BY totalRevenue DESC;-- Channel performanceSELECT sessionDefaultChannelGroup, activeUsers, sessions, engagementRateFROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31'GROUP BY sessionDefaultChannelGroupORDER BY activeUsers DESC;
-- Revenue by sourceSELECT source, medium, totalRevenue, ecommercePurchases, totalRevenue / ecommercePurchases as avgOrderValueFROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31' AND totalRevenue > 0GROUP BY source, mediumORDER BY totalRevenue DESC;-- Product performanceSELECT itemName, itemCategory, itemRevenue, itemsPurchasedFROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31' AND itemName IS NOT NULLGROUP BY itemName, itemCategoryORDER BY itemRevenue DESC;
-- Top eventsSELECT eventName, eventCount, totalUsersFROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31'GROUP BY eventNameORDER BY eventCount DESCLIMIT 10;-- Event funnel analysisSELECT eventName, deviceCategory, eventCount, eventCount / totalUsers as eventsPerUserFROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31' AND eventName IN ('page_view', 'scroll', 'click', 'purchase')GROUP BY eventName, deviceCategoryORDER BY eventName, eventsPerUser DESC;
-- Get complete field listsLIST DIMENSIONS; -- All available dimensionsLIST METRICS; -- All available metrics LIST FIELDS; -- Both dimensions and metricsDESCRIBE ga4; -- Table schema with data types
-- ✅ Good: Always filter by dateSELECT country, totalUsers FROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31';-- ❌ Bad: Missing date filter (will fail)SELECT country, totalUsers FROM ga4;
-- ✅ Good: Group by dimensions when using metricsSELECT country, SUM(totalUsers) as users FROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31'GROUP BY country;-- ✅ Good: Use built-in aggregated metricsSELECT country, totalUsers FROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31'GROUP BY country;
-- ✅ Good: Use specific date rangesWHERE date BETWEEN '2024-01-01' AND '2024-01-31'-- ✅ Good: Limit resultsLIMIT 100-- ✅ Good: Filter earlyWHERE date BETWEEN '2024-01-01' AND '2024-01-31' AND country = 'United States'
-- ✅ Good: Respect GA4 cardinality limitsSELECT country, totalUsers FROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31'GROUP BY countryLIMIT 1000;-- ⚠️ Be careful with high-cardinality dimensionsSELECT pagePath, totalUsers FROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31'GROUP BY pagePathLIMIT 100; -- Limit to avoid sampling
-- ❌ Error: Invalid field nameSELECT campaign FROM ga4; -- ✅ Fix: Use correct field nameSELECT campaignName FROM ga4;
Solution: Use LIST DIMENSIONS to see valid field names.2. “User does not have sufficient permissions”Solution: Ensure your service account has Viewer access to the GA4 property.3. “Property not found”Solution: Verify you’re using the correct Property ID (numeric, not Measurement ID).4. Missing date range
Copy
-- ❌ Error: Missing required date filterSELECT country, totalUsers FROM ga4;-- ✅ Fix: Always include date rangeSELECT country, totalUsers FROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31';
-- Calculate conversion rateSELECT source, medium, conversions, totalUsers, (conversions * 100.0 / totalUsers) as conversionRateFROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31' AND totalUsers > 0GROUP BY source, mediumORDER BY conversionRate DESC;-- Calculate revenue per userSELECT country, totalRevenue, totalUsers, (totalRevenue / totalUsers) as revenuePerUserFROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-01-31' AND totalUsers > 0GROUP BY countryORDER BY revenuePerUser DESC;
-- Weekly trendsSELECT CONCAT(year, '-W', LPAD(week, 2, '0')) as week_label, SUM(totalUsers) as weekly_users, SUM(sessions) as weekly_sessionsFROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-03-31'GROUP BY year, weekORDER BY year, week;-- Month-over-month comparisonSELECT yearMonth, totalUsers, LAG(totalUsers) OVER (ORDER BY yearMonth) as prev_month_users, ((totalUsers - LAG(totalUsers) OVER (ORDER BY yearMonth)) * 100.0 / LAG(totalUsers) OVER (ORDER BY yearMonth)) as growth_rateFROM ( SELECT yearMonth, SUM(totalUsers) as totalUsers FROM ga4 WHERE date BETWEEN '2024-01-01' AND '2024-12-31' GROUP BY yearMonth) monthly_dataORDER BY yearMonth;