Skip to main content

Python toolkit for running GA4 and Search Console reports in Colab/Notebooks and publishing results to Google Sheets, CSV, or BigQuery, with built-in retries and easy append/upsert.

Project description

megaton

PyPI version Python 3.11+ License: MIT

Megaton is a Python toolkit for working with Google Analytics 4, Google Search Console, Google Sheets, and BigQuery from Notebooks with minimal code. It focuses on fast iteration during analysis and provides a UX tailored for Notebook workflows.

Core Concepts

  • Result objects — Method chaining via SearchResult / ReportResult
  • Simple flow — Open → Set dates → Run → Save
  • Notebook-first — Designed for inspecting intermediate results at every step

Quick Start

Prerequisites

You need a Google Cloud service account JSON file with access to GA4, Search Console, or Sheets. See Google Cloud docs for how to create one.

Install

pip install megaton              # core (headless / programmatic use)
pip install megaton[notebook]    # + ipywidgets for the interactive selection UI

ipywidgets is no longer a core dependency (since 2.0). Install the notebook extra when you want the widget-based credential/account/property picker used by Megaton(...) in Jupyter/Colab. For scripts, CI, or headless runs, the core install is enough — use Megaton(..., headless=True), Megaton.for_property(...), or Megaton.for_site(...).

Run a GA4 report and save to Google Sheets

from megaton.start import Megaton

# Interactive (Jupyter/Colab): needs megaton[notebook] for the picker UI.
mg = Megaton("/path/to/service_account.json")
# Scripts/CI (core install, no widgets): select the property up front.
# mg = Megaton.for_property("YOUR_GA4_PROPERTY_ID", "/path/to/service_account.json")

# GA4: fetch event data
mg.report.set.dates("2024-01-01", "2024-01-31")
result = mg.report.run(d=["date", "eventName"], m=["eventCount"])

# Save to Google Sheets
mg.open.sheet("https://docs.google.com/spreadsheets/d/...")
mg.save.to.sheet("_ga_data", result.df)
mg.sheets.select("_ga_data")
mg.sheet.freeze(rows=1)
mg.sheet.resize(rows=1000, cols=20)
mg.sheet.gridlines.hide()
mg.sheet.tab.color("#2f80ed")

Run the same report over multiple date ranges

df = mg.report.run.ranges(
    date_ranges=[("2024-01-01", "2024-01-31"), ("2025-01-01", "2025-01-31")],
    d=["date", "eventName"],
    m=["eventCount"],
)

Read a worksheet as DataFrame

mg.open.sheet("https://docs.google.com/spreadsheets/d/...")
daily_df = mg.sheets.read("daily")

Duplicate a worksheet and patch a cell

mg.open.sheet("https://docs.google.com/spreadsheets/d/...")
mg.sheets.duplicate(
    "template",
    "report_2024_02",
    cell_update={"cell": "B1", "value": "202402"},
)

Search Console with method chaining

# query_map: dict mapping regex patterns to category names
# e.g. {"brand.*keyword": "Brand", ".*": "(other)"}
result = (mg.search
    .run(dimensions=['query', 'page'], clean=True)
    .categorize('query', by=query_map)
    .filter_impressions(min=100)
)

mg.save.to.sheet('_query', result.df, sort_by='impressions')

Installation

# From PyPI
pip install megaton

# Latest from GitHub
pip install git+https://github.com/mak00s/megaton.git

Documentation

Note: Detailed docs are written in Japanese.

If you're new, start with the cookbook for practical examples, then refer to the API reference for details.

Doc Description
cookbook.md Practical recipes — start here
api-reference.md Full API reference (single source of truth)
cheatsheet.md One-line quick reference
design.md Design philosophy and trade-offs

Testing & Coverage

pytest --cov=megaton --cov-report=term-missing

Changelog

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

megaton-2.0.0.tar.gz (160.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

megaton-2.0.0-py3-none-any.whl (103.8 kB view details)

Uploaded Python 3

File details

Details for the file megaton-2.0.0.tar.gz.

File metadata

  • Download URL: megaton-2.0.0.tar.gz
  • Upload date:
  • Size: 160.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for megaton-2.0.0.tar.gz
Algorithm Hash digest
SHA256 303eeb44ff41e7752aff0ab7517ab29a168fc0c9bf43473260d40957b1329385
MD5 f994da7c6ed438ef46cfca5ed6c5e70c
BLAKE2b-256 219f6b7ba0220c18ac524ad72a951788735a62321af648e8ed50d7d2a0f999fe

See more details on using hashes here.

File details

Details for the file megaton-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: megaton-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 103.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for megaton-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3ee4941c09e2296ca4afedbf7684b83161548fd024a32ba4ab7d268242a982a6
MD5 877a1c19bc1de16d450dc14b574ab8a3
BLAKE2b-256 34848ee19cbf26e5bf342d7d1ad3bf8b7c2526831ef8cfad3e85cfa62f7f01f1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page