'How to get the number of currently active users instead of the 30 minutes count in GA4?

I've recently added a Google Analytics GA4 tag to a website for the purpose of counting the currently active users. Before, I could see the number of real-time users right now (I think it had a one-minute delay, but I'm not sure).

But in the new GA4, I can only see the number corresponding to 30 minutes, which is not what I needed.

I've looked around and found an option to add a time dimension of 1 minute, but it was for the old google analytics, and it didn't seem right to me.

Not sure if it's needed to provide my code for this question, but if it's a must, then I'll add it.

Edit:

#Run a realtime report to get desired metrics.
def run_realtime_report(property_id):
    #Runs a realtime report on a Google Analytics 4 property.
    client = BetaAnalyticsDataClient()
    #Run the request.
    request = RunRealtimeReportRequest(
        property=f"properties/{property_id}",
        metrics=[Metric(name="activeUsers")],
    )
    #Parse the response.
    response = client.run_realtime_report(request)
    ...
    return activeUsers

#Run the realtime report function.
def run_sample():
    global property_id
    return run_realtime_report(property_id)


Solution 1:[1]

The number of users in the last 30 minutes in GA4 is similar to the "active users on site right now" in Universal Analytics (UA). However after about 5 minutes of inactivity, UA sometimes assesses users are no longer active on the site. This realtime report was generated with pageviews in the last 5 minutes:

realtime pageviews in the last 5 minutes

After 5 minutes of inactivity, the active users on site goes to zero:

realtime pageviews more than 5 minutes ago

In GA4, you could recreate that calculation by specifying the minutesRange in your realtime report request. This page describes the minuteRange parameter. As a JSON request, this report will only count users who were active in the last 5 minutes:

{
  "metrics": [
    {
      "name": "activeUsers"
    }
  ],
  "minuteRanges": [
    {
      "startMinutesAgo": 5,
      "endMinutesAgo": 0
    }
  ]
}

This request is different from GA4 realtime reporting which highlights the "users in last 30 minutes" as the primary realtime metric:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Brett