GAPandas
GAPandas is a Python package that lets you query the Google Analytics reporting API and return results in Pandas DataFrames, so they can be easily analysed, reported, or plotted in Python applications. It is a simple wrapper to Google's official API which is designed to reduce code and simplify development, especially from Jupyter Notebook environments.
Setup
GAPandas is easy to set up. First, you need to obtain a client_secrets.json keyfile from Google Analytics in order to authenticate. Google's documentation explains how to do this.
Once you have created a client_secrets.json file, download it and store it on your machine and note the path to the file. Add the associated email as a user in your Google Analytics account.
Basic example
To make a query, authenticate by running connect.get_service() passing it the path to your client_secrets.json keyfile.
from gapandas import connect, query
service = connect.get_service('path/to/client_secrets.json')
Now you have a connection, construct an API query to pass to the API. This "payload" must include a start-date and end-date, some metrics and some dimensions stored in a Python dictionary.
The queries can sometimes be fiddly to write. I recommend using the Google Analytics Query Explorer to construct a valid API query or creating a prototype in Google Sheets. In the below example, we'll fetch sessions, pageviews and bounces by date for the past 30 days.
payload = {
'start_date': '30daysAgo',
'end_date': 'today',
'metrics': 'ga:sessions, ga:pageviews, ga:bounces',
'dimensions': 'ga:date'
}
Now you can then use the query.run_query() function to pass your payload to the API, along with the service object and your Google Analytics view ID.
results = query.run_query(service, '123456789', payload)
print(results)
By default, this will return a Pandas DataFrame containing your query results. However, by passing the optional value of 'raw' at the end of the function you can also return the raw data object. The query method also provides some other features to extract data from the raw object.
results = query.run_query(service, '123456789', payload, 'raw')
You can run multiple queries in succession and use the Pandas merge() function to connect these together. Pandas also makes it very easy to write the data to a file, such as a CSV or Excel document or write it to a database. You can use the data in reports, visualisations or machine learning models with very little code.
Pagination
If you do not define max_results the API will return a default of 1000 rows in a single page. You can set this to a maximum of 10000 in your payload.
Pagination is handled automatically. GAPandas will fetch each page of results and return them all in a single DataFrame (or object if you pass the raw flag in your query.)
Changes
- Version 0.16 - Added
set_dtypes()function to set the correct dtypes and improved error handling. - Version 0.17 - Fixed bug in use of
exit()
Metadata
Release files for gapandas 1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| gapandas-1.0.tar.gz | 13.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| gapandas-1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 25.1 kB
Release files / gapandas-1.0.tar.gz
| Download URL | gapandas-1.0.tar.gz |
|---|---|
| Size | 13.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3361cc6dc1805a268ddb81bfcf3451b19c8d2d2de9db8d7f315099f231c54a8b
|
|
BLAKE2b-256 checksum How to use checksums |
98888b0e58004f202d70802ea9dc69a8669dc9829e7b74b94b1293a1a9a2bb79
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10
|
Release files / gapandas-1.0-py3-none-any.whl
| Download URL | gapandas-1.0-py3-none-any.whl |
|---|---|
| Size | 12.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ea50f833fcfe59e949a19a7d34e830fd86cb46e80553ff577efa41a3f793c3aa
|
|
BLAKE2b-256 checksum How to use checksums |
b2a8bafdeb3ea056e260ebd359c08a8d6ec3cbeb86c4cda06897e636fe59306a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10
|
Security for gapandas 1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.