8000 docs: add a sample for using minute ranges in realtime reports (#314) · googleapis/python-analytics-data@4f1305f · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Nov 9, 2024. It is now read-only.

Commit 4f1305f

Browse files
authored
docs: add a sample for using minute ranges in realtime reports (#314)
1 parent d24cb01 commit 4f1305f

File tree

3 files changed

+104
-6
lines changed

3 files changed

+104
-6
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2022 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import run_realtime_report_with_minute_ranges
18+
19+
TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID")
20+
21+
22+
def test_run_report_with_multiple_metrics(capsys):
23+
run_realtime_report_with_minute_ranges.run_realtime_report_with_minute_ranges(
24+
TEST_PROPERTY_ID
25+
)
26+
out, _ = capsys.readouterr()
27+
assert "Report result" in out

samples/snippets/run_report.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,15 @@ def print_run_report_response(response):
6464

6565
# [START analyticsdata_print_run_report_response_rows]
6666
print("Report result:")
67-
for row in response.rows:
68-
for dimension_value in row.dimension_values:
69-
print(dimension_value.value)
70-
71-
for metric_value in row.metric_values:
72-
print(metric_value.value)
67+
for rowIdx, row in enumerate(response.rows):
68+
print(f"\nRow {rowIdx}")
69+
for i, dimension_value in enumerate(row.dimension_values):
70+
dimension_name = response.dimension_headers[i].name
71+
print(f"{dimension_name}: {dimension_value.value}")
72+
73+
for i, metric_value in enumerate(row.metric_values):
74+
metric_name = response.metric_headers[i].name
75+
print(f"{metric_name}: {metric_value.value}")
7376
# [END analyticsdata_print_run_report_response_rows]
7477

7578

0 commit comments

Comments
 (0)
0