Skip to content

Track clones & downloads #4

Track clones & downloads

Track clones & downloads #4

Workflow file for this run

name: Track clones & downloads
on:
schedule: [{ cron: "12 9 * * *" }] # daily 09:12 UTC (~04:12 America/Chicago)
workflow_dispatch:
permissions:
contents: write
actions: read
id-token: write
jobs:
track:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install jq
run: sudo apt-get update && sudo apt-get install -y jq
# ---------- CLONE STATS (14-day window, accumulated to CSV) ----------
- name: Fetch last 14 days of clone data
env:
REPO: ${{ github.repository }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
curl -s -H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/$REPO/traffic/clones?per=day" > clones.json
- name: Merge clones into CSV
run: |
mkdir -p analytics
CSV="analytics/clone_history.csv"
[ -f "$CSV" ] || echo "date,clones,unique_cloners" > "$CSV"
# existing dates to avoid dupes
awk -F, 'NR>1 {print $1}' "$CSV" | sort -u > .existing_clone_dates.txt || true
jq -r '.clones[] | "\(.timestamp[0:10]),\(.count),\(.uniques)"' clones.json \
| while IFS= read -r line; do
d="${line%%,*}"
if ! grep -qx "$d" .existing_clone_dates.txt; then
echo "$line" >> "$CSV"
fi
done
# sort by date (keep header)
{ head -n1 "$CSV"; tail -n +2 "$CSV" | sort; } > "$CSV.sorted"
mv "$CSV.sorted" "$CSV"
# ---------- RELEASE DOWNLOADS (cumulative snapshot per day) ----------
- name: Fetch all releases (paginate)
env:
REPO: ${{ github.repository }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -e
tmpdir=$(mktemp -d)
page=1
> "$tmpdir/all.json"
while :; do
resp=$(curl -s -H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/$REPO/releases?per_page=100&page=$page")
count=$(echo "$resp" | jq 'length')
if [ "$count" -eq 0 ]; then break; fi
echo "$resp" | jq '.[]' >> "$tmpdir/all.json"
page=$((page+1))
done
# Wrap into an array if we added anything
if [ -s "$tmpdir/all.json" ]; then
echo "[" > releases.json
# join with commas
sed -e '$!s/$/,/' "$tmpdir/all.json" >> releases.json
echo "]" >> releases.json
else
echo "[]" > releases.json
fi
- name: Merge downloads into CSVs
run: |
mkdir -p analytics
SNAPCSV="analytics/downloads_snapshot.csv"
TOTCSV="analytics/downloads_totals.csv"
today=$(date -u +%F)
# Create CSVs if missing
[ -f "$SNAPCSV" ] || echo "date,release_tag,asset_id,asset_name,download_count" > "$SNAPCSV"
[ -f "$TOTCSV" ] || echo "date,total_downloads" > "$TOTCSV"
# Avoid duplicating today's snapshot rows
# (filter if today's rows already recorded)
if grep -q "^$today," "$SNAPCSV"; then
echo "Download snapshot for $today already exists; skipping append."
else
# Snapshot rows: one per asset
jq -r '
.[]
| select(.draft==false)
| .tag_name as $tag
| (.assets // [])
| .[]
| [$tag, .id, .name, (.download_count // 0)]
| @csv
' releases.json \
| sed 's/^/'"$today"',/' >> "$SNAPCSV"
fi
# Totals row: sum all asset download_count across all releases
if ! grep -q "^$today," "$TOTCSV"; then
total=$(jq '[ .[] | select(.draft==false) | (.assets // []) | .[] | (.download_count // 0) ] | add // 0' releases.json)
echo "$today,$total" >> "$TOTCSV"
fi
# ---------- COMMIT ----------
- name: Commit changes
run: |
if git diff --quiet; then
echo "No changes."
else
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add analytics/clone_history.csv analytics/downloads_snapshot.csv analytics/downloads_totals.csv
git commit -m "Update clone & download history"
git push
fi