From 527ea80a37f2c0ed85b685ae4ab1c5ab8f0a00bb Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 19 Oct 2023 07:46:41 -0400 Subject: [PATCH] chore: remove migrated samples Migrated to: https://github.com/googleanalytics/python-docs-samples/tree/main/google-analytics-data --- samples/snippets/README.md | 33 -- samples/snippets/get_common_metadata.py | 80 ----- samples/snippets/get_common_metadata_test.py | 21 -- .../snippets/get_metadata_by_property_id.py | 56 ---- .../get_metadata_by_property_id_test.py | 25 -- samples/snippets/noxfile.py | 292 ------------------ samples/snippets/noxfile_config.py | 13 - samples/snippets/quickstart.py | 69 ----- .../snippets/quickstart_json_credentials.py | 76 ----- .../quickstart_json_credentials_test.py | 29 -- samples/snippets/quickstart_oauth2.py | 89 ------ samples/snippets/quickstart_oauth2_test.py | 24 -- samples/snippets/quickstart_test.py | 24 -- samples/snippets/requirements-test.txt | 1 - samples/snippets/requirements.txt | 2 - samples/snippets/run_batch_report.py | 78 ----- samples/snippets/run_batch_report_test.py | 25 -- samples/snippets/run_funnel_report.py | 207 ------------- samples/snippets/run_funnel_report_test.py | 25 -- samples/snippets/run_pivot_report.py | 96 ------ samples/snippets/run_pivot_report_test.py | 25 -- samples/snippets/run_realtime_report.py | 59 ---- samples/snippets/run_realtime_report_test.py | 25 -- .../run_realtime_report_with_minute_ranges.py | 70 ----- ...realtime_report_with_minute_ranges_test.py | 27 -- ...ealtime_report_with_multiple_dimensions.py | 59 ---- ...me_report_with_multiple_dimensions_test.py | 27 -- ...n_realtime_report_with_multiple_metrics.py | 59 ---- ...ltime_report_with_multiple_metrics_test.py | 25 -- samples/snippets/run_report.py | 85 ----- samples/snippets/run_report_test.py | 25 -- .../snippets/run_report_with_aggregations.py | 68 ---- .../run_report_with_aggregations_test.py | 25 -- samples/snippets/run_report_with_cohorts.py | 87 ------ .../snippets/run_report_with_cohorts_test.py | 25 -- .../run_report_with_custom_parameters.py | 41 --- .../snippets/run_report_with_date_ranges.py | 64 ---- .../run_report_with_date_ranges_test.py | 25 -- ...eport_with_dimension_and_metric_filters.py | 101 ------ ..._with_dimension_and_metric_filters_test.py | 27 -- ...un_report_with_dimension_exclude_filter.py | 76 ----- ...port_with_dimension_exclude_filter_test.py | 27 -- .../run_report_with_dimension_filter.py | 75 ----- .../run_report_with_dimension_filter_test.py | 25 -- ...un_report_with_dimension_in_list_filter.py | 81 ----- ...port_with_dimension_in_list_filter_test.py | 27 -- ..._report_with_multiple_dimension_filters.py | 88 ------ ...rt_with_multiple_dimension_filters_test.py | 27 -- .../run_report_with_multiple_dimensions.py | 64 ---- ...un_report_with_multiple_dimensions_test.py | 27 -- .../run_report_with_multiple_metrics.py | 66 ---- .../run_report_with_multiple_metrics_test.py | 25 -- .../run_report_with_named_date_ranges.py | 66 ---- .../run_report_with_named_date_ranges_test.py | 27 -- samples/snippets/run_report_with_ordering.py | 70 ----- .../snippets/run_report_with_ordering_test.py | 25 -- .../snippets/run_report_with_pagination.py | 97 ------ .../run_report_with_pagination_test.py | 25 -- .../run_report_with_property_quota.py | 86 ------ .../run_report_with_property_quota_test.py | 25 -- 60 files changed, 3243 deletions(-) delete mode 100644 samples/snippets/README.md delete mode 100644 samples/snippets/get_common_metadata.py delete mode 100644 samples/snippets/get_common_metadata_test.py delete mode 100644 samples/snippets/get_metadata_by_property_id.py delete mode 100644 samples/snippets/get_metadata_by_property_id_test.py delete mode 100644 samples/snippets/noxfile.py delete mode 100644 samples/snippets/noxfile_config.py delete mode 100644 samples/snippets/quickstart.py delete mode 100644 samples/snippets/quickstart_json_credentials.py delete mode 100644 samples/snippets/quickstart_json_credentials_test.py delete mode 100644 samples/snippets/quickstart_oauth2.py delete mode 100644 samples/snippets/quickstart_oauth2_test.py delete mode 100644 samples/snippets/quickstart_test.py delete mode 100644 samples/snippets/requirements-test.txt delete mode 100644 samples/snippets/requirements.txt delete mode 100644 samples/snippets/run_batch_report.py delete mode 100644 samples/snippets/run_batch_report_test.py delete mode 100644 samples/snippets/run_funnel_report.py delete mode 100644 samples/snippets/run_funnel_report_test.py delete mode 100644 samples/snippets/run_pivot_report.py delete mode 100644 samples/snippets/run_pivot_report_test.py delete mode 100644 samples/snippets/run_realtime_report.py delete mode 100644 samples/snippets/run_realtime_report_test.py delete mode 100644 samples/snippets/run_realtime_report_with_minute_ranges.py delete mode 100644 samples/snippets/run_realtime_report_with_minute_ranges_test.py delete mode 100644 samples/snippets/run_realtime_report_with_multiple_dimensions.py delete mode 100644 samples/snippets/run_realtime_report_with_multiple_dimensions_test.py delete mode 100644 samples/snippets/run_realtime_report_with_multiple_metrics.py delete mode 100644 samples/snippets/run_realtime_report_with_multiple_metrics_test.py delete mode 100644 samples/snippets/run_report.py delete mode 100644 samples/snippets/run_report_test.py delete mode 100644 samples/snippets/run_report_with_aggregations.py delete mode 100644 samples/snippets/run_report_with_aggregations_test.py delete mode 100644 samples/snippets/run_report_with_cohorts.py delete mode 100644 samples/snippets/run_report_with_cohorts_test.py delete mode 100644 samples/snippets/run_report_with_custom_parameters.py delete mode 100644 samples/snippets/run_report_with_date_ranges.py delete mode 100644 samples/snippets/run_report_with_date_ranges_test.py delete mode 100644 samples/snippets/run_report_with_dimension_and_metric_filters.py delete mode 100644 samples/snippets/run_report_with_dimension_and_metric_filters_test.py delete mode 100644 samples/snippets/run_report_with_dimension_exclude_filter.py delete mode 100644 samples/snippets/run_report_with_dimension_exclude_filter_test.py delete mode 100644 samples/snippets/run_report_with_dimension_filter.py delete mode 100644 samples/snippets/run_report_with_dimension_filter_test.py delete mode 100644 samples/snippets/run_report_with_dimension_in_list_filter.py delete mode 100644 samples/snippets/run_report_with_dimension_in_list_filter_test.py delete mode 100644 samples/snippets/run_report_with_multiple_dimension_filters.py delete mode 100644 samples/snippets/run_report_with_multiple_dimension_filters_test.py delete mode 100644 samples/snippets/run_report_with_multiple_dimensions.py delete mode 100644 samples/snippets/run_report_with_multiple_dimensions_test.py delete mode 100644 samples/snippets/run_report_with_multiple_metrics.py delete mode 100644 samples/snippets/run_report_with_multiple_metrics_test.py delete mode 100644 samples/snippets/run_report_with_named_date_ranges.py delete mode 100644 samples/snippets/run_report_with_named_date_ranges_test.py delete mode 100644 samples/snippets/run_report_with_ordering.py delete mode 100644 samples/snippets/run_report_with_ordering_test.py delete mode 100644 samples/snippets/run_report_with_pagination.py delete mode 100644 samples/snippets/run_report_with_pagination_test.py delete mode 100644 samples/snippets/run_report_with_property_quota.py delete mode 100644 samples/snippets/run_report_with_property_quota_test.py diff --git a/samples/snippets/README.md b/samples/snippets/README.md deleted file mode 100644 index f3902244..00000000 --- a/samples/snippets/README.md +++ /dev/null @@ -1,33 +0,0 @@ -# Google Analytics Data API examples - -[![Open in Cloud Shell][shell_img]][shell_link] - -[shell_img]: http://gstatic.com/cloudssh/images/open-btn.png -[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/python-analytics-data&page=editor&working_dir=samples/snippets - -These samples show how to use the -[Google Analytics Data API](https://developers.google.com/analytics/devguides/reporting/data/v1) from Python. - -## Build and Run -1. **Enable APIs** - [Enable the Analytics Data API](https://console.cloud.google.com/flows/enableapi?apiid=analyticsdata.googleapis.com) - and create a new project or select an existing project. -2. **Download The Credentials** - Configure your project using [Application Default Credentials][adc]. - Click "Go to credentials" after enabling the APIs. Click "Create Credentials" - and select "Service Account Credentials" and download the credentials file. Then set the path to - this file to the environment variable `GOOGLE_APPLICATION_CREDENTIALS`: -```sh - $ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json -``` -3. **Clone the repo** and cd into this directory -```sh - $ git clone https://github.com/googleapis/python-analytics-data - $ cd python-analytics-data/samples/snippets -``` -4. **Install dependencies** via [pip3](https://pip.pypa.io/en/stable). - Run `pip3 install --upgrade google-analytics-data`. -5. **Review the comments starting with `TODO(developer)` and update the code -to use correct values. -6. **Run** with the command `python3 SNIPPET_NAME.py`. For example: -```sh - $ python3 quickstart.py -``` diff --git a/samples/snippets/get_common_metadata.py b/samples/snippets/get_common_metadata.py deleted file mode 100644 index 643cf554..00000000 --- a/samples/snippets/get_common_metadata.py +++ /dev/null @@ -1,80 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application retrieving dimension and metrics -metadata. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/getMetadata -for more information. -""" -# [START analyticsdata_get_common_metadata] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import GetMetadataRequest, MetricType - - -def run_sample(): - """Runs the sample.""" - get_common_metadata() - - -def get_common_metadata(): - """Retrieves dimensions and metrics available for all Google Analytics 4 - properties.""" - client = BetaAnalyticsDataClient() - - # Set the Property ID to 0 for dimensions and metrics common - # to all properties. In this special mode, this method will - # not return custom dimensions and metrics. - property_id = 0 - request = GetMetadataRequest(name=f"properties/{property_id}/metadata") - response = client.get_metadata(request) - - print("Dimensions and metrics available for all Google Analytics 4 properties:") - print_get_metadata_response(response) - - -def print_get_metadata_response(response): - """Prints results of the getMetadata call.""" - # [START analyticsdata_print_get_metadata_response] - for dimension in response.dimensions: - print("DIMENSION") - print(f"{dimension.api_name} ({dimension.ui_name}): {dimension.description}") - print(f"custom_definition: {dimension.custom_definition}") - if dimension.deprecated_api_names: - print(f"Deprecated API names: {dimension.deprecated_api_names}") - print("") - - for metric in response.metrics: - print("METRIC") - print(f"{metric.api_name} ({metric.ui_name}): {metric.description}") - print(f"custom_definition: {metric.custom_definition}") - if metric.expression: - print(f"Expression: {metric.expression}") - - metric_type = MetricType(metric.type_).name - print(f"Type: {metric_type}") - - if metric.deprecated_api_names: - print(f"Deprecated API names: {metric.deprecated_api_names}") - print("") - # [END analyticsdata_print_get_metadata_response] - - -# [END analyticsdata_get_common_metadata] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/get_common_metadata_test.py b/samples/snippets/get_common_metadata_test.py deleted file mode 100644 index ae119693..00000000 --- a/samples/snippets/get_common_metadata_test.py +++ /dev/null @@ -1,21 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import get_common_metadata - - -def test_get_common_metadata(capsys): - get_common_metadata.get_common_metadata() - out, _ = capsys.readouterr() - assert "Dimensions and metrics" in out diff --git a/samples/snippets/get_metadata_by_property_id.py b/samples/snippets/get_metadata_by_property_id.py deleted file mode 100644 index 6950ced2..00000000 --- a/samples/snippets/get_metadata_by_property_id.py +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application retrieving dimension and metrics -metadata. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/getMetadata -for more information. -""" -# [START analyticsdata_get_metadata_by_property_id] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import GetMetadataRequest - -from get_common_metadata import print_get_metadata_response - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - get_metadata_by_property_id(property_id) - - -def get_metadata_by_property_id(property_id="YOUR-GA4-PROPERTY-ID"): - """Retrieves dimensions and metrics available for a Google Analytics 4 - property, including custom fields.""" - client = BetaAnalyticsDataClient() - - request = GetMetadataRequest(name=f"properties/{property_id}/metadata") - response = client.get_metadata(request) - - print( - f"Dimensions and metrics available for Google Analytics 4 " - f"property {property_id} (including custom fields):" - ) - print_get_metadata_response(response) - - -# [END analyticsdata_get_metadata_by_property_id] - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/get_metadata_by_property_id_test.py b/samples/snippets/get_metadata_by_property_id_test.py deleted file mode 100644 index 958f4b1a..00000000 --- a/samples/snippets/get_metadata_by_property_id_test.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import get_metadata_by_property_id - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_get_metadata_by_property_id(capsys): - get_metadata_by_property_id.get_metadata_by_property_id(TEST_PROPERTY_ID) - out, _ = capsys.readouterr() - assert "Dimensions and metrics" in out diff --git a/samples/snippets/noxfile.py b/samples/snippets/noxfile.py deleted file mode 100644 index de104dbc..00000000 --- a/samples/snippets/noxfile.py +++ /dev/null @@ -1,292 +0,0 @@ -# Copyright 2019 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from __future__ import print_function - -import glob -import os -from pathlib import Path -import sys -from typing import Callable, Dict, Optional - -import nox - -# WARNING - WARNING - WARNING - WARNING - WARNING -# WARNING - WARNING - WARNING - WARNING - WARNING -# DO NOT EDIT THIS FILE EVER! -# WARNING - WARNING - WARNING - WARNING - WARNING -# WARNING - WARNING - WARNING - WARNING - WARNING - -BLACK_VERSION = "black==22.3.0" -ISORT_VERSION = "isort==5.10.1" - -# Copy `noxfile_config.py` to your directory and modify it instead. - -# `TEST_CONFIG` dict is a configuration hook that allows users to -# modify the test configurations. The values here should be in sync -# with `noxfile_config.py`. Users will copy `noxfile_config.py` into -# their directory and modify it. - -TEST_CONFIG = { - # You can opt out from the test for specific Python versions. - "ignored_versions": [], - # Old samples are opted out of enforcing Python type hints - # All new samples should feature them - "enforce_type_hints": False, - # An envvar key for determining the project id to use. Change it - # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a - # build specific Cloud project. You can also use your own string - # to use your own Cloud project. - "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", - # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', - # If you need to use a specific version of pip, - # change pip_version_override to the string representation - # of the version number, for example, "20.2.4" - "pip_version_override": None, - # A dictionary you want to inject into your test. Don't put any - # secrets here. These values will override predefined values. - "envs": {}, -} - - -try: - # Ensure we can import noxfile_config in the project's directory. - sys.path.append(".") - from noxfile_config import TEST_CONFIG_OVERRIDE -except ImportError as e: - print("No user noxfile_config found: detail: {}".format(e)) - TEST_CONFIG_OVERRIDE = {} - -# Update the TEST_CONFIG with the user supplied values. -TEST_CONFIG.update(TEST_CONFIG_OVERRIDE) - - -def get_pytest_env_vars() -> Dict[str, str]: - """Returns a dict for pytest invocation.""" - ret = {} - - # Override the GCLOUD_PROJECT and the alias. - env_key = TEST_CONFIG["gcloud_project_env"] - # This should error out if not set. - ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key] - - # Apply user supplied envs. - ret.update(TEST_CONFIG["envs"]) - return ret - - -# DO NOT EDIT - automatically generated. -# All versions used to test samples. -ALL_VERSIONS = ["3.7", "3.8", "3.9", "3.10", "3.11"] - -# Any default versions that should be ignored. -IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"] - -TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS]) - -INSTALL_LIBRARY_FROM_SOURCE = os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False) in ( - "True", - "true", -) - -# Error if a python version is missing -nox.options.error_on_missing_interpreters = True - -# -# Style Checks -# - - -# Linting with flake8. -# -# We ignore the following rules: -# E203: whitespace before ‘:’ -# E266: too many leading ‘#’ for block comment -# E501: line too long -# I202: Additional newline in a section of imports -# -# We also need to specify the rules which are ignored by default: -# ['E226', 'W504', 'E126', 'E123', 'W503', 'E24', 'E704', 'E121'] -FLAKE8_COMMON_ARGS = [ - "--show-source", - "--builtin=gettext", - "--max-complexity=20", - "--exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py", - "--ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I202", - "--max-line-length=88", -] - - -@nox.session -def lint(session: nox.sessions.Session) -> None: - if not TEST_CONFIG["enforce_type_hints"]: - session.install("flake8") - else: - session.install("flake8", "flake8-annotations") - - args = FLAKE8_COMMON_ARGS + [ - ".", - ] - session.run("flake8", *args) - - -# -# Black -# - - -@nox.session -def blacken(session: nox.sessions.Session) -> None: - """Run black. Format code to uniform standard.""" - session.install(BLACK_VERSION) - python_files = [path for path in os.listdir(".") if path.endswith(".py")] - - session.run("black", *python_files) - - -# -# format = isort + black -# - - -@nox.session -def format(session: nox.sessions.Session) -> None: - """ - Run isort to sort imports. Then run black - to format code to uniform standard. - """ - session.install(BLACK_VERSION, ISORT_VERSION) - python_files = [path for path in os.listdir(".") if path.endswith(".py")] - - # Use the --fss option to sort imports using strict alphabetical order. - # See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections - session.run("isort", "--fss", *python_files) - session.run("black", *python_files) - - -# -# Sample Tests -# - - -PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"] - - -def _session_tests( - session: nox.sessions.Session, post_install: Callable = None -) -> None: - # check for presence of tests - test_list = glob.glob("**/*_test.py", recursive=True) + glob.glob( - "**/test_*.py", recursive=True - ) - test_list.extend(glob.glob("**/tests", recursive=True)) - - if len(test_list) == 0: - print("No tests found, skipping directory.") - return - - if TEST_CONFIG["pip_version_override"]: - pip_version = TEST_CONFIG["pip_version_override"] - session.install(f"pip=={pip_version}") - """Runs py.test for a particular project.""" - concurrent_args = [] - if os.path.exists("requirements.txt"): - if os.path.exists("constraints.txt"): - session.install("-r", "requirements.txt", "-c", "constraints.txt") - else: - session.install("-r", "requirements.txt") - with open("requirements.txt") as rfile: - packages = rfile.read() - - if os.path.exists("requirements-test.txt"): - if os.path.exists("constraints-test.txt"): - session.install("-r", "requirements-test.txt", "-c", "constraints-test.txt") - else: - session.install("-r", "requirements-test.txt") - with open("requirements-test.txt") as rtfile: - packages += rtfile.read() - - if INSTALL_LIBRARY_FROM_SOURCE: - session.install("-e", _get_repo_root()) - - if post_install: - post_install(session) - - if "pytest-parallel" in packages: - concurrent_args.extend(["--workers", "auto", "--tests-per-worker", "auto"]) - elif "pytest-xdist" in packages: - concurrent_args.extend(["-n", "auto"]) - - session.run( - "pytest", - *(PYTEST_COMMON_ARGS + session.posargs + concurrent_args), - # Pytest will return 5 when no tests are collected. This can happen - # on travis where slow and flaky tests are excluded. - # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html - success_codes=[0, 5], - env=get_pytest_env_vars(), - ) - - -@nox.session(python=ALL_VERSIONS) -def py(session: nox.sessions.Session) -> None: - """Runs py.test for a sample using the specified version of Python.""" - if session.python in TESTED_VERSIONS: - _session_tests(session) - else: - session.skip( - "SKIPPED: {} tests are disabled for this sample.".format(session.python) - ) - - -# -# Readmegen -# - - -def _get_repo_root() -> Optional[str]: - """Returns the root folder of the project.""" - # Get root of this repository. Assume we don't have directories nested deeper than 10 items. - p = Path(os.getcwd()) - for i in range(10): - if p is None: - break - if Path(p / ".git").exists(): - return str(p) - # .git is not available in repos cloned via Cloud Build - # setup.py is always in the library's root, so use that instead - # https://github.com/googleapis/synthtool/issues/792 - if Path(p / "setup.py").exists(): - return str(p) - p = p.parent - raise Exception("Unable to detect repository root.") - - -GENERATED_READMES = sorted([x for x in Path(".").rglob("*.rst.in")]) - - -@nox.session -@nox.parametrize("path", GENERATED_READMES) -def readmegen(session: nox.sessions.Session, path: str) -> None: - """(Re-)generates the readme for a sample.""" - session.install("jinja2", "pyyaml") - dir_ = os.path.dirname(path) - - if os.path.exists(os.path.join(dir_, "requirements.txt")): - session.install("-r", os.path.join(dir_, "requirements.txt")) - - in_file = os.path.join(dir_, "README.rst.in") - session.run( - "python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file - ) diff --git a/samples/snippets/noxfile_config.py b/samples/snippets/noxfile_config.py deleted file mode 100644 index b6dddd6c..00000000 --- a/samples/snippets/noxfile_config.py +++ /dev/null @@ -1,13 +0,0 @@ -TEST_CONFIG_OVERRIDE = { - # You can opt out from the test for specific Python versions. - "ignored_versions": ["2.7"], - # An envvar key for determining the project id to use. Change it - # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a - # build specific Cloud project. You can also use your own string - # to use your own Cloud project. - "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", - # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', - # A dictionary you want to inject into your test. Don't put any - # secrets here. These values will override predefined values. - "envs": {"GA_TEST_PROPERTY_ID": "222596558"}, -} diff --git a/samples/snippets/quickstart.py b/samples/snippets/quickstart.py deleted file mode 100644 index f1e3538b..00000000 --- a/samples/snippets/quickstart.py +++ /dev/null @@ -1,69 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample quickstart application. -This application demonstrates the usage of the Analytics Data API using -service account credentials. -Before you start the application, please review the comments starting with -"TODO(developer)" and update the code to use correct values. -Usage: - pip3 install --upgrade google-analytics-data - python3 quickstart.py -""" -# [START analyticsdata_quickstart] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - DateRange, - Dimension, - Metric, - RunReportRequest, -) - - -def sample_run_report(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a simple report on a Google Analytics 4 property.""" - # TODO(developer): Uncomment this variable and replace with your - # Google Analytics 4 property ID before running the sample. - # property_id = "YOUR-GA4-PROPERTY-ID" - - # [START analyticsdata_run_report_initialize] - # Using a default constructor instructs the client to use the credentials - # specified in GOOGLE_APPLICATION_CREDENTIALS environment variable. - client = BetaAnalyticsDataClient() - # [END analyticsdata_run_report_initialize] - - # [START analyticsdata_run_report] - request = RunReportRequest( - property=f"properties/{property_id}", - dimensions=[Dimension(name="city")], - metrics=[Metric(name="activeUsers")], - date_ranges=[DateRange(start_date="2020-03-31", end_date="today")], - ) - response = client.run_report(request) - # [END analyticsdata_run_report] - - # [START analyticsdata_run_report_response] - print("Report result:") - for row in response.rows: - print(row.dimension_values[0].value, row.metric_values[0].value) - # [END analyticsdata_run_report_response] - - -# [END analyticsdata_quickstart] - - -if __name__ == "__main__": - sample_run_report() diff --git a/samples/snippets/quickstart_json_credentials.py b/samples/snippets/quickstart_json_credentials.py deleted file mode 100644 index fb93cbdb..00000000 --- a/samples/snippets/quickstart_json_credentials.py +++ /dev/null @@ -1,76 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample quickstart application. - -This application demonstrates the usage of the Analytics Data API using -service account credentials from a JSON file downloaded from -the Google Cloud Console. - -Before you start the application, please review the comments starting with -"TODO(developer)" and update the code to use correct values. - -Usage: - pip3 install --upgrade google-analytics-data - python3 quickstart_json_credentials.py -""" -# [START analyticsdata_json_credentials_quickstart] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - DateRange, - Dimension, - Metric, - RunReportRequest, -) - - -def sample_run_report(property_id="YOUR-GA4-PROPERTY-ID", credentials_json_path=""): - """Runs a simple report on a Google Analytics 4 property.""" - # TODO(developer): Uncomment this variable and replace with your - # Google Analytics 4 property ID before running the sample. - # property_id = "YOUR-GA4-PROPERTY-ID" - - # [START analyticsdata_json_credentials_initialize] - # TODO(developer): Uncomment this variable and replace with a valid path to - # the credentials.json file for your service account downloaded from the - # Cloud Console. - # credentials_json_path = "/path/to/credentials.json" - - # Explicitly use service account credentials by specifying - # the private key file. - client = BetaAnalyticsDataClient.from_service_account_json(credentials_json_path) - # [END analyticsdata_json_credentials_initialize] - - # [START analyticsdata_json_credentials_run_report] - request = RunReportRequest( - property=f"properties/{property_id}", - dimensions=[Dimension(name="city")], - metrics=[Metric(name="activeUsers")], - date_ranges=[DateRange(start_date="2020-03-31", end_date="today")], - ) - response = client.run_report(request) - # [END analyticsdata_json_credentials_run_report] - - print("Report result:") - for row in response.rows: - print(row.dimension_values[0].value, row.metric_values[0].value) - - -# [END analyticsdata_json_credentials_quickstart] - - -if __name__ == "__main__": - sample_run_report() diff --git a/samples/snippets/quickstart_json_credentials_test.py b/samples/snippets/quickstart_json_credentials_test.py deleted file mode 100644 index 06d7d1e4..00000000 --- a/samples/snippets/quickstart_json_credentials_test.py +++ /dev/null @@ -1,29 +0,0 @@ -# Copyright 2020 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import quickstart_json_credentials - - -def test_quickstart(capsys): - # Create a temporary service account credentials JSON file to be used by - # the test. - TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - CREDENTIALS_JSON_PATH = os.getenv("GOOGLE_APPLICATION_CREDENTIALS") - quickstart_json_credentials.sample_run_report( - TEST_PROPERTY_ID, CREDENTIALS_JSON_PATH - ) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/quickstart_oauth2.py b/samples/snippets/quickstart_oauth2.py deleted file mode 100644 index e8282cca..00000000 --- a/samples/snippets/quickstart_oauth2.py +++ /dev/null @@ -1,89 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample quickstart application. -This application demonstrates the usage of the Analytics Data API using -OAuth2 credentials. -Before you start the application, please review the comments starting with -"TODO(developer)" and update the code to use correct values. -Usage: - pip3 install --upgrade google-auth-oauthlib - pip3 install --upgrade google-analytics-data - python3 quickstart_oauth2.py -""" -# [START analyticsdata_oauth2_quickstart] -from google.analytics.data import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - DateRange, - Dimension, - Metric, - RunReportRequest, -) -from google_auth_oauthlib import flow - - -def sample_run_report(credentials=None, property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a simple report on a Google Analytics 4 property.""" - # TODO(developer): Uncomment this variable and replace with your - # Google Analytics 4 property ID before running the sample. - # property_id = "YOUR-GA4-PROPERTY-ID" - - client = BetaAnalyticsDataClient(credentials=credentials) - request = RunReportRequest( - property=f"properties/{property_id}", - dimensions=[Dimension(name="city")], - metrics=[Metric(name="activeUsers")], - date_ranges=[DateRange(start_date="2020-03-31", end_date="today")], - ) - - response = client.run_report(request) - - print("Report result:") - for row in response.rows: - print(row.dimension_values[0].value, row.metric_values[0].value) - - -def get_credentials(): - """Creates an OAuth2 credentials instance.""" - # [START analyticsdata_oauth2_initialize] - appflow = flow.InstalledAppFlow.from_client_secrets_file( - "client_secrets.json", - scopes=["https://www.googleapis.com/auth/analytics.readonly"], - ) - # TODO(developer): Update the line below to set the `launch_browser` variable. - # The `launch_browser` boolean variable indicates if a local server is used - # as the callback URL in the auth flow. A value of `True` is recommended, - # but a local server does not work if accessing the application remotely, - # such as over SSH or from a remote Jupyter notebook. - launch_browser = True - if launch_browser: - appflow.run_local_server() - else: - appflow.run_console() - return appflow.credentials - # [END analyticsdata_oauth2_initialize] - - -def main(): - credentials = get_credentials() - sample_run_report(credentials) - - -# [END analyticsdata_oauth2_quickstart] - - -if __name__ == "__main__": - main() diff --git a/samples/snippets/quickstart_oauth2_test.py b/samples/snippets/quickstart_oauth2_test.py deleted file mode 100644 index dcbdc199..00000000 --- a/samples/snippets/quickstart_oauth2_test.py +++ /dev/null @@ -1,24 +0,0 @@ -# Copyright 2020 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import quickstart_oauth2 - - -def test_quickstart(capsys): - TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - quickstart_oauth2.sample_run_report(None, TEST_PROPERTY_ID) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/quickstart_test.py b/samples/snippets/quickstart_test.py deleted file mode 100644 index 14c639d2..00000000 --- a/samples/snippets/quickstart_test.py +++ /dev/null @@ -1,24 +0,0 @@ -# Copyright 2020 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import quickstart - - -def test_quickstart(capsys): - TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - quickstart.sample_run_report(TEST_PROPERTY_ID) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/requirements-test.txt b/samples/snippets/requirements-test.txt deleted file mode 100644 index 2a929edc..00000000 --- a/samples/snippets/requirements-test.txt +++ /dev/null @@ -1 +0,0 @@ -pytest==7.4.2 diff --git a/samples/snippets/requirements.txt b/samples/snippets/requirements.txt deleted file mode 100644 index ecbbd503..00000000 --- a/samples/snippets/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -google-analytics-data==0.17.2 -google-auth-oauthlib==1.1.0 \ No newline at end of file diff --git a/samples/snippets/run_batch_report.py b/samples/snippets/run_batch_report.py deleted file mode 100644 index 07cdf40f..00000000 --- a/samples/snippets/run_batch_report.py +++ /dev/null @@ -1,78 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the batch creation -of multiple reports. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/batchRunReports -for more information. -""" -# [START analyticsdata_run_batch_report] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - BatchRunReportsRequest, - DateRange, - Dimension, - Metric, - RunReportRequest, -) - -from run_report import print_run_report_response - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_batch_report(property_id) - - -def run_batch_report(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a batch report on a Google Analytics 4 property.""" - client = BetaAnalyticsDataClient() - - request = BatchRunReportsRequest( - property=f"properties/{property_id}", - requests=[ - RunReportRequest( - dimensions=[ - Dimension(name="country"), - Dimension(name="region"), - Dimension(name="city"), - ], - metrics=[Metric(name="activeUsers")], - date_ranges=[DateRange(start_date="2021-01-03", end_date="2021-01-09")], - ), - RunReportRequest( - dimensions=[Dimension(name="browser")], - metrics=[Metric(name="activeUsers")], - date_ranges=[DateRange(start_date="2021-01-01", end_date="2021-01-31")], - ), - ], - ) - response = client.batch_run_reports(request) - - print("Batch report results:") - for report in response.reports: - print_run_report_response(report) - - -# [END analyticsdata_run_batch_report] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_batch_report_test.py b/samples/snippets/run_batch_report_test.py deleted file mode 100644 index 880c10bb..00000000 --- a/samples/snippets/run_batch_report_test.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_batch_report - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_run_batch_report(capsys): - run_batch_report.run_batch_report(TEST_PROPERTY_ID) - out, _ = capsys.readouterr() - assert "Batch report result" in out diff --git a/samples/snippets/run_funnel_report.py b/samples/snippets/run_funnel_report.py deleted file mode 100644 index 937e3e88..00000000 --- a/samples/snippets/run_funnel_report.py +++ /dev/null @@ -1,207 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2022 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the creation of -a funnel report. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1alpha/properties/runFunnelReport -for more information. -""" -# [START analyticsdata_run_funnel_report] -from google.analytics.data_v1alpha import AlphaAnalyticsDataClient -from google.analytics.data_v1alpha.types import ( - DateRange, - Dimension, - Funnel, - FunnelBreakdown, - FunnelEventFilter, - FunnelFieldFilter, - FunnelFilterExpression, - FunnelFilterExpressionList, - FunnelStep, - RunFunnelReportRequest, - StringFilter, -) - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_funnel_report(property_id) - - -def run_funnel_report(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a funnel query to build a report with 5 funnel steps. - Step 1: First open/visit (event name is `first_open` or `first_visit`). - Step 2: Organic visitors (`firstUserMedium` dimension contains the term - "organic"). - Step 3: Session start (event name is `session_start`). - Step 4: Screen/Page view (event name is `screen_view` or `page_view`). - Step 5: Purchase (event name is `purchase` or `in_app_purchase`). - - The report configuration reproduces the default funnel report provided in - the Funnel Exploration template of the Google Analytics UI. - See more at https://support.google.com/analytics/answer/9327974 - """ - client = AlphaAnalyticsDataClient() - - request = RunFunnelReportRequest( - property=f"properties/{property_id}", - date_ranges=[DateRange(start_date="30daysAgo", end_date="today")], - funnel_breakdown=FunnelBreakdown( - breakdown_dimension=Dimension(name="deviceCategory") - ), - funnel=Funnel( - steps=[ - FunnelStep( - name="First open/visit", - filter_expression=FunnelFilterExpression( - or_group=FunnelFilterExpressionList( - expressions=[ - FunnelFilterExpression( - funnel_event_filter=FunnelEventFilter( - event_name="first_open" - ) - ), - FunnelFilterExpression( - funnel_event_filter=FunnelEventFilter( - event_name="first_visit" - ) - ), - ] - ) - ), - ), - FunnelStep( - name="Organic visitors", - filter_expression=FunnelFilterExpression( - funnel_field_filter=FunnelFieldFilter( - field_name="firstUserMedium", - string_filter=StringFilter( - match_type=StringFilter.MatchType.CONTAINS, - case_sensitive=False, - value="organic", - ), - ) - ), - ), - FunnelStep( - name="Session start", - filter_expression=FunnelFilterExpression( - funnel_event_filter=FunnelEventFilter( - event_name="session_start" - ) - ), - ), - FunnelStep( - name="Screen/Page view", - filter_expression=FunnelFilterExpression( - or_group=FunnelFilterExpressionList( - expressions=[ - FunnelFilterExpression( - funnel_event_filter=FunnelEventFilter( - event_name="screen_view" - ) - ), - FunnelFilterExpression( - funnel_event_filter=FunnelEventFilter( - event_name="page_view" - ) - ), - ] - ) - ), - ), - FunnelStep( - name="Purchase", - filter_expression=FunnelFilterExpression( - or_group=FunnelFilterExpressionList( - expressions=[ - FunnelFilterExpression( - funnel_event_filter=FunnelEventFilter( - event_name="purchase" - ) - ), - FunnelFilterExpression( - funnel_event_filter=FunnelEventFilter( - event_name="in_app_purchase" - ) - ), - ] - ) - ), - ), - ] - ), - ) - response = client.run_funnel_report(request) - print_run_funnel_report_response(response) - - -# [START analyticsdata_print_run_funnel_report_response] -def print_funnel_sub_report(funnel_sub_report): - """Prints the contents of a FunnelSubReport object.""" - print("Dimension headers:") - for dimension_header in funnel_sub_report.dimension_headers: - print(dimension_header.name) - - print("\nMetric headers:") - for metric_header in funnel_sub_report.metric_headers: - print(metric_header.name) - - print("\nDimensions and metric values for each row in the report:") - for row_idx, row in enumerate(funnel_sub_report.rows): - print("\nRow #{}".format(row_idx)) - for field_idx, dimension_value in enumerate(row.dimension_values): - dimension_name = funnel_sub_report.dimension_headers[field_idx].name - print("{}: '{}'".format(dimension_name, dimension_value.value)) - - for field_idx, metric_value in enumerate(row.metric_values): - metric_name = funnel_sub_report.metric_headers[field_idx].name - print("{}: '{}'".format(metric_name, metric_value.value)) - - print("\nSampling metadata for each date range:") - for metadata_idx, metadata in enumerate( - funnel_sub_report.metadata.sampling_metadatas - ): - print( - "Sampling metadata for date range #{}: samplesReadCount={}, " - "samplingSpaceSize={}".format( - metadata_idx, metadata.samples_read_count, metadata.sampling_space_size - ) - ) - - -def print_run_funnel_report_response(response): - """Prints results of a runFunnelReport call.""" - print("Report result:") - print("=== FUNNEL VISUALIZATION ===") - print_funnel_sub_report(response.funnel_visualization) - - print("=== FUNNEL TABLE ===") - print_funnel_sub_report(response.funnel_table) - - -# [END analyticsdata_print_run_funnel_report_response] - - -# [END analyticsdata_run_funnel_report] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_funnel_report_test.py b/samples/snippets/run_funnel_report_test.py deleted file mode 100644 index e8b2aaa3..00000000 --- a/samples/snippets/run_funnel_report_test.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_funnel_report - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_run_funnel_report(capsys): - run_funnel_report.run_funnel_report(TEST_PROPERTY_ID) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/run_pivot_report.py b/samples/snippets/run_pivot_report.py deleted file mode 100644 index 699780ce..00000000 --- a/samples/snippets/run_pivot_report.py +++ /dev/null @@ -1,96 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the creation of -a pivot report. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runPivotReport -for more information. -""" -# [START analyticsdata_run_pivot_report] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - DateRange, - Dimension, - Metric, - OrderBy, - Pivot, - RunPivotReportRequest, -) - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_pivot_report(property_id) - - -def run_pivot_report(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a pivot query to build a report of session counts by country, - pivoted by the browser dimension.""" - client = BetaAnalyticsDataClient() - - request = RunPivotReportRequest( - property=f"properties/{property_id}", - date_ranges=[DateRange(start_date="2021-01-01", end_date="2021-01-30")], - pivots=[ - Pivot( - field_names=["country"], - limit=250, - order_bys=[ - OrderBy( - dimension=OrderBy.DimensionOrderBy(dimension_name="country") - ) - ], - ), - Pivot( - field_names=["browser"], - offset=3, - limit=3, - order_bys=[ - OrderBy( - metric=OrderBy.MetricOrderBy(metric_name="sessions"), desc=True - ) - ], - ), - ], - metrics=[Metric(name="sessions")], - dimensions=[Dimension(name="country"), Dimension(name="browser")], - ) - response = client.run_pivot_report(request) - print_run_pivot_report_response(response) - - -def print_run_pivot_report_response(response): - """Prints results of a runPivotReport call.""" - # [START analyticsdata_print_run_pivot_report_response] - print("Report result:") - for row in response.rows: - for dimension_value in row.dimension_values: - print(dimension_value.value) - - for metric_value in row.metric_values: - print(metric_value.value) - # [END analyticsdata_print_run_pivot_report_response] - - -# [END analyticsdata_run_pivot_report] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_pivot_report_test.py b/samples/snippets/run_pivot_report_test.py deleted file mode 100644 index da010a1c..00000000 --- a/samples/snippets/run_pivot_report_test.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_pivot_report - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_run_pivot_report(capsys): - run_pivot_report.run_pivot_report(TEST_PROPERTY_ID) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/run_realtime_report.py b/samples/snippets/run_realtime_report.py deleted file mode 100644 index 5efd627c..00000000 --- a/samples/snippets/run_realtime_report.py +++ /dev/null @@ -1,59 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the creation of -a realtime report. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runRealtimeReport -for more information. -""" -# [START analyticsdata_run_realtime_report] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - Dimension, - Metric, - RunRealtimeReportRequest, -) - -from run_report import print_run_report_response - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_realtime_report(property_id) - - -def run_realtime_report(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a realtime report on a Google Analytics 4 property.""" - client = BetaAnalyticsDataClient() - - request = RunRealtimeReportRequest( - property=f"properties/{property_id}", - dimensions=[Dimension(name="country")], - metrics=[Metric(name="activeUsers")], - ) - response = client.run_realtime_report(request) - print_run_report_response(response) - - -# [END analyticsdata_run_realtime_report] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_realtime_report_test.py b/samples/snippets/run_realtime_report_test.py deleted file mode 100644 index b0a643e7..00000000 --- a/samples/snippets/run_realtime_report_test.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_realtime_report - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_run_realtime_report(capsys): - run_realtime_report.run_realtime_report(TEST_PROPERTY_ID) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/run_realtime_report_with_minute_ranges.py b/samples/snippets/run_realtime_report_with_minute_ranges.py deleted file mode 100644 index 2db37728..00000000 --- a/samples/snippets/run_realtime_report_with_minute_ranges.py +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2022 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the creation of -a realtime report using minute ranges. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runRealtimeReport#body.request_body.FIELDS.minute_ranges -for more information. -""" -# [START analyticsdata_run_realtime_report_with_minute_ranges] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - Metric, - MinuteRange, - RunRealtimeReportRequest, -) - -from run_report import print_run_report_response - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_realtime_report_with_minute_ranges(property_id) - - -def run_realtime_report_with_minute_ranges(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a realtime report on a Google Analytics 4 property. Dimensions - field is omitted in the query, which results in total values of active users - returned for each minute range in the report. - - Note the `dateRange` dimension added to the report response automatically - as a result of querying multiple minute ranges. - """ - client = BetaAnalyticsDataClient() - - request = RunRealtimeReportRequest( - property=f"properties/{property_id}", - metrics=[Metric(name="activeUsers")], - minute_ranges=[ - MinuteRange(name="0-4 minutes ago", start_minutes_ago=4), - MinuteRange( - name="25-29 minutes ago", start_minutes_ago=29, end_minutes_ago=25 - ), - ], - ) - response = client.run_realtime_report(request) - print_run_report_response(response) - - -# [END analyticsdata_run_realtime_report_with_minute_ranges] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_realtime_report_with_minute_ranges_test.py b/samples/snippets/run_realtime_report_with_minute_ranges_test.py deleted file mode 100644 index f4288205..00000000 --- a/samples/snippets/run_realtime_report_with_minute_ranges_test.py +++ /dev/null @@ -1,27 +0,0 @@ -# Copyright 2022 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_realtime_report_with_minute_ranges - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_run_report_with_multiple_metrics(capsys): - run_realtime_report_with_minute_ranges.run_realtime_report_with_minute_ranges( - TEST_PROPERTY_ID - ) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/run_realtime_report_with_multiple_dimensions.py b/samples/snippets/run_realtime_report_with_multiple_dimensions.py deleted file mode 100644 index e4c1fe02..00000000 --- a/samples/snippets/run_realtime_report_with_multiple_dimensions.py +++ /dev/null @@ -1,59 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the creation of -a realtime report. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport -for more information. -""" -# [START analyticsdata_run_realtime_report_with_multiple_dimensions] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - Dimension, - Metric, - RunRealtimeReportRequest, -) - -from run_report import print_run_report_response - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_realtime_report_with_multiple_dimensions(property_id) - - -def run_realtime_report_with_multiple_dimensions(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a realtime report on a Google Analytics 4 property.""" - client = BetaAnalyticsDataClient() - - request = RunRealtimeReportRequest( - property=f"properties/{property_id}", - dimensions=[Dimension(name="country"), Dimension(name="city")], - metrics=[Metric(name="activeUsers")], - ) - response = client.run_realtime_report(request) - print_run_report_response(response) - - -# [END analyticsdata_run_realtime_report_with_multiple_dimensions] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_realtime_report_with_multiple_dimensions_test.py b/samples/snippets/run_realtime_report_with_multiple_dimensions_test.py deleted file mode 100644 index 2f0f0575..00000000 --- a/samples/snippets/run_realtime_report_with_multiple_dimensions_test.py +++ /dev/null @@ -1,27 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_report_with_multiple_dimensions - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_run_report_with_multiple_dimensions(capsys): - run_report_with_multiple_dimensions.run_report_with_multiple_dimensions( - TEST_PROPERTY_ID - ) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/run_realtime_report_with_multiple_metrics.py b/samples/snippets/run_realtime_report_with_multiple_metrics.py deleted file mode 100644 index 831b32bd..00000000 --- a/samples/snippets/run_realtime_report_with_multiple_metrics.py +++ /dev/null @@ -1,59 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the creation of -a realtime report. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport -for more information. -""" -# [START analyticsdata_run_realtime_report_with_multiple_metrics] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - Dimension, - Metric, - RunRealtimeReportRequest, -) - -from run_report import print_run_report_response - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_realtime_report_with_multiple_metrics(property_id) - - -def run_realtime_report_with_multiple_metrics(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a realtime report on a Google Analytics 4 property.""" - client = BetaAnalyticsDataClient() - - request = RunRealtimeReportRequest( - property=f"properties/{property_id}", - dimensions=[Dimension(name="unifiedScreenName")], - metrics=[Metric(name="screenPageViews"), Metric(name="conversions")], - ) - response = client.run_realtime_report(request) - print_run_report_response(response) - - -# [END analyticsdata_run_realtime_report_with_multiple_metrics] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_realtime_report_with_multiple_metrics_test.py b/samples/snippets/run_realtime_report_with_multiple_metrics_test.py deleted file mode 100644 index 92ab1eb5..00000000 --- a/samples/snippets/run_realtime_report_with_multiple_metrics_test.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_report_with_multiple_metrics - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_run_report_with_multiple_metrics(capsys): - run_report_with_multiple_metrics.run_report_with_multiple_metrics(TEST_PROPERTY_ID) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/run_report.py b/samples/snippets/run_report.py deleted file mode 100644 index d3c19ca5..00000000 --- a/samples/snippets/run_report.py +++ /dev/null @@ -1,85 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the creation -of a basic report. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport -for more information. -""" -# [START analyticsdata_run_report] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - DateRange, - Dimension, - Metric, - MetricType, - RunReportRequest, -) - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_report(property_id) - - -def run_report(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a report of active users grouped by country.""" - client = BetaAnalyticsDataClient() - - request = RunReportRequest( - property=f"properties/{property_id}", - dimensions=[Dimension(name="country")], - metrics=[Metric(name="activeUsers")], - date_ranges=[DateRange(start_date="2020-09-01", end_date="2020-09-15")], - ) - response = client.run_report(request) - print_run_report_response(response) - - -def print_run_report_response(response): - """Prints results of a runReport call.""" - # [START analyticsdata_print_run_report_response_header] - print(f"{response.row_count} rows received") - for dimensionHeader in response.dimension_headers: - print(f"Dimension header name: {dimensionHeader.name}") - for metricHeader in response.metric_headers: - metric_type = MetricType(metricHeader.type_).name - print(f"Metric header name: {metricHeader.name} ({metric_type})") - # [END analyticsdata_print_run_report_response_header] - - # [START analyticsdata_print_run_report_response_rows] - print("Report result:") - for rowIdx, row in enumerate(response.rows): - print(f"\nRow {rowIdx}") - for i, dimension_value in enumerate(row.dimension_values): - dimension_name = response.dimension_headers[i].name - print(f"{dimension_name}: {dimension_value.value}") - - for i, metric_value in enumerate(row.metric_values): - metric_name = response.metric_headers[i].name - print(f"{metric_name}: {metric_value.value}") - # [END analyticsdata_print_run_report_response_rows] - - -# [END analyticsdata_run_report] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_report_test.py b/samples/snippets/run_report_test.py deleted file mode 100644 index d6e6f2f3..00000000 --- a/samples/snippets/run_report_test.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_report - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_run_report_basic(capsys): - run_report.run_report(TEST_PROPERTY_ID) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/run_report_with_aggregations.py b/samples/snippets/run_report_with_aggregations.py deleted file mode 100644 index 9cee3256..00000000 --- a/samples/snippets/run_report_with_aggregations.py +++ /dev/null @@ -1,68 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the usage of -metric aggregations in a report. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport#body.request_body.FIELDS.metric_aggregations -for more information. -""" -# [START analyticsdata_run_report_with_aggregations] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - DateRange, - Dimension, - Metric, - MetricAggregation, - RunReportRequest, -) - -from run_report import print_run_report_response - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_report_with_aggregations(property_id) - - -def run_report_with_aggregations(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a report which includes total, maximum and minimum values for - each metric.""" - client = BetaAnalyticsDataClient() - - request = RunReportRequest( - property=f"properties/{property_id}", - dimensions=[Dimension(name="country")], - metrics=[Metric(name="sessions")], - date_ranges=[DateRange(start_date="365daysAgo", end_date="today")], - metric_aggregations=[ - MetricAggregation.TOTAL, - MetricAggregation.MAXIMUM, - MetricAggregation.MINIMUM, - ], - ) - response = client.run_report(request) - print_run_report_response(response) - - -# [END analyticsdata_run_report_with_aggregations] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_report_with_aggregations_test.py b/samples/snippets/run_report_with_aggregations_test.py deleted file mode 100644 index 420c412d..00000000 --- a/samples/snippets/run_report_with_aggregations_test.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_report_with_aggregations - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_run_report_with_aggregations(capsys): - run_report_with_aggregations.run_report_with_aggregations(TEST_PROPERTY_ID) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/run_report_with_cohorts.py b/samples/snippets/run_report_with_cohorts.py deleted file mode 100644 index 38442a12..00000000 --- a/samples/snippets/run_report_with_cohorts.py +++ /dev/null @@ -1,87 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the usage of -cohort specification in a report. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport#body.request_body.FIELDS.cohort_spec -for more information. -""" -# [START analyticsdata_run_report_with_cohorts] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - Cohort, - CohortSpec, - CohortsRange, - DateRange, - Dimension, - Metric, - RunReportRequest, -) - -from run_report import print_run_report_response - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_report_with_cohorts(property_id) - - -def run_report_with_cohorts(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a report on a cohort of users whose first session happened on the - same week. The number of active users and user retention rate is calculated - for the cohort using WEEKLY granularity.""" - client = BetaAnalyticsDataClient() - - request = RunReportRequest( - property=f"properties/{property_id}", - dimensions=[Dimension(name="cohort"), Dimension(name="cohortNthWeek")], - metrics=[ - Metric(name="cohortActiveUsers"), - Metric( - name="cohortRetentionRate", - expression="cohortActiveUsers/cohortTotalUsers", - ), - ], - cohort_spec=CohortSpec( - cohorts=[ - Cohort( - dimension="firstSessionDate", - name="cohort", - date_range=DateRange( - start_date="2021-01-03", end_date="2021-01-09" - ), - ) - ], - cohorts_range=CohortsRange( - start_offset=0, - end_offset=4, - granularity=CohortsRange.Granularity.WEEKLY, - ), - ), - ) - response = client.run_report(request) - print_run_report_response(response) - - -# [END analyticsdata_run_report_with_cohorts] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_report_with_cohorts_test.py b/samples/snippets/run_report_with_cohorts_test.py deleted file mode 100644 index 06907e7d..00000000 --- a/samples/snippets/run_report_with_cohorts_test.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_report_with_cohorts - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_run_report_with_cohorts(capsys): - run_report_with_cohorts.run_report_with_cohorts(TEST_PROPERTY_ID) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/run_report_with_custom_parameters.py b/samples/snippets/run_report_with_custom_parameters.py deleted file mode 100644 index e0a31911..00000000 --- a/samples/snippets/run_report_with_custom_parameters.py +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application. -""" -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import RunReportRequest - - -def run_report_with_custom_parameters(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a simple report on a Google Analytics 4 property.""" - client = BetaAnalyticsDataClient() - - # [START run_report_with_custom_parameters] - request = RunReportRequest() - response = client.run_report(request) - # [END run_report_with_custom_parameters] - - print("Report result:") - for row in response.rows: - print(row.dimension_values[0].value, row.metric_values[0].value) - - -if __name__ == "__main__": - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_report_with_custom_parameters(property_id) diff --git a/samples/snippets/run_report_with_date_ranges.py b/samples/snippets/run_report_with_date_ranges.py deleted file mode 100644 index b228425b..00000000 --- a/samples/snippets/run_report_with_date_ranges.py +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the usage of -date ranges in a report. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport#body.request_body.FIELDS.date_ranges -for more information. -""" -# [START analyticsdata_run_report_with_date_ranges] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - DateRange, - Dimension, - Metric, - RunReportRequest, -) - -from run_report import print_run_report_response - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_report_with_date_ranges(property_id) - - -def run_report_with_date_ranges(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a report using two date ranges.""" - client = BetaAnalyticsDataClient() - - request = RunReportRequest( - property=f"properties/{property_id}", - date_ranges=[ - DateRange(start_date="2019-08-01", end_date="2019-08-14"), - DateRange(start_date="2020-08-01", end_date="2020-08-14"), - ], - dimensions=[Dimension(name="platform")], - metrics=[Metric(name="activeUsers")], - ) - response = client.run_report(request) - print_run_report_response(response) - - -# [END analyticsdata_run_report_with_date_ranges] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_report_with_date_ranges_test.py b/samples/snippets/run_report_with_date_ranges_test.py deleted file mode 100644 index 552f9693..00000000 --- a/samples/snippets/run_report_with_date_ranges_test.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_report_with_date_ranges - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_run_report_with_date_ranges(capsys): - run_report_with_date_ranges.run_report_with_date_ranges(TEST_PROPERTY_ID) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/run_report_with_dimension_and_metric_filters.py b/samples/snippets/run_report_with_dimension_and_metric_filters.py deleted file mode 100644 index cc013a5c..00000000 --- a/samples/snippets/run_report_with_dimension_and_metric_filters.py +++ /dev/null @@ -1,101 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the usage of -dimension and metric filters in a report. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport#body.request_body.FIELDS.dimension_filter -for more information. -""" -# [START analyticsdata_run_report_with_dimension_and_metric_filters] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - DateRange, - Dimension, - Filter, - FilterExpression, - FilterExpressionList, - Metric, - NumericValue, - RunReportRequest, -) - -from run_report import print_run_report_response - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_report_with_dimension_and_metric_filters(property_id) - - -def run_report_with_dimension_and_metric_filters(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a report using both metric and dimension filters. A dimension filter - limits the report to include only users who made an in-app purchase using - Android platform. A metric filter specifies that only users with session - counts larger than 1,000 should be included.""" - client = BetaAnalyticsDataClient() - - request = RunReportRequest( - property=f"properties/{property_id}", - dimensions=[Dimension(name="city")], - metrics=[Metric(name="activeUsers")], - date_ranges=[DateRange(start_date="2020-03-31", end_date="today")], - metric_filter=FilterExpression( - filter=Filter( - field_name="sessions", - numeric_filter=Filter.NumericFilter( - operation=Filter.NumericFilter.Operation.GREATER_THAN, - value=NumericValue(int64_value=1000), - ), - ) - ), - dimension_filter=FilterExpression( - and_group=FilterExpressionList( - expressions=[ - FilterExpression( - filter=Filter( - field_name="platform", - string_filter=Filter.StringFilter( - match_type=Filter.StringFilter.MatchType.EXACT, - value="Android", - ), - ) - ), - FilterExpression( - filter=Filter( - field_name="eventName", - string_filter=Filter.StringFilter( - match_type=Filter.StringFilter.MatchType.EXACT, - value="in_app_purchase", - ), - ) - ), - ] - ) - ), - ) - response = client.run_report(request) - print_run_report_response(response) - - -# [END analyticsdata_run_report_with_dimension_and_metric_filters] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_report_with_dimension_and_metric_filters_test.py b/samples/snippets/run_report_with_dimension_and_metric_filters_test.py deleted file mode 100644 index 54995749..00000000 --- a/samples/snippets/run_report_with_dimension_and_metric_filters_test.py +++ /dev/null @@ -1,27 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_report_with_dimension_and_metric_filters - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_run_report_with_dimension_and_metric_filters(capsys): - run_report_with_dimension_and_metric_filters.run_report_with_dimension_and_metric_filters( - TEST_PROPERTY_ID - ) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/run_report_with_dimension_exclude_filter.py b/samples/snippets/run_report_with_dimension_exclude_filter.py deleted file mode 100644 index 532e17fe..00000000 --- a/samples/snippets/run_report_with_dimension_exclude_filter.py +++ /dev/null @@ -1,76 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the usage of -dimension and metric filters in a report. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport#body.request_body.FIELDS.dimension_filter -for more information. -""" -# [START analyticsdata_run_report_with_dimension_exclude_filter] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - DateRange, - Dimension, - Filter, - FilterExpression, - Metric, - RunReportRequest, -) - -from run_report import print_run_report_response - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_report_with_dimension_exclude_filter(property_id) - - -def run_report_with_dimension_exclude_filter(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a report using a filter with `not_expression`. The dimension filter - selects for when `pageTitle` is not `My Homepage`. - - This sample uses relative date range values. See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/DateRange - for more information. - """ - client = BetaAnalyticsDataClient() - - request = RunReportRequest( - property=f"properties/{property_id}", - dimensions=[Dimension(name="pageTitle")], - metrics=[Metric(name="sessions")], - date_ranges=[DateRange(start_date="7daysAgo", end_date="yesterday")], - dimension_filter=FilterExpression( - not_expression=FilterExpression( - filter=Filter( - field_name="pageTitle", - string_filter=Filter.StringFilter(value="My Homepage"), - ) - ) - ), - ) - response = client.run_report(request) - print_run_report_response(response) - - -# [END analyticsdata_run_report_with_dimension_exclude_filter] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_report_with_dimension_exclude_filter_test.py b/samples/snippets/run_report_with_dimension_exclude_filter_test.py deleted file mode 100644 index 3cef3fba..00000000 --- a/samples/snippets/run_report_with_dimension_exclude_filter_test.py +++ /dev/null @@ -1,27 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_report_with_dimension_exclude_filter - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_run_report_with_dimension_exclude_filter(capsys): - run_report_with_dimension_exclude_filter.run_report_with_dimension_exclude_filter( - TEST_PROPERTY_ID - ) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/run_report_with_dimension_filter.py b/samples/snippets/run_report_with_dimension_filter.py deleted file mode 100644 index 64e899f9..00000000 --- a/samples/snippets/run_report_with_dimension_filter.py +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the usage of -dimension and metric filters in a report. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport#body.request_body.FIELDS.dimension_filter -for more information. -""" -# [START analyticsdata_run_report_with_dimension_filter] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - DateRange, - Dimension, - Filter, - FilterExpression, - Metric, - RunReportRequest, -) - -from run_report import print_run_report_response - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_report_with_dimension_filter(property_id) - - -def run_report_with_dimension_filter(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a report using a dimension filter. The call returns a time series - report of `eventCount` when `eventName` is `first_open` for each date. - - This sample uses relative date range values. See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/DateRange - for more information. - """ - - client = BetaAnalyticsDataClient() - - request = RunReportRequest( - property=f"properties/{property_id}", - dimensions=[Dimension(name="date")], - metrics=[Metric(name="eventCount")], - date_ranges=[DateRange(start_date="7daysAgo", end_date="yesterday")], - dimension_filter=FilterExpression( - filter=Filter( - field_name="eventName", - string_filter=Filter.StringFilter(value="first_open"), - ) - ), - ) - response = client.run_report(request) - print_run_report_response(response) - - -# [END analyticsdata_run_report_with_dimension_filter] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_report_with_dimension_filter_test.py b/samples/snippets/run_report_with_dimension_filter_test.py deleted file mode 100644 index b4e5791a..00000000 --- a/samples/snippets/run_report_with_dimension_filter_test.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_report_with_dimension_filter - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_run_report_with_dimension_filter(capsys): - run_report_with_dimension_filter.run_report_with_dimension_filter(TEST_PROPERTY_ID) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/run_report_with_dimension_in_list_filter.py b/samples/snippets/run_report_with_dimension_in_list_filter.py deleted file mode 100644 index 14c5b49d..00000000 --- a/samples/snippets/run_report_with_dimension_in_list_filter.py +++ /dev/null @@ -1,81 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the usage of -dimension and metric filters in a report. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport#body.request_body.FIELDS.dimension_filter -for more information. -""" -# [START analyticsdata_run_report_with_dimension_in_list_filter] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - DateRange, - Dimension, - Filter, - FilterExpression, - Metric, - RunReportRequest, -) - -from run_report import print_run_report_response - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_report_with_dimension_in_list_filter(property_id) - - -def run_report_with_dimension_in_list_filter(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a report using a dimension filter with `in_list_filter` expression. - The filter selects for when `eventName` is set to one of three event names - specified in the query. - - This sample uses relative date range values. See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/DateRange - for more information. - """ - client = BetaAnalyticsDataClient() - - request = RunReportRequest( - property=f"properties/{property_id}", - dimensions=[Dimension(name="eventName")], - metrics=[Metric(name="sessions")], - date_ranges=[DateRange(start_date="7daysAgo", end_date="yesterday")], - dimension_filter=FilterExpression( - filter=Filter( - field_name="eventName", - in_list_filter=Filter.InListFilter( - values=[ - "purchase", - "in_app_purchase", - "app_store_subscription_renew", - ] - ), - ) - ), - ) - response = client.run_report(request) - print_run_report_response(response) - - -# [END analyticsdata_run_report_with_dimension_in_list_filter] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_report_with_dimension_in_list_filter_test.py b/samples/snippets/run_report_with_dimension_in_list_filter_test.py deleted file mode 100644 index 60f34a52..00000000 --- a/samples/snippets/run_report_with_dimension_in_list_filter_test.py +++ /dev/null @@ -1,27 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_report_with_dimension_in_list_filter - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_run_report_with_dimension_in_list_filter(capsys): - run_report_with_dimension_in_list_filter.run_report_with_dimension_in_list_filter( - TEST_PROPERTY_ID - ) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/run_report_with_multiple_dimension_filters.py b/samples/snippets/run_report_with_multiple_dimension_filters.py deleted file mode 100644 index 0fe06085..00000000 --- a/samples/snippets/run_report_with_multiple_dimension_filters.py +++ /dev/null @@ -1,88 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the usage of -dimension and metric filters in a report. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport#body.request_body.FIELDS.dimension_filter -for more information. -""" -# [START analyticsdata_run_report_with_multiple_dimension_filters] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - DateRange, - Dimension, - Filter, - FilterExpression, - FilterExpressionList, - Metric, - RunReportRequest, -) - -from run_report import print_run_report_response - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_report_with_multiple_dimension_filters(property_id) - - -def run_report_with_multiple_dimension_filters(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a report using multiple dimension filters joined as `and_group` - expression. The filter selects for when both `browser` is `Chrome` and - `countryId` is `US`. - - This sample uses relative date range values. See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/DateRange - for more information. - """ - client = BetaAnalyticsDataClient() - - request = RunReportRequest( - property=f"properties/{property_id}", - dimensions=[Dimension(name="browser")], - metrics=[Metric(name="activeUsers")], - date_ranges=[DateRange(start_date="7daysAgo", end_date="yesterday")], - dimension_filter=FilterExpression( - and_group=FilterExpressionList( - expressions=[ - FilterExpression( - filter=Filter( - field_name="browser", - string_filter=Filter.StringFilter(value="Chrome"), - ) - ), - FilterExpression( - filter=Filter( - field_name="countryId", - string_filter=Filter.StringFilter(value="US"), - ) - ), - ] - ) - ), - ) - response = client.run_report(request) - print_run_report_response(response) - - -# [END analyticsdata_run_report_with_multiple_dimension_filters] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_report_with_multiple_dimension_filters_test.py b/samples/snippets/run_report_with_multiple_dimension_filters_test.py deleted file mode 100644 index 82f2160a..00000000 --- a/samples/snippets/run_report_with_multiple_dimension_filters_test.py +++ /dev/null @@ -1,27 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_report_with_multiple_dimension_filters - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_run_report_with_multiple_dimension_filters(capsys): - run_report_with_multiple_dimension_filters.run_report_with_multiple_dimension_filters( - TEST_PROPERTY_ID - ) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/run_report_with_multiple_dimensions.py b/samples/snippets/run_report_with_multiple_dimensions.py deleted file mode 100644 index 4b0e6ba5..00000000 --- a/samples/snippets/run_report_with_multiple_dimensions.py +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the creation -of a basic report. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport -for more information. -""" -# [START analyticsdata_run_report_with_multiple_dimensions] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - DateRange, - Dimension, - Metric, - RunReportRequest, -) - -from run_report import print_run_report_response - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_report_with_multiple_dimensions(property_id) - - -def run_report_with_multiple_dimensions(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a report of active users grouped by three dimensions.""" - client = BetaAnalyticsDataClient() - - request = RunReportRequest( - property=f"properties/{property_id}", - dimensions=[ - Dimension(name="country"), - Dimension(name="region"), - Dimension(name="city"), - ], - metrics=[Metric(name="activeUsers")], - date_ranges=[DateRange(start_date="7daysAgo", end_date="today")], - ) - response = client.run_report(request) - print_run_report_response(response) - - -# [END analyticsdata_run_report_with_multiple_dimensions] - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_report_with_multiple_dimensions_test.py b/samples/snippets/run_report_with_multiple_dimensions_test.py deleted file mode 100644 index 2f0f0575..00000000 --- a/samples/snippets/run_report_with_multiple_dimensions_test.py +++ /dev/null @@ -1,27 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_report_with_multiple_dimensions - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_run_report_with_multiple_dimensions(capsys): - run_report_with_multiple_dimensions.run_report_with_multiple_dimensions( - TEST_PROPERTY_ID - ) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/run_report_with_multiple_metrics.py b/samples/snippets/run_report_with_multiple_metrics.py deleted file mode 100644 index 279ed40d..00000000 --- a/samples/snippets/run_report_with_multiple_metrics.py +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the creation -of a basic report. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport -for more information. -""" -# [START analyticsdata_run_report_with_multiple_metrics] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - DateRange, - Dimension, - Metric, - RunReportRequest, -) - -from run_report import print_run_report_response - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_report_with_multiple_metrics(property_id) - - -def run_report_with_multiple_metrics(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a report of active users, new users and total revenue grouped by - date dimension.""" - client = BetaAnalyticsDataClient() - - # Runs a report of active users grouped by three dimensions. - request = RunReportRequest( - property=f"properties/{property_id}", - dimensions=[Dimension(name="date")], - metrics=[ - Metric(name="activeUsers"), - Metric(name="newUsers"), - Metric(name="totalRevenue"), - ], - date_ranges=[DateRange(start_date="7daysAgo", end_date="today")], - ) - response = client.run_report(request) - print_run_report_response(response) - - -# [END analyticsdata_run_report_with_multiple_metrics] - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_report_with_multiple_metrics_test.py b/samples/snippets/run_report_with_multiple_metrics_test.py deleted file mode 100644 index 92ab1eb5..00000000 --- a/samples/snippets/run_report_with_multiple_metrics_test.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_report_with_multiple_metrics - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_run_report_with_multiple_metrics(capsys): - run_report_with_multiple_metrics.run_report_with_multiple_metrics(TEST_PROPERTY_ID) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/run_report_with_named_date_ranges.py b/samples/snippets/run_report_with_named_date_ranges.py deleted file mode 100644 index 41aee5b0..00000000 --- a/samples/snippets/run_report_with_named_date_ranges.py +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the usage of -date ranges in a report. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/DateRange#FIELDS.name -for more information. -""" -# [START analyticsdata_run_report_with_named_date_ranges] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - DateRange, - Dimension, - Metric, - RunReportRequest, -) - -from run_report import print_run_report_response - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_report_with_named_date_ranges(property_id) - - -def run_report_with_named_date_ranges(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a report using named date ranges.""" - client = BetaAnalyticsDataClient() - - request = RunReportRequest( - property=f"properties/{property_id}", - date_ranges=[ - DateRange(start_date="2020-01-01", end_date="2020-01-31", name="year_ago"), - DateRange( - start_date="2021-01-01", end_date="2021-01-31", name="current_year" - ), - ], - dimensions=[Dimension(name="country")], - metrics=[Metric(name="sessions")], - ) - response = client.run_report(request) - print_run_report_response(response) - - -# [END analyticsdata_run_report_with_named_date_ranges] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_report_with_named_date_ranges_test.py b/samples/snippets/run_report_with_named_date_ranges_test.py deleted file mode 100644 index 0c0637fa..00000000 --- a/samples/snippets/run_report_with_named_date_ranges_test.py +++ /dev/null @@ -1,27 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_report_with_named_date_ranges - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_run_report_with_named_date_ranges(capsys): - run_report_with_named_date_ranges.run_report_with_named_date_ranges( - TEST_PROPERTY_ID - ) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/run_report_with_ordering.py b/samples/snippets/run_report_with_ordering.py deleted file mode 100644 index fad333ae..00000000 --- a/samples/snippets/run_report_with_ordering.py +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the ordering of - report rows. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport#body.request_body.FIELDS.order_bys -for more information. -""" -# [START analyticsdata_run_report_with_ordering] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - DateRange, - Dimension, - Metric, - OrderBy, - RunReportRequest, -) - -from run_report import print_run_report_response - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_report_with_ordering(property_id) - - -def run_report_with_ordering(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a report of active users grouped by three dimensions, ordered by - the total revenue in descending order.""" - client = BetaAnalyticsDataClient() - - request = RunReportRequest( - property=f"properties/{property_id}", - dimensions=[Dimension(name="date")], - metrics=[ - Metric(name="activeUsers"), - Metric(name="newUsers"), - Metric(name="totalRevenue"), - ], - date_ranges=[DateRange(start_date="7daysAgo", end_date="today")], - order_bys=[ - OrderBy(metric=OrderBy.MetricOrderBy(metric_name="totalRevenue"), desc=True) - ], - ) - response = client.run_report(request) - print_run_report_response(response) - - -# [END analyticsdata_run_report_with_ordering] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_report_with_ordering_test.py b/samples/snippets/run_report_with_ordering_test.py deleted file mode 100644 index 28c72978..00000000 --- a/samples/snippets/run_report_with_ordering_test.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_report_with_ordering - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_run_report_with_ordering(capsys): - run_report_with_ordering.run_report_with_ordering(TEST_PROPERTY_ID) - out, _ = capsys.readouterr() - assert "Report result" in out diff --git a/samples/snippets/run_report_with_pagination.py b/samples/snippets/run_report_with_pagination.py deleted file mode 100644 index 9211993c..00000000 --- a/samples/snippets/run_report_with_pagination.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the use of -pagination to retrieve large result sets. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport#body.request_body.FIELDS.offset -for more information. -""" -# [START analyticsdata_run_report_with_pagination] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - DateRange, - Dimension, - Metric, - RunReportRequest, -) - -from run_report import print_run_report_response - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_report_with_pagination(property_id) - - -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() - - # [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="conversions"), - Metric(name="totalRevenue"), - ], - limit=100000, - offset=0, - ) - response = client.run_report(request) - # [END analyticsdata_run_report_with_pagination_page1] - 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="conversions"), - Metric(name="totalRevenue"), - ], - limit=100000, - offset=100000, - ) - response = client.run_report(request) - # [END analyticsdata_run_report_with_pagination_page2] - print_run_report_response(response) - - -# [END analyticsdata_run_report_with_pagination] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_report_with_pagination_test.py b/samples/snippets/run_report_with_pagination_test.py deleted file mode 100644 index b9b65e70..00000000 --- a/samples/snippets/run_report_with_pagination_test.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_report_with_pagination - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -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 diff --git a/samples/snippets/run_report_with_property_quota.py b/samples/snippets/run_report_with_property_quota.py deleted file mode 100644 index cd87336f..00000000 --- a/samples/snippets/run_report_with_property_quota.py +++ /dev/null @@ -1,86 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Data API sample application demonstrating the usage of -property quota metadata. - -See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport#body.request_body.FIELDS.return_property_quota -for more information. -""" -# [START analyticsdata_run_report_with_property_quota] -from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - DateRange, - Dimension, - Metric, - RunReportRequest, -) - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - run_report_with_property_quota(property_id) - - -def run_report_with_property_quota(property_id="YOUR-GA4-PROPERTY-ID"): - """Runs a report and prints property quota information.""" - client = BetaAnalyticsDataClient() - - request = RunReportRequest( - property=f"properties/{property_id}", - return_property_quota=True, - dimensions=[Dimension(name="country")], - metrics=[Metric(name="activeUsers")], - date_ranges=[DateRange(start_date="7daysAgo", end_date="today")], - ) - response = client.run_report(request) - - # [START analyticsdata_run_report_with_property_quota_print_response] - if response.property_quota: - print( - f"Tokens per day quota consumed: {response.property_quota.tokens_per_day.consumed}, " - f"remaining: {response.property_quota.tokens_per_day.remaining}." - ) - - print( - f"Tokens per hour quota consumed: {response.property_quota.tokens_per_hour.consumed}, " - f"remaining: {response.property_quota.tokens_per_hour.remaining}." - ) - - print( - f"Concurrent requests quota consumed: {response.property_quota.concurrent_requests.consumed}, " - f"remaining: {response.property_quota.concurrent_requests.remaining}." - ) - - print( - f"Server errors per project per hour quota consumed: {response.property_quota.server_errors_per_project_per_hour.consumed}, " - f"remaining: {response.property_quota.server_errors_per_project_per_hour.remaining}." - ) - print( - f"Potentially thresholded requests per hour quota consumed: {response.property_quota.potentially_thresholded_requests_per_hour.consumed}, " - f"remaining: {response.property_quota.potentially_thresholded_requests_per_hour.remaining}." - ) - # [END analyticsdata_run_report_with_property_quota_print_response] - - -# [END analyticsdata_run_report_with_property_quota] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/snippets/run_report_with_property_quota_test.py b/samples/snippets/run_report_with_property_quota_test.py deleted file mode 100644 index 1b17fb88..00000000 --- a/samples/snippets/run_report_with_property_quota_test.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import run_report_with_property_quota - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") - - -def test_run_report_with_property_quota(capsys): - run_report_with_property_quota.run_report_with_property_quota(TEST_PROPERTY_ID) - out, _ = capsys.readouterr() - assert "Tokens per day quota consumed" in out