Skip to main content

Google Analytics Extractor

PyPI version

A CLI tool for extracting Google Analytics data using Google Reporting API. Can be also used to transform data to various formats suitable for migration to other analytics platforms.

Setup

You will need Google Cloud API access for run the CLI:

  • Navigate to Cloud Resource Manager and click Create Project

    • alternatively create project with gcloud projects create $PROJECT_ID
  • Navigate to Reporting API and click Enable

  • Create credentials:

    • Go to credentials page

    • Click Create credentials, select Service account

    • Give it a name and make note of service account email. Click Create and Continue

    • Open Service account page

    • Select previously created service account, Open Keys tab

    • Click Add Key and Create New Key. Choose JSON format and download it. (store this securely)

  • Give SA permissions to GA - guide

    • email: SA email from earlier
    • role: Viewer

Alternatively see https://martinheinz.dev/blog/62.

To install and run:

pip install ga-extractor
ga-extractor --help

Running

ga-extractor --help
# Usage: ga-extractor [OPTIONS] COMMAND [ARGS]...
# ...

# Create config file:
ga-extractor setup \
  --sa-key-path="analytics-api-24102021-4edf0b7270c0.json" \
  --table-id="123456789" \
  --metrics="ga:sessions" \
  --dimensions="ga:browser" \
  --start-date="2022-03-15" \
  --end-date="2022-03-19"
  
cat ~/.config/ga-extractor/config.yaml  # Optionally, check config

ga-extractor auth  # Test authentication
# Successfully authenticated with user: ...

ga-extractor setup --help  # For options and flags
  • Value for --table-id can be found in GA web console - Click on Admin section, View Settings and see View ID field
  • All configurations and generated extracts/reports are stored in ~/.config/ga-extrator/...
  • You can also use metrics and dimensions presets using --preset with FULL or BASIC, if you're not sure which data to extract

Extract

ga-extractor extract
# Report written to /home/some-user/.config/ga-extractor/report.json

extract perform raw extraction of dimensions and metrics using the provided configs

Migrate

You can directly extract and transform data to various formats. Available options are:

  • JSON (Default option; Default API output)
  • CSV
  • SQL (compatible with Umami Analytics PostgreSQL backend)
ga-extractor migrate --format=CSV
# Report written to /home/user/.config/ga-extractor/02c2db1a-1ff0-47af-bad3-9c8bc51c1d13_extract.csv

head /home/user/.config/ga-extractor/02c2db1a-1ff0-47af-bad3-9c8bc51c1d13_extract.csv
# path,browser,os,device,screen,language,country,referral_path,count,date
# /,Chrome,Android,mobile,1370x1370,zh-cn,China,(direct),1,2022-03-18
# /,Chrome,Android,mobile,340x620,en-gb,United Kingdom,t.co/,1,2022-03-18

ga-extractor migrate --format=UMAMI
# Report written to /home/user/.config/ga-extractor/cee9e1d0-3b87-4052-a295-1b7224c5ba78_extract.sql

# IMPORTANT: Verify the data and check test database before inserting into production instance 
# To insert into DB (This should be run against clean database):
cat cee9e1d0-3b87-4052-a295-1b7224c5ba78_extract.sql | psql -Upostgres -a some-db

You can verify the data is correct in Umami web console and GA web console:

Note: Some data in GA and Umami web console might be little off, because GA displays many metrics based on sessions (e.g. Sessions by device), but data is extracted/migrated based on page views. You can however confirm that percentage breakdown of browser or OS usage does match.

Development

Setup

Requirements:

  • Poetry (+ virtual environment)
poetry install
python -m ga_extractor --help

Testing

pytest

Building Package

poetry install
ga-extractor --help

# Usage: ga-extractor [OPTIONS] COMMAND [ARGS]...
# ...

Metadata

Release files for ga-extractor 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ga-extractor 0.1.1
File Size Uploaded Provenance Actions
ga-extractor-0.1.1.tar.gz 9.5 kB Without provenance Details for ga-extractor-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ga-extractor 0.1.1
File Interpreter ABI Platform Actions
ga_extractor-0.1.1-py3-none-any.whl Python 3 none any Details for ga_extractor-0.1.1-py3-none-any.whl

Total release size: 18.5 kB

Release files / ga-extractor-0.1.1.tar.gz

Download URL ga-extractor-0.1.1.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
fc57cca1a10fde034aebc1cdab2885917a38fb50b6dda3af84ad65389851f9ba
BLAKE2b-256 checksum
How to use checksums
bcd840f7313c6579376ec3302d83aa05b6546ab25e78400a848543c861f8f277
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.8.10 Linux/5.11.0-44-generic

Release files / ga_extractor-0.1.1-py3-none-any.whl

Download URL ga_extractor-0.1.1-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6d86da94758fba706321ea3dcd14ada9aaa44e496684673775020814f5d48b6b
BLAKE2b-256 checksum
How to use checksums
00ab71d099d2d5f2b9a65ba164087f86c6f4b2ff70154e17c917e89e3ef0a8a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.8.10 Linux/5.11.0-44-generic

Security for ga-extractor 0.1.1

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.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page