Skip to main content

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

Release files for megaton 2.1.2

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.1.2
File Size Uploaded
megaton-2.1.2.tar.gz 176.3 kB Details

Built distribution (wheel)

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

Total release size: 289.9 kB

Release files / megaton-2.1.2.tar.gz

Download URL megaton-2.1.2.tar.gz
Size 176.3 kB
Tags Source
SHA-256 checksum
How to use checksums
e698435091b376e87bc6a9d3e1bf71ab6b3f2d67c6471e2edab5477e27936e59
BLAKE2b-256 checksum
How to use checksums
fca1860add4c4d806553ff1bc9f629bf8cfba5d7b4aba1f9f8cf01dece412404
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

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

Download URL megaton-2.1.2-py3-none-any.whl
Size 113.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6018dfa6f9735da6f7bfa8355b644b300dcc56ad5b9bb480ed91d313d4591f8a
BLAKE2b-256 checksum
How to use checksums
e5cea2ab444b7d3203b3b66b6f1267b5aa0bca97e6c85ac8ea92d116e9f17db5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release history Release notifications | RSS feed

2.2.0

2 release files

2.1.3

2 release files

This release

2.1.2 This release

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.5.0

2 release files

1.4.3

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

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