A lightweight, platform-agnostic client for the Swetrix Events API.
Use it to track page views, custom events, heartbeats, and application errors from any Flutter or Dart application—no platform channels required.
- HTTP client built on top of
package:httpwith no runtime platform code - Automatic enrichment of events with OS, OS version, locale, country, device type, browser (web), app version & build number
- Visitor identifier persisted across sessions to keep repeat users deduplicated
- Automatically injects
User-AgentandX-Client-IP-Addressheaders (configurable resolver) so Swetrix can identify unique visitors reliably - Error tracking support aligned with the Swetrix dashboard
- Heartbeat scheduler to keep live visitor counters up to date
- Works with the production cloud API or self-hosted deployments
Add the package to your Flutter (or pure Dart) project:
dependencies:
swetrix_flutter: ">=0.1.0"or
dependencies:
swetrix_flutter:
git:
url: https://github.com/ehwplus/swetrix_flutter.gitThen install dependencies:
flutter pub getimport 'package:swetrix_flutter/swetrix_flutter.dart';
final swetrix = SwetrixFlutterClient(
projectId: 'YOUR_PROJECT_ID',
options: SwetrixOptions(apiUrl: Uri.parse('https://analytics.api.ehwplus.com/log')),
);
Future<void> trackLaunch() async {
await swetrix.trackPageView(
page: '/home',
context: SwetrixContext(metadata: {'uiMode': brightness.name}),
);
await swetrix.trackEvent(
'AppLaunch',
metadata: const {'build': 42, 'channel': 'stable'},
);
}
Future<void> reportError(Object error, StackTrace stack) async {
await swetrix.trackError(
SwetrixErrorEvent(
name: error.runtimeType.toString(),
message: error.toString(),
stackTrace: stack.toString(),
page: '/home',
),
);
}The SwetrixFlutterClient automatically:
- Collects OS, OS version, device classification (mobile/tablet/desktop), language, country and—on web builds—the active browser name.
- Pulls the app version and build number via
package_info_plus. - Persists a per-project visitor identifier in
SharedPreferencesso every user counts only once. - Adds the visitor ID and device metadata to all page views, events, and error payloads, while marking the first page view as
unique.
Important: When using the lower-level
Swetrixclient directly you must provide accurateUser-AgentandX-Client-IP-Addressheaders yourself to keep unique visitor metrics meaningful. See the Events API reference for full details.
By default the Flutter helper performs a single request to https://api.ipify.org to determine the public IP address. You can supply your own resolver if you prefer a different service.
- Self-hosted API – Override the endpoint via
SwetrixOptions(apiUrl: Uri.parse('https://your-host/log')). - Additional metadata – Supply
contextormetadataoverrides when callingtrackPageView/trackEventto extend the automatically collected fields. - Heartbeats – Use
startHeartbeat()/stopHeartbeat()to keep live visitor counters fresh. - Custom headers per call – Supply
SwetrixRequestOptionswhen sending individual events. - Custom client IP logic – Pass
clientIpResolverwhen constructingSwetrixFlutterClientto plug in your own IP detection (e.g. hitting an on-premise endpoint). - Custom user agent – Provide the
userAgentparameter if you prefer to send a hand-crafted header instead of the generated one.
A runnable Flutter example that wires everything together lives under example/.
Clone the repository and execute:
cd example
flutter run- Swetrix JS SDK (feature parity reference): https://github.com/Swetrix/swetrix-js
- Events API reference: https://docs.swetrix.com/events-api
- Swetrix integrations overview: https://docs.swetrix.com/integrations
Bug reports and feature requests are welcome via the issue tracker.