Skip to content

feat: encode data in url instead of json, use for-the-badge style format #8

feat: encode data in url instead of json, use for-the-badge style format

feat: encode data in url instead of json, use for-the-badge style format #8

Workflow file for this run

name: Update Analytics Badge
on:
schedule:
- cron: '0 2 * * 1' # Every Monday at 2 AM UTC
workflow_dispatch: # Allow manual triggering with inputs
inputs:
force_update:
description: 'Force update even if no changes detected'
required: false
default: false
type: boolean
custom_message:
description: 'Custom commit message (optional)'
required: false
type: string
push: # Trigger on push to any branch
branches: ['**']
jobs:
update-badge:
runs-on: ubuntu-latest
permissions:
contents: write # Needed to commit changes
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '18'
- name: Fetch Plausible Analytics Data
id: analytics
run: |
echo "=== DEBUG INFORMATION ==="
echo "Checking if API key is set..."
if [ -z "${{ secrets.PLAUSIBLE_API_KEY }}" ]; then
echo "❌ PLAUSIBLE_API_KEY is not set in repository secrets!"
echo "Using fallback values..."
pageviews="Loading..."
visitors="Loading..."
formatted_pageviews="Loading..."
formatted_visitors="Loading..."
else
echo "✅ PLAUSIBLE_API_KEY is set"
# Fetch both pageviews and visitors from Plausible API
response=$(curl -s -X POST https://plausible.io/api/v2/query \
-H "Authorization: Bearer ${{ secrets.PLAUSIBLE_API_KEY }}" \
-H "Content-Type: application/json" \
-d '{
"site_id": "openwashdata.org",
"metrics": ["pageviews", "visitors"],
"date_range": "all"
}')
echo "API Response: $response"
# Check if response contains error
if echo "$response" | jq -e '.error' > /dev/null 2>&1; then
echo "API Error: $(echo "$response" | jq -r '.error')"
pageviews="Error"
visitors="Error"
formatted_pageviews="Error"
formatted_visitors="Error"
else
# The response structure is: {"results":[{"metrics":[pageviews, visitors]}]}
# Extract pageview and visitor counts from the metrics array
pageviews=$(echo $response | jq -r '.results[0].metrics[0] // 0')
visitors=$(echo $response | jq -r '.results[0].metrics[1] // 0')
echo "Extracted pageviews: $pageviews"
echo "Extracted visitors: $visitors"
# Format numbers with commas for readability
if [ "$pageviews" != "0" ] && [ "$pageviews" != "null" ]; then
formatted_pageviews=$(echo $pageviews | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta')
else
formatted_pageviews="0"
fi
if [ "$visitors" != "0" ] && [ "$visitors" != "null" ]; then
formatted_visitors=$(echo $visitors | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta')
else
formatted_visitors="0"
fi
fi
fi
echo "Final values - Pageviews: $formatted_pageviews, Visitors: $formatted_visitors"
echo "pageviews=$pageviews" >> $GITHUB_OUTPUT
echo "formatted_pageviews=$formatted_pageviews" >> $GITHUB_OUTPUT
echo "visitors=$visitors" >> $GITHUB_OUTPUT
echo "formatted_visitors=$formatted_visitors" >> $GITHUB_OUTPUT
- name: Update README Badges
run: |
echo "Updating README with dynamic badge URLs..."
# Get values from previous step
pageviews_value="${{ steps.analytics.outputs.formatted_pageviews }}"
visitors_value="${{ steps.analytics.outputs.formatted_visitors }}"
echo "Using pageviews: $pageviews_value"
echo "Using visitors: $visitors_value"
# URL encode the values (replace spaces and special chars)
pageviews_encoded=$(echo "$pageviews_value" | sed 's/ /%20/g')
visitors_encoded=$(echo "$visitors_value" | sed 's/ /%20/g')
# Update the README.md with new badge URLs (for-the-badge style)
# Look for existing badge lines and replace them, or add them if not found
if grep -q "Total%20Views" README.md; then
# Replace existing pageviews badge
sed -i "s|!\[Total Views\].*|![Total Views](https://img.shields.io/badge/Total%20Views-${pageviews_encoded}-2d0e2d?style=for-the-badge)|" README.md
else
# Add pageviews badge if not found (after # OpenWashData Website line)
sed -i '/# OpenWashData Website/a\\n![Total Views](https://img.shields.io/badge/Total%20Views-'"${pageviews_encoded}"'-2d0e2d?style=for-the-badge)' README.md
fi
if grep -q "Unique%20Visitors" README.md; then
# Replace existing visitors badge
sed -i "s|!\[Unique Visitors\].*|![Unique Visitors](https://img.shields.io/badge/Unique%20Visitors-${visitors_encoded}-c8a3c8?style=for-the-badge)|" README.md
else
# Add visitors badge if not found (after pageviews badge)
sed -i '/Total%20Views.*-2d0e2d/a![Unique Visitors](https://img.shields.io/badge/Unique%20Visitors-'"${visitors_encoded}"'-c8a3c8?style=for-the-badge)' README.md
fi
echo "README updated with new badge URLs"
echo "Updated badges:"
grep -E "!\[(Total Views|Unique Visitors)\]" README.md || echo "No badges found in README"
- name: Commit and Push Changes
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add README.md
# Use custom commit message if provided, otherwise use default
if [ -n "${{ github.event.inputs.custom_message }}" ]; then
commit_msg="${{ github.event.inputs.custom_message }}"
else
commit_msg="Update analytics badges: ${{ steps.analytics.outputs.formatted_pageviews }} views, ${{ steps.analytics.outputs.formatted_visitors }} visitors"
fi
# Force commit if requested, otherwise only commit if changes exist
if [ "${{ github.event.inputs.force_update }}" == "true" ]; then
git commit -m "$commit_msg" --allow-empty
else
git diff --staged --quiet || git commit -m "$commit_msg"
fi
git push