Track Clone and Visitor Statistics #40
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Track Clone and Visitor Statistics | |
| on: | |
| schedule: | |
| # Runs daily at midnight UTC | |
| - cron: '0 0 * * *' | |
| workflow_dispatch: # Allow manual trigger | |
| jobs: | |
| track-traffic: | |
| runs-on: ubuntu-latest | |
| env: | |
| FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true | |
| permissions: | |
| contents: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v5 | |
| - name: Fetch traffic data and append | |
| env: | |
| GH_TOKEN: ${{ secrets.TRAFFIC_TOKEN }} | |
| REPO: slimbootloader/slimbootloader | |
| run: | | |
| mkdir -p traffic-data | |
| echo "=== Fetching clone data from $REPO ===" | |
| HTTP_CODE=$(curl -s -o traffic-data/latest_clones.json -w "%{http_code}" \ | |
| -H "Authorization: token $GH_TOKEN" \ | |
| https://api.github.com/repos/$REPO/traffic/clones) | |
| echo "Clones API HTTP Response: $HTTP_CODE" | |
| cat traffic-data/latest_clones.json | |
| if [ "$HTTP_CODE" != "200" ]; then | |
| echo "::error::Failed to fetch clone data (HTTP $HTTP_CODE). Check token permissions." | |
| exit 1 | |
| fi | |
| echo "" | |
| echo "=== Fetching visitor/view data from $REPO ===" | |
| HTTP_CODE=$(curl -s -o traffic-data/latest_visitors.json -w "%{http_code}" \ | |
| -H "Authorization: token $GH_TOKEN" \ | |
| https://api.github.com/repos/$REPO/traffic/views) | |
| echo "Views API HTTP Response: $HTTP_CODE" | |
| cat traffic-data/latest_visitors.json | |
| if [ "$HTTP_CODE" != "200" ]; then | |
| echo "::error::Failed to fetch visitor data (HTTP $HTTP_CODE). Check token permissions." | |
| exit 1 | |
| fi | |
| echo "" | |
| echo "=== Fetching popular referral sources ===" | |
| curl -s -H "Authorization: token $GH_TOKEN" \ | |
| https://api.github.com/repos/$REPO/traffic/popular/referrers \ | |
| > traffic-data/latest_referrers.json | |
| echo "=== Fetching popular content/paths ===" | |
| curl -s -H "Authorization: token $GH_TOKEN" \ | |
| https://api.github.com/repos/$REPO/traffic/popular/paths \ | |
| > traffic-data/latest_popular_paths.json | |
| echo "" | |
| echo "=== Processing data ===" | |
| python3 - <<'PYEOF' | |
| import json | |
| import csv | |
| import os | |
| from datetime import datetime | |
| def append_traffic_data(json_file, csv_file, data_key, columns): | |
| """Append new daily entries from JSON API response to a CSV file.""" | |
| with open(json_file) as f: | |
| data = json.load(f) | |
| existing_dates = set() | |
| if os.path.exists(csv_file): | |
| with open(csv_file, "r") as f: | |
| reader = csv.DictReader(f) | |
| for row in reader: | |
| existing_dates.add(row["date"]) | |
| write_header = not os.path.exists(csv_file) or os.path.getsize(csv_file) == 0 | |
| new_entries = 0 | |
| with open(csv_file, "a", newline="") as f: | |
| writer = csv.writer(f) | |
| if write_header: | |
| writer.writerow(columns) | |
| for entry in data.get(data_key, []): | |
| date = entry["timestamp"][:10] | |
| if date not in existing_dates: | |
| writer.writerow([date, entry["count"], entry["uniques"]]) | |
| new_entries += 1 | |
| total = data.get("count", 0) | |
| uniques = data.get("uniques", 0) | |
| print(f" {csv_file}: {new_entries} new entries | Last 14d total: {total}, unique: {uniques}") | |
| # Process clones | |
| print("Processing clones...") | |
| append_traffic_data( | |
| "traffic-data/latest_clones.json", | |
| "traffic-data/clone_history.csv", | |
| "clones", | |
| ["date", "total_clones", "unique_cloners"] | |
| ) | |
| # Process visitors (views) | |
| print("Processing visitors...") | |
| append_traffic_data( | |
| "traffic-data/latest_visitors.json", | |
| "traffic-data/visitor_history.csv", | |
| "views", | |
| ["date", "total_views", "unique_visitors"] | |
| ) | |
| # Write summary | |
| now = datetime.utcnow().strftime("%Y-%m-%d %H:%M UTC") | |
| # Read latest totals | |
| with open("traffic-data/latest_clones.json") as f: | |
| clones = json.load(f) | |
| with open("traffic-data/latest_visitors.json") as f: | |
| visitors = json.load(f) | |
| with open("traffic-data/last_updated.txt", "w") as f: | |
| f.write(f"Last updated: {now}\n\n") | |
| f.write(f"Last 14 days summary:\n") | |
| f.write(f" Clones: {clones.get('count', 0)} total, {clones.get('uniques', 0)} unique\n") | |
| f.write(f" Visitors: {visitors.get('count', 0)} total, {visitors.get('uniques', 0)} unique\n") | |
| print(f"\nDone at {now}") | |
| print(f" Clones (14d): {clones.get('count', 0)} total, {clones.get('uniques', 0)} unique") | |
| print(f" Visitors (14d): {visitors.get('count', 0)} total, {visitors.get('uniques', 0)} unique") | |
| PYEOF | |
| - name: Commit and push | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git add traffic-data/ | |
| if git diff --cached --quiet; then | |
| echo "No new data to commit." | |
| else | |
| git commit -m "Update traffic statistics $(date -u +%Y-%m-%d)" | |
| git push | |
| fi |