The django-smalk-tracker
is a Django package that provides server-side tracking for Smalk.ai. It includes a middleware that automatically tracks page views and user interactions, sending the data to your Smalk analytics service.
- Easy integration with Django projects
- Configurable URL patterns for tracking
- Lightweight and performant
- Asynchronous tracking requests
- Comprehensive error handling
- Support for Django 3.2+ and Python 3.7+
Install using pip:
pip install django-smalk-tracker
- Add
'smalk_tracker'
to yourINSTALLED_APPS
setting:
INSTALLED_APPS = [
# ...
'smalk_tracker',
]
- Add the middleware to your
MIDDLEWARE
setting:
MIDDLEWARE = [
# ...
'smalk_tracker.middleware.SmalkServerSideTrackingMiddleware',
# ...
]
- Add your Smalk API key to
settings.py
: Generate the API key in your Smalk dashboard: https://app.smalk.ai/settings/api-keys
# Required
SMALK_PROJECT_API_KEY = "your-api-key-here"
SMALK_PROJECT_API_KEY
(str): Your Smalk project API key. Get this from your Smalk dashboard.
SMALK_DISABLE_TRACKING
(bool): Set toTrue
to disable all tracking (default:False
)SMALK_TRACKING_ALLOWED_PATH
(list): List of URL paths to track (if not set, all paths are tracked)SMALK_TRACKING_EXCLUDE_URLS_REGEXES
(list): List of regex patterns to exclude from trackingSMALK_DEFAULT_TIMEOUT
(float): Request timeout in seconds (default:0.15
)SMALK_VISIT_URL
(str): Override the default tracking endpoint URLSMALK_BOT_USER_AGENT
(str): User agent string for the tracking requests
By default, the following paths are excluded from tracking:
/api/*
- All API endpoints/admin/*
- Django admin interface/health/
- Health check endpoints*.js, *.css, *.png, *.jpg, *.jpeg, *.gif, *.ico
- Static files
# settings.py
# Required
SMALK_PROJECT_API_KEY = "your-api-key-here"
# Optional configuration
SMALK_DISABLE_TRACKING = False # Set to True to disable tracking
SMALK_DEFAULT_TIMEOUT = 0.2 # 200ms
# Only track these paths (if not set, all paths are tracked)
SMALK_TRACKING_ALLOWED_PATH = [
"/blog/",
"/products/"
]
# Exclude these URL patterns from tracking
SMALK_TRACKING_EXCLUDE_URLS_REGEXES = [
r"^/api/.*$",
r"^/admin/.*$",
r"^/private/.*$",
r"^/health/.*$"
]
# Customize the tracking endpoint (optional)
# SMALK_VISIT_URL = "https://your-custom-endpoint.example.com/track"
# Customize the user agent (optional)
# SMALK_BOT_USER_AGENT = "YourApp/1.0"
-
For each incoming request, the middleware checks if tracking should be performed based on:
- If tracking is disabled via
SMALK_DISABLE_TRACKING
- If the request path matches any allowed paths (if
SMALK_TRACKING_ALLOWED_PATH
is set) - If the request path doesn't match any excluded patterns
- If the request is for a static file (images, CSS, JS, etc.)
- If tracking is disabled via
-
If tracking is enabled for the request, it sends the following data to the Smalk tracking endpoint:
- Request path
- Request method
- Request headers
- Timestamp
-
The tracking request is made asynchronously with a configurable timeout to minimize impact on response times.
The middleware includes comprehensive error handling:
- Failed tracking requests are logged but don't affect the user experience
- Invalid configurations are logged at startup
- Network timeouts are handled gracefully
- All exceptions are caught and logged to prevent affecting the main application
-
Performance:
- Keep the
SMALK_DEFAULT_TIMEOUT
low (100-300ms) to minimize impact on page load times - Use
SMALK_TRACKING_ALLOWED_PATH
to limit tracking to specific paths when possible
- Keep the
-
Security:
- Be cautious when including sensitive information in request headers
- Review the default exclusions and add any additional paths that might contain sensitive information
-
Development:
- Set
SMALK_DISABLE_TRACKING = True
in your development settings - Monitor your logs for any tracking-related errors
- Set
-
Production:
- Ensure
SMALK_PROJECT_API_KEY
is stored securely (e.g., environment variables) - Monitor the performance impact of tracking on your application
- Ensure
- Check if
SMALK_DISABLE_TRACKING
isFalse
- Verify
SMALK_PROJECT_API_KEY
is set correctly - Check if the request path is included in
SMALK_TRACKING_ALLOWED_PATH
(if set) - Look for any error messages in your application logs
- Ensure the request path doesn't match any patterns in
SMALK_TRACKING_EXCLUDE_URLS_REGEXES
- Reduce the
SMALK_DEFAULT_TIMEOUT
value - Check your network connection to the Smalk API
- Consider using a CDN or proxy if the Smalk API is geographically distant
To contribute to this project:
- Fork the repository
- Create a virtual environment
- Install development dependencies:
pip install -e .[dev]
- Run tests:
python -m pytest
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
For support, please open an issue on the GitHub repository or contact support@smalk.ai.