This is a simple repo that allows you to extract your google analytics UA metrics and dimensions into a csv file.
You will need to setup a project in GCP and create a service account with the correct permissions. You will then need to download the credentials.json file and place it in the root of the repo.
https://developers.google.com/docs/api/quickstart/python
- Clone the repo
- Create a virtual environment:
python3 -m venv venv - Activate the virtual environment:
source venv/bin/activate - Install the requirements:
pip install -r requirements.txt - Create a credentials.json file in the root of the repo (see above)
- Create a uids.csv file in the root of the repo (see below)
- Add the report objects to the reports.py file in the GetAllReports function (see below)
- Run the script:
python reports.py
The Uids.csv file should have the following format:
View ID,Start Date,File 123456789,2019-01-01,myWebSite The report objects are created using the Google Analytics Reporting API v4. You can find the documentation here: https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet You can browse all the metrics/dimensions here: https://developers.google.com/analytics/devguides/reporting/core/dimsmets
Here is an example of a report object for this repo:
NOTE: Do not forget to add the filename to the report object. This will be used to name the csv file.
{'metrics': [{'expression': 'ga:users', },{'expression': 'ga:bounces', },{'expression': 'ga:sessions', },{'expression': 'ga:pageviews', },{'expression': 'ga:pageviewsPerSession', },{'expression': 'ga:sessionDuration', }], 'dimensions': [{'name': 'ga:date' }], 'filename': 'key_metrics' }