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

1

6

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)
Nunuance answered 16/7, 2021 at 11:33 Comment(0)
C
6

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

Cute answered 16/7, 2021 at 15:35 Comment(3)
This seems logical @Brett, but can you please take a look at my code snippet in the post? I didn't seem to figure out how to add the minute ranges to my code. Thank you!Nunuance
Minute ranges were just recently added to the API. You'll need to update to the latest v0.7.0 released on 2021-07-10: googleapis.dev/python/analyticsdata/latest/changelog.html#id1. After that, the python code will look like minute_ranges=[MinuteRange(start_minutes_ago=5, end_minutes_ago=0)],Cute
Thank you very much @Brett. You've solved my problem clearly and successfully! Accepted!Nunuance

© 2022 - 2024 — McMap. All rights reserved.