How to keep GitHub traffic data longer than 14 days
GitHub shows repository views and clones for the last 14 days only. This guide explains how to keep them for as long as you want, with free tools.
Key takeaways
- GitHub keeps 14 days of views and clones and the top 10 referrers and paths; older data is gone.
- You need push access and a token; save each day by date (upsert), never append.
- A scheduled GitHub Actions workflow plus a JSON file in the repository is enough, at no cost.
What GitHub keeps
The Insights → Traffic page and the traffic REST endpoints return the previous 14 days of views and clones, plus the top 10 referring sites and the top 10 popular paths. Anything older is no longer available, so history has to be saved by you.
Step 1: get a token
Only people with push access can read traffic. A classic personal access token with the repo scope works; for a fine-grained token, GitHub documents read access to the repository's Administration permission for these endpoints. Store it as a secret (for Actions: GH_PAT), never in a web page or in code.
Step 2: read the endpoints
GET /repos/{owner}/{repo}/traffic/views: total and unique views per dayGET /repos/{owner}/{repo}/traffic/clones: total and unique clones per dayGET /repos/{owner}/{repo}/traffic/popular/referrers: top 10 referring sitesGET /repos/{owner}/{repo}/traffic/popular/paths: top 10 popular paths
Step 3: merge by day, do not append
Each call returns a rolling window, and GitHub can revise recent days for 24-48 hours. Save each day under its date and overwrite it on the next run (an upsert). This keeps history correct and lets you fetch less often than daily without losing data, as long as you run at least every 14 days.
import json, os, requests
TOKEN = os.environ["GH_PAT"] # token with push access to the repository
REPO = "owner/repo"
H = {"Authorization": f"Bearer {TOKEN}", "Accept": "application/vnd.github+json"}
def fetch(kind): # kind: "views" or "clones"
r = requests.get(f"https://api.github.com/repos/{REPO}/traffic/{kind}", headers=H, timeout=30)
r.raise_for_status()
return r.json()[kind] # [{"timestamp", "count", "uniques"}, ...]
history = json.load(open("traffic.json")) if os.path.exists("traffic.json") else {"views": {}, "clones": {}}
for kind in ("views", "clones"):
for row in fetch(kind):
history[kind][row["timestamp"][:10]] = [row["count"], row["uniques"]] # upsert by day
json.dump(history, open("traffic.json", "w"), indent=1, sort_keys=True)
Step 4: run it on a schedule
A scheduled GitHub Actions workflow can run the script daily and commit the JSON file back to the repository.
name: Save traffic
on:
schedule:
- cron: "17 2 * * *"
workflow_dispatch:
permissions:
contents: write
jobs:
save:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with: {python-version: "3.12"}
- run: pip install requests && python save_traffic.py
env:
GH_PAT: ${{ secrets.GH_PAT }}
- run: |
git config user.name "traffic-bot"
git config user.email "traffic-bot@users.noreply.github.com"
git add traffic.json
git diff --cached --quiet || git commit -m "traffic: $(date -u +%F)"
git push
Step 5: stay inside rate limits
Authenticated REST requests have an hourly allowance (see GitHub's rate limit documentation). Four calls per repository per run is small for a handful of repositories, but hundreds of repositories need care: call sequentially, check the x-ratelimit-remaining header, and fetch idle repositories less often.
Skip the work: use this dashboard
This open-source project does all of the above for every public repository of an account and adds charts, rankings and exports. Fork musman550/github-traffic-analytics-dashboard, add the GH_PAT secret, enable GitHub Pages on the docs folder and run the workflow. See the methodology for details.