Skip to content

Archive GitHub Traffic Stats #184

Archive GitHub Traffic Stats

Archive GitHub Traffic Stats #184

name: Archive GitHub Traffic Stats
# GitHub Traffic API only retains the last 14 days.
# This workflow runs daily and appends data to analytics/traffic/*.jsonl
# so we build a permanent historical record.
on:
schedule:
- cron: '0 3 * * *' # 03:00 UTC every day
workflow_dispatch: # allow manual trigger for testing
jobs:
archive:
runs-on: ubuntu-latest
permissions:
contents: write # needed to push the commit
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Fetch and append traffic stats
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -euo pipefail
mkdir -p analytics/traffic
DATE=$(date -u +%Y-%m-%dT%H:%M:%SZ)
REPO="antbald/ha-ev-smart-charger"
fetch() {
curl -sf \
-H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/${REPO}/traffic/$1"
}
# Clones (unique cloners + total)
fetch clones \
| jq --arg d "$DATE" '. + {fetched_at: $d}' \
>> analytics/traffic/clones.jsonl
# Page views (unique visitors + total)
fetch views \
| jq --arg d "$DATE" '. + {fetched_at: $d}' \
>> analytics/traffic/views.jsonl
# Top referrers
fetch popular/referrers \
| jq --arg d "$DATE" '{referrers: ., fetched_at: $d}' \
>> analytics/traffic/referrers.jsonl
# Top paths
fetch popular/paths \
| jq --arg d "$DATE" '{paths: ., fetched_at: $d}' \
>> analytics/traffic/paths.jsonl
echo "Traffic stats fetched at $DATE"
- name: Commit stats
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add analytics/
# Only commit if there are actual changes
git diff --staged --quiet \
|| git commit -m "chore: archive traffic stats $(date -u +%Y-%m-%d)" \
&& git push