fix: plausible API fetching error #2
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: 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: | | |
# Fetch both pageviews and visitors from Plausible API (as per official docs) | |
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')" | |
exit 1 | |
fi | |
# The response for multiple metrics should be in results[0] with both values | |
# Let's see the actual structure first | |
echo "Results structure: $(echo "$response" | jq '.results[0]')" | |
# Extract pageview and visitor counts | |
pageviews=$(echo $response | jq -r '.results[0].pageviews // 0') | |
visitors=$(echo $response | jq -r '.results[0].visitors // 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="Loading..." | |
fi | |
if [ "$visitors" != "0" ] && [ "$visitors" != "null" ]; then | |
formatted_visitors=$(echo $visitors | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta') | |
else | |
formatted_visitors="Loading..." | |
fi | |
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: Create Badge Data | |
run: | | |
# Create badge data JSON file | |
cat > badge-data.json << EOF | |
{ | |
"schemaVersion": 1, | |
"label": "Total Views", | |
"message": "${{ steps.analytics.outputs.formatted_pageviews }}", | |
"color": "brightgreen", | |
"cacheSeconds": 86400 | |
} | |
EOF | |
- name: Update README Badge | |
run: | | |
# Update README.md with current timestamp | |
sed -i "s/Updated: .*/Updated: $(date +'%Y-%m-%d')/" README.md | |
- name: Commit and Push Changes | |
run: | | |
git config --local user.email "[email protected]" | |
git config --local user.name "GitHub Action" | |
git add badge-data-pageviews.json badge-data-visitors.json 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 |