The Swetrix Python SDK is a client library designed to interact with the Swetrix API. This SDK provides easy-to-use methods for tracking and analyzing various events, user interactions, and metrics within your Python applications.
- Simple integration with Swetrix API.
- Track various events and user interactions.
- Collect and analyze custom metrics.
- Lightweight and easy to use.
You can install the SDK using pip:
pip install swetrix-sdk
Below is a basic example of how to use the Swetrix Python SDK to send events to Swetrix.
# In your .env file or environment
SWETRIX_PROJECT_ID=your_project_id
SWETRIX_API_KEY=your_api_key
Alternatively, you can pass these values directly to the Swetrix
instance.
Initialize the Swetrix client with your PROJECT_ID
and API_KEY
.
from swetrix_sdk import Swetrix
from enums import CustomEventType, PageViewOption
# Initialize Swetrix instance
swetrix = Swetrix(
project_id='your_project_id',
api_key='your_api_key',
enable_logging=True # Enable logging for debugging
)
Use the track_event
method to track any custom event. Here's an example where we track a "signup" event:
event_data = {
PageViewOption.SOURCE: "homepage",
PageViewOption.METHOD: "google"
}
swetrix.track_event(
ip="192.168.0.1",
user_agent="Mozilla/5.0",
event_type=CustomEventType.SIGNUP,
event_options=event_data
)
You can easily track page views by providing the necessary options like URL
and User ID
.
page_view_data = {
PageViewOption.URL: "/homepage",
PageViewOption.USER_ID: "user123"
}
swetrix.track_page_view(
ip="192.168.0.1",
user_agent="Mozilla/5.0",
page_view_options=page_view_data
)
The SDK provides decorators that automatically track events or page views whenever a function is called. This is useful for tracking specific function executions, like API endpoints.
To automatically track an event when a function is executed, use the track_event_decorator
:
@swetrix.track_event_decorator(
ip="192.168.0.1",
user_agent="Mozilla/5.0",
event_type=CustomEventType.LOGIN,
event_options={PageViewOption.SOURCE: "login_page"}
)
def user_login(username: str, password: str):
return "User logged in"
# Call the decorated function
user_login("Yehor", "Python")
Similarly, you can use the track_page_view_decorator
to track page views when a function is executed.
@swetrix.track_page_view_decorator(
ip="192.168.0.1",
user_agent="Mozilla/5.0",
page_view_options={PageViewOption.URL: "/profile", PageViewOption.USER_ID: "user123"}
)
def load_profile():
return "Profile loaded"
load_profile()
project_id
: The project ID for Swetrix.api_key
: The API key for Swetrix.options
: Additional configuration options for custom API URLs.enable_logging
: Boolean flag to enable or disable logging.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Make your changes.
- Submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for more details.
For any issues or support requests, please open an issue on the GitHub Issues page.