|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2022 Google Inc. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Google Analytics Data API sample application demonstrating the creation of |
| 18 | +a realtime report using minute ranges. |
| 19 | +
|
| 20 | +See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runRealtimeReport#body.request_body.FIELDS.minute_ranges |
| 21 | +for more information. |
| 22 | +""" |
| 23 | +# [START analyticsdata_run_realtime_report_with_minute_ranges] |
| 24 | +from google.analytics.data_v1beta import BetaAnalyticsDataClient |
| 25 | +from google.analytics.data_v1beta.types import Metric |
| 26 | +from google.analytics.data_v1beta.types import MinuteRange |
| 27 | +from google.analytics.data_v1beta.types import RunRealtimeReportRequest |
| 28 | + |
| 29 | +from run_report import print_run_report_response |
| 30 | + |
| 31 | + |
| 32 | +def run_sample(): |
| 33 | + """Runs the sample.""" |
| 34 | + # TODO(developer): Replace this variable with your Google Analytics 4 |
| 35 | + # property ID before running the sample. |
| 36 | + property_id = "YOUR-GA4-PROPERTY-ID" |
| 37 | + run_realtime_report_with_minute_ranges(property_id) |
| 38 | + |
| 39 | + |
| 40 | +def run_realtime_report_with_minute_ranges(property_id="YOUR-GA4-PROPERTY-ID"): |
| 41 | + """Runs a realtime report on a Google Analytics 4 property. Dimensions |
| 42 | + field is omitted in the query, which results in total values of active users |
| 43 | + returned for each minute range in the report. |
| 44 | +
|
| 45 | + Note the `dateRange` dimension added to the report response automatically |
| 46 | + as a result of querying multiple minute ranges. |
| 47 | + """ |
| 48 | + client = BetaAnalyticsDataClient() |
| 49 | + |
| 50 | + request = RunRealtimeReportRequest( |
| 51 | + property=f"properties/{property_id}", |
| 52 | + metrics=[Metric(name="activeUsers")], |
| 53 | + minute_ranges=[ |
| 54 | + MinuteRange(name="0-4 minutes ago", start_minutes_ago=4), |
| 55 | + MinuteRange( |
| 56 | + name="25-29 minutes ago", start_minutes_ago=29, end_minutes_ago=25 |
| 57 | + ), |
| 58 | + ], |
| 59 | + ) |
| 60 | + response = client.run_realtime_report(request) |
| 61 | + print_run_report_response(response) |
| 62 | + |
| 63 | + |
| 64 | +# [END analyticsdata_run_realtime_report_with_minute_ranges] |
| 65 | + |
| 66 | + |
| 67 | +if __name__ == "__main__": |
| 68 | + run_sample() |
0 commit comments