A Discourse plugin that counts eligible API requests as topic views, allowing API traffic (mobile apps, partner integrations, etc.) to contribute to topic analytics just like regular web requests.
Compatible with Discourse 3.2.0+
- ✅ Tracks API requests as topic views
- ✅ Supports both API and User API requests
- ✅ Optional custom header requirement for fine-grained control
- ✅ Filters out crawlers and background requests
- ✅ User-aware tracking when authenticated
- ✅ Asynchronous job processing for performance
- ✅ Comprehensive test coverage
- Hooks directly into Discourse's
TopicsController#showaction usingafter_actioncallback - Identifies API requests by checking for API keys or User API keys in headers/params
- Validates request criteria (API request, 200 status, topic exists, etc.)
- Optionally checks for a required custom header
- Enqueues a background job to increment topic view count
- Tracks user visits for authenticated requests
All settings are configurable in the Admin Panel under Settings > Plugins > api-topic-views:
| Setting | Default | Description |
|---|---|---|
api_topic_views_enabled |
true |
Master switch to enable/disable the plugin |
api_topic_views_require_header |
"" |
Optional header name (e.g., X-Count-As-View) that must be present to count views. Leave empty to count all API requests |
api_topic_views_max_per_minute_per_ip |
0 |
Maximum views per IP per topic per minute. Set to 0 to disable rate limiting |
If you set api_topic_views_require_header to X-Count-As-View, your API clients must include this header:
curl -H "X-Count-As-View: true" \
-H "Api-Key: your_api_key" \
https://your-discourse.com/t/topic-slug/123.json-
Add to your
app.ymlin the plugins section:hooks: after_code: - exec: cd: $home/plugins cmd: - git clone https://github.com/gorfist/api-topic-view.git
-
Rebuild your container:
cd /var/discourse ./launcher rebuild app -
Enable the plugin in Admin Panel → Settings → Plugins
-
Clone into your Discourse plugins directory:
cd /path/to/discourse/plugins git clone https://github.com/gorfist/api-topic-view.git -
Run bundle install:
bundle install
-
Restart your development server
Run the test suite:
bundle exec rake plugin:spec['api-topic-views']- Discourse Version: 3.2.0 or higher
- Ruby: 3.3.0+
- Rails: 8.0+
See .discourse-compatibility file for version pinning details.
The plugin supports multiple languages:
- English (en) - Full support
- Persian/Farsi (fa_IR) - Full support
To add support for additional languages, create a file:
config/locales/server.{language_code}.yml
See LOCALE_FIX.md for details on adding new languages.
plugin.rb: Plugin initialization and configurationlib/api_topic_views/request_logger.rb: Request interceptor and validatorlib/api_topic_views/track_view_job.rb: Background job for view countingconfig/settings.yml: Plugin settings definitionsconfig/locales/server.en.yml: Localization strings
API Request → TopicsController#show
→ after_action: track_api_topic_view
→ Validate request criteria (API key present, 200 status, etc.)
→ Jobs.enqueue(:track_api_topic_view)
→ TrackApiTopicView job executes
→ Topic.views incremented (with rate limiting)
→ TopicUser.track_visit! (if authenticated)
Quick diagnostic steps:
-
Verify plugin is enabled: Admin → Settings → Plugins →
api_topic_views_enabled= true -
Check if you're making API requests: You MUST include proper API authentication headers:
Api-Key: your_keyANDApi-Username: system, ORUser-Api-Key: your_user_key
⚠️ Regular session cookies don't count as API requests! -
Verify the request returns 200 status (not 301/302 redirect)
-
Check the URL pattern: Must be
/t/:id.jsonor/t/:slug/:id.json -
If using custom header, ensure it's being sent with the correct name
Enable debug logging:
Set environment variable in your app.yml:
env:
API_TOPIC_VIEWS_DEBUG: 'true'Then rebuild and check logs:
./launcher rebuild app
./launcher logs app | grep api-topic-viewsRun the test script:
Access your Rails console and run the included test script:
# Docker
./launcher enter app
rails c
# Then in console
load 'plugins/api-topic-view/TEST_SCRIPT.rb'This will check:
- ✓ Plugin is loaded
- ✓ Callbacks are registered
- ✓ Settings are correct
- ✓ Provide a test curl command
Check detailed diagnostics:
See DEBUG.md for comprehensive troubleshooting steps.
| Issue | Symptom | Solution |
|---|---|---|
| Not using API auth | Views not counting | Include Api-Key + Api-Username headers |
| Wrong URL format | No jobs queued | Use /t/123.json not /t/123/ |
| Custom header missing | Jobs not created | Check api_topic_views_require_header setting |
| Plugin disabled | Nothing happens | Enable in Admin → Settings → Plugins |
| Jobs not processing | Jobs queued but views don't increase | Restart Sidekiq: ./launcher restart app |
Ensure your config/settings.yml follows the correct format. Settings should be flat under the plugins: key, not nested under a sub-key.
- Fork the repository
- Create a feature branch
- Write tests for your changes
- Ensure all tests pass
- Submit a pull request
MIT License - See repository for details
- GitHub Issues: https://github.com/gorfist/api-topic-view/issues
- Discourse Meta: https://meta.discourse.org/
- 🔧 BREAKING CHANGE: Switched from middleware hooks to controller hooks for more reliable tracking
- ✨ Implemented direct
TopicsControllerintegration usingafter_actioncallback - ✨ Added proper rate limiting functionality (was placeholder before)
- ✨ Added bot detection - skips tracking for bot users
- ✨ Added deleted topic check before incrementing views
- 🐛 Fixed view counting that wasn't working with middleware approach
- ⚡ Improved performance by using
update_allfor atomic view increments - 📝 Updated documentation to reflect new architecture
Migration Note: This version uses a completely different tracking method. If you were using the previous version and it wasn't working, this should fix it!
- 🐛 Enhanced debugging capabilities with detailed logging
- 📝 Added comprehensive troubleshooting documentation
- ✨ Added test script (TEST_SCRIPT.rb) for easy diagnostics
- ✨ Added debugging guide (DEBUG.md) and quick fix guide (QUICK_FIX.md)
- ✨ Added test-api-request.sh script for testing API calls
- 🔧 Improved error messages and logging in RequestLogger
- 🔧 Added view count logging in TrackApiTopicView job
- 📝 Enhanced README with common issues table
- ✨ Modernized plugin structure for Discourse 3.2+
- ✨ Added background job processing for better performance
- ✨ Comprehensive test coverage
- ✨ Added
.discourse-compatibilityfile - 🐛 Fixed settings.yml structure issue
- 📝 Improved documentation
- 🔧 Cleaner error handling
- Initial release
- Basic API request tracking