Skip to main content

megaton

PyPI version Python 3.11+ License: MIT

Megaton is a Python and CLI toolkit for Google Analytics 4, Search Console, Google Sheets and BigQuery. Run a single query without an application checkout; use the same operations from scripts, Notebooks and MCP adapters.

Python and CLI

pip install megaton
megaton query --params query.json --credential /path/to/google.json --output result.csv
megaton resources gsc --credential /path/to/google.json
python -m megaton query --params query.json --validate-only
megaton auth list                      # machine-wide credential store (2.6.0+)
megaton query --params query.json --sa corp-ga

Credentials can live in one per-machine store (~/.config/megaton) and be named by identity (Megaton(sa="corp-ga"), Megaton(account="sim"), megaton auth exec --account sim --scopes docs -- <command>). The child receives GOOGLE_ACCESS_TOKEN (--env NAME_TOKEN overrides it; only uppercase names ending in _TOKEN); authentication failure prevents launch. Use megaton auth token --account sim --scopes docs --check to check without printing a token. Errors go to stderr: keep it visible, never discard it with 2>/dev/null. Exit codes: 0 success, 2 invalid name/arguments, 3 missing scopes, 4 unregistered account/token, 5 refresh/network/store failure. exec returns the child status (126/127 if launch fails). megaton --version shows the package version and import path. See docs/credential-store.md. GitHub Actions can also use keyless Workload Identity Federation (external_account) through GOOGLE_APPLICATION_CREDENTIALS; see the authentication guide for setup.

query.json selects the resource and dates explicitly:

{
  "schema_version": "1.0",
  "source": "ga4",
  "property_id": "123456789",
  "date_range": {"start": "2026-01-01", "end": "2026-01-31"},
  "dimensions": ["date"],
  "metrics": ["sessions"]
}
from megaton.query import run_query

result = run_query(params, credential="/path/to/google.json")
df = result.df
result.save("result.csv")

The core package provides single Google queries; megaton-app adds multi-step workflows, Adobe, jobs, audits, validation and delivery. See the machine interface contract for GSC/BQ requests, authentication, JSON output and MCP integration. These commands are available from 2.3.0.

Core Concepts

Version 2.2.0 includes reliability changes. GA4/GSC query errors and Sheets read/upsert errors now raise instead of looking like empty results. Sheets overwrite uses one atomic request; structural mutations are submitted once. See failure handling and migration before upgrading scheduled jobs from 2.1.3.

  • Result objects — Method chaining via SearchResult / ReportResult
  • Simple flow — Open → Set dates → Run → Save
  • Shared operations — Explicit machine requests and interactive Notebook sessions

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

For normal Sheets work, use one flow: mg.open.sheet(...) opens a Spreadsheet, mg.sheets.* selects or manages Worksheets, and mg.sheet.* operates on the selected Worksheet.

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.sheets.select("_ga_data")
mg.sheet.save(result)
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.open.sheet("https://docs.google.com/spreadsheets/d/...")
mg.sheets.select("_query")
mg.sheet.save(result, 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

Metadata

Release files for megaton 2.9.0

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

Source distribution (sdist)

Source distribution for megaton 2.9.0
File Size Uploaded Provenance Actions
megaton-2.9.0.tar.gz 226.8 kB With provenance Details for megaton-2.9.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for megaton 2.9.0
File Interpreter ABI Platform Actions
megaton-2.9.0-py3-none-any.whl Python 3 none any Details for megaton-2.9.0-py3-none-any.whl

Total release size: 371.3 kB

Release files / megaton-2.9.0.tar.gz

Download URL megaton-2.9.0.tar.gz
Size 226.8 kB
Tags Source
SHA-256 checksum
How to use checksums
00346b8bc61bde125feaf0eae3eb0b900c30fbc97159d3be71ff08ed2a2d3676
BLAKE2b-256 checksum
How to use checksums
f4bea21f259483c11d2e502ac9c0661fb09e94d79c0b062972e53bc859a235ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Release files / megaton-2.9.0-py3-none-any.whl

Download URL megaton-2.9.0-py3-none-any.whl
Size 144.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6b48f9cd4b1871a1a9b1a6223bff9d7bc8e5958cc1d3107e94dc9c187861dbfa
BLAKE2b-256 checksum
How to use checksums
82cf5c35692b0d74a5ec361bc527812c7f5f1bfac161ee2038d8a3f4ec53e484
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Security for megaton 2.9.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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for megaton-2.9.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

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.

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