Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 26 additions & 42 deletions google-analytics-data/run_report_with_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,50 +44,34 @@ def run_report_with_pagination(property_id="YOUR-GA4-PROPERTY-ID"):
"""Runs a report several times, each time retrieving a portion of result
using pagination."""
client = BetaAnalyticsDataClient()
limit = 100000
offset = 0

# [START analyticsdata_run_report_with_pagination_page1]
request = RunReportRequest(
property=f"properties/{property_id}",
date_ranges=[DateRange(start_date="365daysAgo", end_date="yesterday")],
dimensions=[
Dimension(name="firstUserSource"),
Dimension(name="firstUserMedium"),
Dimension(name="firstUserCampaignName"),
],
metrics=[
Metric(name="sessions"),
Metric(name="keyEvents"),
Metric(name="totalRevenue"),
],
limit=100000,
offset=0,
)
response = client.run_report(request)
# [END analyticsdata_run_report_with_pagination_page1]
print_run_report_response(response)
while True:
request = RunReportRequest(
property=f"properties/{property_id}",
date_ranges=[DateRange(start_date="365daysAgo", end_date="yesterday")],
dimensions=[
Dimension(name="firstUserSource"),
Dimension(name="firstUserMedium"),
Dimension(name="firstUserCampaignName"),
],
metrics=[
Metric(name="sessions"),
Metric(name="keyEvents"),
Metric(name="totalRevenue"),
],
limit=limit,
offset=offset,
)
response = client.run_report(request)
print_run_report_response(response)

# Run the same report with a different offset value to retrieve the second
# page of a response.
# [START analyticsdata_run_report_with_pagination_page2]
request = RunReportRequest(
property=f"properties/{property_id}",
date_ranges=[DateRange(start_date="365daysAgo", end_date="yesterday")],
dimensions=[
Dimension(name="firstUserSource"),
Dimension(name="firstUserMedium"),
Dimension(name="firstUserCampaignName"),
],
metrics=[
Metric(name="sessions"),
Metric(name="keyEvents"),
Metric(name="totalRevenue"),
],
limit=100000,
offset=100000,
)
response = client.run_report(request)
# [END analyticsdata_run_report_with_pagination_page2]
print_run_report_response(response)
# row_count is the total number of rows matching the request. Request
# another page only when the current page does not reach that total.
if offset + limit >= response.row_count:
break
offset += limit


# [END analyticsdata_run_report_with_pagination]
Expand Down
20 changes: 20 additions & 0 deletions google-analytics-data/run_report_with_pagination_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# limitations under the License.

import os
from types import SimpleNamespace
from unittest import mock

import run_report_with_pagination

Expand All @@ -23,3 +25,21 @@ def test_run_report_with_pagination(capsys):
run_report_with_pagination.run_report_with_pagination(TEST_PROPERTY_ID)
out, _ = capsys.readouterr()
assert "Report result" in out


def test_run_report_with_pagination_requests_pages_until_row_count():
responses = [
SimpleNamespace(row_count=250001, rows=[]),
SimpleNamespace(row_count=250001, rows=[]),
SimpleNamespace(row_count=250001, rows=[]),
]
client = mock.Mock()
client.run_report.side_effect = responses

with mock.patch.object(
run_report_with_pagination, "BetaAnalyticsDataClient", return_value=client
), mock.patch.object(run_report_with_pagination, "print_run_report_response"):
run_report_with_pagination.run_report_with_pagination("123")

offsets = [request.offset for (request,) in client.run_report.call_args_list]
assert offsets == [0, 100000, 200000]
Loading