Package: google-analytics-data-json
IntroductionThis 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 functionrun_report_return_json(...)ingoogle_analytics_data_json.py, you can call the GA4 API and retrieve data in JSON format. Using theGoogle GA4 Query Explorer, you can find all possible Metrics, Dimensions, Order Bys, and Date Ranges.Installationpip install google-analytics-data-jsonEnvironment VariablesAfter installing the package in your virtual environment, add thecredentials.jsonfile to your project.Optional: Set an environment variable namedGOOGLE_APPLICATION_CREDENTIALSequal to the path of the previously mentionedcredentials.jsonfile.Optional: Set theGOOGLE_ANALYTICS_PROPERTY_IDenvironment variable (required for unit tests).Running The Project and ExamplesThe simpelest way to run the project is to import the functionrun_report_return_json(...)from the filegoogle_analytics_data_json. This function takes one parameter called ananalytics_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:fromgoogle_analytics_data_json.google_analytics_data_jsonimportrun_report_return_jsonanalytics_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 pathga_data:List[dict]=run_report_return_json(analytics_dictionary)# uses credentials in dict, from a secret manager or similarfromgoogle.oauth2importservice_accountservice_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 thedimension_names,metric_names,order_by_names, anddate_range_valuesare list objects, with theorder_by_namesanddate_range_valuesrequiring nested dictionaries as shown above.Although multiple date ranges can be used, the results from the API can be confusing if the dimensiondateis used - therefore, I suggest only using one date range dictionary if you plan querying thedatedimension.If you would like to take advantage of only the API call or only the data transformation, you can import the classesGa4Requestfromcreate_ga4_request.pyorTransformGa4Datafromtransform_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 TestThe build and tests for the project are hostedin the project repository.Usepython -m unittestto run the unit tests. A coverage report can also be generated usingcoverage run -m unittestand then runningcoverage html.The following environment variables should be set for unit tests:GOOGLE_APPLICATION_CREDENTIALSGOOGLE_ANALYTICS_PROPERTY_ID