google-analytics-data-json


Namegoogle-analytics-data-json JSON
Version 0.0.2.2 PyPI version JSON
download
home_page
SummaryA package to simplify fetching and transforming data from the Google Analytics Data GA4 API
upload_time2023-10-17 18:48:03
maintainer
docs_urlNone
author
requires_python>=3.7
licenseMIT License Copyright (c) 2022 Daniel Jerrehian Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords
VCS
bugtrack_url
requirements cachetools certifi charset-normalizer google-analytics-data google-api-core google-auth googleapis-common-protos grpcio grpcio-status idna proto-plus protobuf pyasn1 pyasn1-modules python-dotenv requests rsa six google-api-python-client
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Introduction 
This package simplifies fetching and transforming data from the Google Analytics Data GA4 API. The GA4 API returns data in a Google object, which can be a hassle to parse and transform. Using this package, you can create a dictionary with your Google Analytics Property ID, as well as the desired Dimensions, Metrics, Order Bys, and Date Ranges you would like to query. By running the function `run_report_return_json(...)` in `google_analytics_data_json.py`, you can call the GA4 API and retrieve data in JSON format. Using the [Google GA4 Query Explorer](https://ga-dev-tools.web.app/ga4/query-explorer/), you can find all possible Metrics, Dimensions, Order Bys, and Date Ranges.

# Installation
`pip install google-analytics-data-json`

# Environment Variables
1. After installing the package in your virtual environment, add the `credentials.json` file to your project.
2. Optional: Set an environment variable named `GOOGLE_APPLICATION_CREDENTIALS` equal to the path of the previously mentioned `credentials.json` file.
3. Optional: Set the `GOOGLE_ANALYTICS_PROPERTY_ID` environment variable (required for unit tests).

# Running The Project and Examples
The simpelest way to run the project is to import the function `run_report_return_json(...)` from the file `google_analytics_data_json`. This function takes one parameter called an `analytics_dictionary`, which is used to fetch data from the API and return it in JSON format. The structure of the analytics dictionary should look like:

```python

from google_analytics_data_json.google_analytics_data_json import run_report_return_json

analytics_dictionary = {
    "property_id": "<your property_id>",
    "dimension_names": ["date", "country", "deviceCategory"],
    "metric_names": ["totalUsers", "newUsers"], 
    "order_by_names": [
        {
            "type": "dimension", 
            "value": "date", 
            "descending": False
        },
        {
            "type": "dimension",
            "value": "country", 
            "descending": True
        },
        {
            "type": "metric",
            "value": "newUsers", 
            "descending": True
        }
    ],
    "date_range_values": [
        {
            "start_date": "2022-07-01",
            "end_date": "2022-07-04"
        }
    ]
}

# uses GOOGLE_APPLICATION_CREDENTIALS environment variable path
ga_data: List[dict] = run_report_return_json(analytics_dictionary)

# uses credentials in dict, from a secret manager or similar
from google.oauth2 import service_account
service_account_credentials = service_account.Credentials.from_service_account_info(ga_credentials_secret_dict)
ga_data: List[dict] = run_report_return_json(analytics_dictionary, service_account_credentials=service_account_credentials)

```

It is worth noting that the `dimension_names`, `metric_names`, `order_by_names`, and `date_range_values` are list objects, with the `order_by_names` and `date_range_values` requiring nested dictionaries as shown above. 

Although multiple date ranges can be used, the results from the API can be confusing if the dimension `date` is used - therefore, I suggest only using one date range dictionary if you plan querying the `date` dimension.

If you would like to take advantage of only the API call or only the data transformation, you can import the classes `Ga4Request` from `create_ga4_request.py` or `TransformGa4Data` from `transform_ga4_data.py`. This allows you to control the exact methods which are called in each class, although in most cases you will use all of them.

# Build and Test
The build and tests for the project are hosted [in the project repository](https://github.com/DanielJerrehian/google_analytics_data_json).

Use `python -m unittest` to run the unit tests. A coverage report can also be generated using `coverage run -m unittest` and then running `coverage html`.

The following environment variables should be set for unit tests:
* `GOOGLE_APPLICATION_CREDENTIALS`
* `GOOGLE_ANALYTICS_PROPERTY_ID`

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "google-analytics-data-json",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "",
    "author": "",
    "author_email": "Daniel Jerrehian <danieljerrehian@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/80/3e/edab98a6199797eef76139ccccbf02da4dea51ddc089b046382dc0f12b69/google_analytics_data_json-0.0.2.2.tar.gz",
    "platform": null,
    "description": "# Introduction \nThis package simplifies fetching and transforming data from the Google Analytics Data GA4 API. The GA4 API returns data in a Google object, which can be a hassle to parse and transform. Using this package, you can create a dictionary with your Google Analytics Property ID, as well as the desired Dimensions, Metrics, Order Bys, and Date Ranges you would like to query. By running the function `run_report_return_json(...)` in `google_analytics_data_json.py`, you can call the GA4 API and retrieve data in JSON format. Using the [Google GA4 Query Explorer](https://ga-dev-tools.web.app/ga4/query-explorer/), you can find all possible Metrics, Dimensions, Order Bys, and Date Ranges.\n\n# Installation\n`pip install google-analytics-data-json`\n\n# Environment Variables\n1. After installing the package in your virtual environment, add the `credentials.json` file to your project.\n2. Optional: Set an environment variable named `GOOGLE_APPLICATION_CREDENTIALS` equal to the path of the previously mentioned `credentials.json` file.\n3. Optional: Set the `GOOGLE_ANALYTICS_PROPERTY_ID` environment variable (required for unit tests).\n\n# Running The Project and Examples\nThe simpelest way to run the project is to import the function `run_report_return_json(...)` from the file `google_analytics_data_json`. This function takes one parameter called an `analytics_dictionary`, which is used to fetch data from the API and return it in JSON format. The structure of the analytics dictionary should look like:\n\n```python\n\nfrom google_analytics_data_json.google_analytics_data_json import run_report_return_json\n\nanalytics_dictionary = {\n    \"property_id\": \"<your property_id>\",\n    \"dimension_names\": [\"date\", \"country\", \"deviceCategory\"],\n    \"metric_names\": [\"totalUsers\", \"newUsers\"], \n    \"order_by_names\": [\n        {\n            \"type\": \"dimension\", \n            \"value\": \"date\", \n            \"descending\": False\n        },\n        {\n            \"type\": \"dimension\",\n            \"value\": \"country\", \n            \"descending\": True\n        },\n        {\n            \"type\": \"metric\",\n            \"value\": \"newUsers\", \n            \"descending\": True\n        }\n    ],\n    \"date_range_values\": [\n        {\n            \"start_date\": \"2022-07-01\",\n            \"end_date\": \"2022-07-04\"\n        }\n    ]\n}\n\n# uses GOOGLE_APPLICATION_CREDENTIALS environment variable path\nga_data: List[dict] = run_report_return_json(analytics_dictionary)\n\n# uses credentials in dict, from a secret manager or similar\nfrom google.oauth2 import service_account\nservice_account_credentials = service_account.Credentials.from_service_account_info(ga_credentials_secret_dict)\nga_data: List[dict] = run_report_return_json(analytics_dictionary, service_account_credentials=service_account_credentials)\n\n```\n\nIt is worth noting that the `dimension_names`, `metric_names`, `order_by_names`, and `date_range_values` are list objects, with the `order_by_names` and `date_range_values` requiring nested dictionaries as shown above. \n\nAlthough multiple date ranges can be used, the results from the API can be confusing if the dimension `date` is used - therefore, I suggest only using one date range dictionary if you plan querying the `date` dimension.\n\nIf you would like to take advantage of only the API call or only the data transformation, you can import the classes `Ga4Request` from `create_ga4_request.py` or `TransformGa4Data` from `transform_ga4_data.py`. This allows you to control the exact methods which are called in each class, although in most cases you will use all of them.\n\n# Build and Test\nThe build and tests for the project are hosted [in the project repository](https://github.com/DanielJerrehian/google_analytics_data_json).\n\nUse `python -m unittest` to run the unit tests. A coverage report can also be generated using `coverage run -m unittest` and then running `coverage html`.\n\nThe following environment variables should be set for unit tests:\n* `GOOGLE_APPLICATION_CREDENTIALS`\n* `GOOGLE_ANALYTICS_PROPERTY_ID`\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2022 Daniel Jerrehian  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "A package to simplify fetching and transforming data from the Google Analytics Data GA4 API",
    "version": "0.0.2.2",
    "project_urls": {
        "Bug Tracker": "https://github.com/DanielJerrehian/google_analytics_data_json/issues",
        "Homepage": "https://github.com/DanielJerrehian/google_analytics_data_json"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bb48eac5feac96c9fcfcb487ab287d61a3af8f0cbdcc048abef8ef1b4e47256c",
                "md5": "559c91aa2844b63ebf9c795f4adccdb4",
                "sha256": "9953a712c6cb483938c911ff2f79c250b035ebd0b9abe04dde1399c1443414df"
            },
            "downloads": -1,
            "filename": "google_analytics_data_json-0.0.2.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "559c91aa2844b63ebf9c795f4adccdb4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 7373,
            "upload_time": "2023-10-17T18:47:57",
            "upload_time_iso_8601": "2023-10-17T18:47:57.256841Z",
            "url": "https://files.pythonhosted.org/packages/bb/48/eac5feac96c9fcfcb487ab287d61a3af8f0cbdcc048abef8ef1b4e47256c/google_analytics_data_json-0.0.2.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "803eedab98a6199797eef76139ccccbf02da4dea51ddc089b046382dc0f12b69",
                "md5": "8c893375f78b8c645e18ba14e59169c5",
                "sha256": "b448d6c6019755b537940a386d87ed89e004c38e70c0ef874cd4295c8d8224a7"
            },
            "downloads": -1,
            "filename": "google_analytics_data_json-0.0.2.2.tar.gz",
            "has_sig": false,
            "md5_digest": "8c893375f78b8c645e18ba14e59169c5",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 8148,
            "upload_time": "2023-10-17T18:48:03",
            "upload_time_iso_8601": "2023-10-17T18:48:03.714515Z",
            "url": "https://files.pythonhosted.org/packages/80/3e/edab98a6199797eef76139ccccbf02da4dea51ddc089b046382dc0f12b69/google_analytics_data_json-0.0.2.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-17 18:48:03",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "DanielJerrehian",
    "github_project": "google_analytics_data_json",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "cachetools",
            "specs": [
                [
                    "~=",
                    "5.3.1"
                ]
            ]
        },
        {
            "name": "certifi",
            "specs": [
                [
                    "==",
                    "2022.6.15"
                ]
            ]
        },
        {
            "name": "charset-normalizer",
            "specs": [
                [
                    "~=",
                    "2.1.0"
                ]
            ]
        },
        {
            "name": "google-analytics-data",
            "specs": [
                [
                    "==",
                    "0.12.1"
                ]
            ]
        },
        {
            "name": "google-api-core",
            "specs": [
                [
                    "==",
                    "2.8.2"
                ]
            ]
        },
        {
            "name": "google-auth",
            "specs": [
                [
                    "==",
                    "2.9.0"
                ]
            ]
        },
        {
            "name": "googleapis-common-protos",
            "specs": [
                [
                    "==",
                    "1.56.3"
                ]
            ]
        },
        {
            "name": "grpcio",
            "specs": [
                [
                    "==",
                    "1.47.0"
                ]
            ]
        },
        {
            "name": "grpcio-status",
            "specs": [
                [
                    "==",
                    "1.47.0"
                ]
            ]
        },
        {
            "name": "idna",
            "specs": [
                [
                    "<",
                    "4"
                ],
                [
                    ">=",
                    "3.3"
                ]
            ]
        },
        {
            "name": "proto-plus",
            "specs": [
                [
                    "==",
                    "1.20.6"
                ]
            ]
        },
        {
            "name": "protobuf",
            "specs": [
                [
                    "==",
                    "3.20.1"
                ]
            ]
        },
        {
            "name": "pyasn1",
            "specs": [
                [
                    "==",
                    "0.4.8"
                ]
            ]
        },
        {
            "name": "pyasn1-modules",
            "specs": [
                [
                    "==",
                    "0.2.8"
                ]
            ]
        },
        {
            "name": "python-dotenv",
            "specs": [
                [
                    "~=",
                    "0.20.0"
                ]
            ]
        },
        {
            "name": "requests",
            "specs": [
                [
                    ">=",
                    "2.28.1"
                ]
            ]
        },
        {
            "name": "rsa",
            "specs": [
                [
                    "~=",
                    "4.8"
                ]
            ]
        },
        {
            "name": "six",
            "specs": [
                [
                    "~=",
                    "1.16.0"
                ]
            ]
        },
        {
            "name": "google-api-python-client",
            "specs": [
                [
                    "~=",
                    "2.86.0"
                ]
            ]
        }
    ],
    "lcname": "google-analytics-data-json"
}
        
Elapsed time: 0.12976s