← Back to all products
$29
Cohort Tool
Cohort analysis with user retention heatmaps, behavior segmentation, and time-series grouping.
JSONMarkdownPython
📄 Product Preview
Try the interactive reader and demo tools below, or get the full product with all content unlocked.
📖 Interactive Reader (Free Preview) ⚙ Try Demo Tools 📦 Download Free Sample📁 File Structure 9 files
cohort-tool/
├── LICENSE
├── README.md
├── examples/
│ └── sample_cohort_events.json
├── free-sample.zip
├── guide/
│ ├── 01_features.md
│ ├── 02_quick-start.md
│ └── 03_license.md
├── index.html
└── src/
└── cohort_tool.py
📖 Documentation Preview README excerpt
Cohort Tool
Cohort analysis: user retention heatmaps, behavior segmentation, and time-series grouping. Understand how user behavior changes over time.
Features
- Retention analysis — Track what percentage of users remain active over time
- Flexible cohorts — Group by month, week, or day
- Heatmap output — HTML retention heatmap with color-coded cells
- Average retention — See the overall retention curve across all cohorts
- JSON export — Machine-readable results for dashboards and further analysis
- CLI and library — Use from the command line or import as a module
Requirements
- Python 3.10+
- No external dependencies (stdlib only)
Quick Start
# Monthly cohort retention
python src/cohort_tool.py --events examples/sample_cohort_data.json --cohort-by month
# Weekly cohorts with HTML heatmap
python src/cohort_tool.py --events data.json --cohort-by week --html heatmap.html
# Export results
python src/cohort_tool.py --events data.json --output retention.json --periods 12
Output Example
================================================================================
COHORT RETENTION (MONTH)
Total users: 450
================================================================================
Cohort Size P0 P1 P2 P3 P4
--------------------------------------------------------
2026-01 80 100.0% 72.5% 58.8% 45.0% 38.8%
2026-02 95 100.0% 68.4% 52.6% 41.1%
2026-03 110 100.0% 70.9% 55.5%
2026-04 85 100.0% 65.9%
2026-05 80 100.0%
--------------------------------------------------------
Average 100.0% 69.4% 55.6% 43.0% 38.8%
License
MIT License — see LICENSE file.
📄 Code Sample .py preview
src/cohort_tool.py
#!/usr/bin/env python3
"""
Cohort Tool — Analytics Hub (DataNest)
Cohort analysis: user retention, behavior segmentation, and time-series
grouping. Understand how user behavior changes over time by grouping
users into cohorts based on their signup date or first action.
Usage:
python cohort_tool.py --events users.json --cohort-by month --metric retention
python cohort_tool.py --events data.csv --cohort-by week --output report.json
Dependencies: Python 3.10+ stdlib only (no pip packages)
License: MIT
"""
from __future__ import annotations
import argparse
import csv
import json
import logging
import sys
from collections import defaultdict
from dataclasses import dataclass, field
from datetime import datetime, timedelta, timezone
from pathlib import Path
from typing import Any
logger = logging.getLogger("cohort_tool")
# ---------------------------------------------------------------------------
# Data models
# ---------------------------------------------------------------------------
@dataclass
class CohortRow:
"""A single cohort's retention data across periods."""
cohort_label: str
cohort_size: int
retention: list[float] = field(default_factory=list) # % retained per period
active_counts: list[int] = field(default_factory=list) # absolute active per period
@dataclass
class CohortResult:
"""Complete cohort analysis result."""
cohorts: list[CohortRow] = field(default_factory=list)
periods: list[str] = field(default_factory=list) # Period labels
metric: str = "retention"
cohort_by: str = "month"
# ... 305 more lines ...