Active users is a core metric in Google Analytics 4—it tells you how many distinct users engaged with your property during a given time period. But GA4's definition of "active" is strict: only users who triggered at least one engagement event count. You need to ensure your events are configured correctly to get accurate numbers.
Set Up Event Tracking in GA4
GA4 won't track active users unless you're collecting events. Start by installing the SDK and configuring basic event tracking.
Install the GA4 SDK (gtag.js)
Add the Google Analytics SDK to your HTML. You'll find your Measurement ID in Admin > Property > Tracking Info > Tracking Code. This snippet initializes GA4 and starts collecting page views automatically.
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXX', {
'page_path': window.location.pathname
});
</script>Enable User ID Tracking (optional but recommended)
If you have logged-in users, set a User ID in GA4 to identify the same person across devices and sessions. Go to Admin > Property Settings and enable User-ID feature. Then pass the user ID to gtag().
gtag('config', 'G-XXXXX', {
'user_id': 'USER_ID_123',
'user_properties': {
'subscription_status': 'premium',
'customer_lifetime_value': 299.99
}
});Track Custom Events
Active users are counted based on engagement events. Page views count, but so do button clicks, form submissions, and purchases. Send events using gtag('event', ...) with relevant parameters.
// Track a button click
gtag('event', 'button_click', {
'button_name': 'sign_up'
});
// Track a purchase
gtag('event', 'purchase', {
'transaction_id': 'txn_12345',
'value': 99.99,
'currency': 'USD'
});
// Track a form submission
gtag('event', 'form_submit', {
'form_id': 'contact_form'
});View Active Users in GA4 Reports
Once events are flowing, you can see active user metrics in the GA4 dashboard. GA4 provides real-time active user data and historical trends.
Check Real-Time Active Users
Navigate to Reports > Real-time. The Active Users card shows users currently on your property in the last 30 minutes, updating every few seconds. This is useful for debugging your event setup or monitoring during product launches.
// Query real-time active users (Google Analytics Data API)
const request = {
property: 'properties/YOUR_PROPERTY_ID',
metrics: [
{ name: 'activeUsers' }
],
dateRanges: [
{
startDate: 'today',
endDate: 'today'
}
]
};
// Data is delayed by 3-5 seconds and covers the last 30 minutesView Historical Active Users
Go to Reports > User to see the Active Users metric over time. By default, GA4 shows daily active users for your selected date range. You can break down active users by Country, Device Category, User Segment, or any custom dimension.
const {BetaAnalyticsDataClient} = require('@google-analytics/data');
const client = new BetaAnalyticsDataClient();
const response = await client.runReport({
property: 'properties/YOUR_PROPERTY_ID',
dateRanges: [
{
startDate: '2024-03-01',
endDate: '2024-03-26'
}
],
metrics: [
{ name: 'activeUsers' }
],
dimensions: [
{ name: 'date' }
]
});
response.rows.forEach(row => {
const date = row.dimensionValues[0].value;
const users = row.metricValues[0].value;
console.log(`${date}: ${users} active users`);
});Segment Active Users by Dimension
Create segments to track subsets of active users. Click Add Segment in the Users report and build a condition (e.g., "users from the United States" or "users who viewed the pricing page"). Active user counts will recalculate for that segment.
// Query active users filtered by country
const response = await client.runReport({
property: 'properties/YOUR_PROPERTY_ID',
dateRanges: [
{
startDate: '2024-03-01',
endDate: '2024-03-26'
}
],
metrics: [
{ name: 'activeUsers' }
],
dimensions: [
{ name: 'country' }
],
dimensionFilter: {
filter: {
fieldName: 'country',
stringFilter: {
matchType: 'EXACT',
value: 'United States'
}
}
}
});
response.rows.forEach(row => {
console.log(`${row.dimensionValues[0].value}: ${row.metricValues[0].value}`);
});Extract Active Users Data Programmatically
For dashboards or automated reports, query active users via the Google Analytics Data API. This API returns the exact same metrics you see in the GA4 interface.
Set Up Google Analytics Data API Credentials
Create a service account in Google Cloud > Service Accounts and download the JSON key file. Then in GA4, navigate to Admin > Property > Property Access Management and grant the service account Viewer role on your property. This allows your application to query data programmatically.
// npm install @google-analytics/data
const {BetaAnalyticsDataClient} = require('@google-analytics/data');
const analyticsDataClient = new BetaAnalyticsDataClient({
projectId: 'YOUR_GCP_PROJECT_ID',
keyFilename: '/path/to/service-account-key.json'
});
console.log('GA4 Data API client initialized');Query Active Users for a Date Range
Use the runReport() method to fetch active user metrics. Specify your Property ID (found in Admin > Property > Property Details), the date range, and the activeUsers metric.
async function getActiveUsers() {
const [response] = await analyticsDataClient.runReport({
property: 'properties/1234567890',
dateRanges: [
{
startDate: '2024-03-01',
endDate: '2024-03-26'
}
],
metrics: [
{ name: 'activeUsers' }
]
});
if (response.rows.length === 0) {
console.log('No data available');
return;
}
const activeUsers = response.rows[0].metricValues[0].value;
console.log(`Active users: ${activeUsers}`);
return activeUsers;
}
await getActiveUsers();Compare Active Users Across Segments
To compare active users by a dimension (e.g., device type, traffic source, or user segment), add a dimensions array to your query. GA4 will return active user counts broken down by each dimension value.
async function getActiveUsersByDevice() {
const [response] = await analyticsDataClient.runReport({
property: 'properties/1234567890',
dateRanges: [
{
startDate: '2024-03-01',
endDate: '2024-03-26'
}
],
metrics: [
{ name: 'activeUsers' }
],
dimensions: [
{ name: 'deviceCategory' }
]
});
response.rows.forEach((row) => {
const device = row.dimensionValues[0].value;
const users = row.metricValues[0].value;
console.log(`${device}: ${users} active users`);
});
}
await getActiveUsersByDevice();offset and limit parameters in your request.Common Pitfalls
- Forgetting to set up events: GA4 counts active users only if they trigger engagement events. Page views alone don't always count—you need to configure custom events properly.
- Misunderstanding User ID feature: Enabling User ID tracking doesn't retroactively merge historical sessions. It only applies to new sessions after activation.
- Confusing active users with sessions: Active users is a count of unique users with engagement. Sessions count distinct browsing periods. One user can have multiple sessions.
- Not filtering out internal traffic: If you have staging or test environments sending events to the same GA4 property, your active user count will be inflated. Use Admin > Data Filters to exclude internal IP ranges.
Wrapping Up
You now have the foundational setup for tracking active users in GA4: events flowing in, visibility in reports, and programmatic access via the Data API. Active user metrics give you a clear view of how many real people are engaging with your property—critical data for understanding product adoption and growth. If you want to track this metric automatically across all your tools and tie it to product changes, Product Analyst can help.