applytics_flutter 0.0.3
applytics_flutter: ^0.0.3 copied to clipboard
A powerful analytics package for Flutter apps inspired by PostHog. Track events, identify users, and send data to your own backend with automatic batching and session management.
Applytics Flutter #
A powerful Flutter analytics package inspired by PostHog. Send analytics events to your own backend with support for batching, session management, and automatic device information collection.
๐ Features #
- โ Simple and intuitive event tracking
- โ Automatic event batching
- โ Automatic session management
- โ User identification
- โ Queue with periodic flushing
- โ Debug mode support
- โ Network error handling with retry
- โ API connection testing
- โ Singleton architecture for easy access
- โ Automatic device and app info collection (included)
๐ฆ Installation #
Add this dependency to your pubspec.yaml:
dependencies:
applytics_flutter: ^0.0.1
Then run:
flutter pub get
That's it! Device info packages are already included.
๐ฏ Basic Usage #
1. Initialization #
Initialize Applytics at app startup:
import 'package:applytics_flutter/applytics_flutter.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
ApplyticsClient.initialize(
config: AnalyticsConfig(
apiKey: 'your-project-api-key', // From your projects table
debug: true, // Enable debug logs
batchSize: 10, // Send events in batches of 10
flushIntervalSeconds: 30, // Or every 30 seconds
),
);
runApp(MyApp());
}
2. Track Events #
final analytics = ApplyticsClient.instance;
// Simple event
analytics.track('button_clicked');
// Event with properties
analytics.track('product_viewed', properties: {
'product_id': '123',
'product_name': 'iPhone 15',
'category': 'electronics',
'price': 999.99,
});
3. Identify a User #
analytics.identify('user_123', userProperties: {
'name': 'John Doe',
'email': 'john@example.com',
'plan': 'premium',
});
4. Reset (Logout) #
analytics.reset(); // Reset user and session
๐ง Advanced Configuration #
Configuration Options #
AnalyticsConfig(
apiKey: 'your-api-key', // Required
debug: false, // Enable debug logs
batchSize: 10, // Number of events before auto-send
flushIntervalSeconds: 30, // Send interval in seconds
timeoutSeconds: 10, // HTTP request timeout
enableSessionTracking: true, // Enable session tracking
)
Additional Features #
// Force immediate flush of all pending events
await analytics.flush();
// Get current queue size
int queueSize = analytics.queueSize;
// Get current session ID
String? sessionId = analytics.sessionId;
// Get current user ID
String? userId = analytics.userId;
// Test API connection
bool isConnected = await analytics.testConnection();
๐ก Data Format #
Events are sent in the following JSON format:
{
"events": [
{
"name": "button_clicked",
"properties": {
"button_name": "submit"
},
"created_at": "2025-12-01T10:30:00.000Z",
"user_id": "user_123",
"session_id": "1701425400000-1701425400000000"
}
]
}
Expected API Endpoints #
Your backend should expose these endpoints:
POST /events)GET /health- Check API health (optional)
๐งช Complete Example #
Check the example/ folder for a complete application demonstrating all features.
๐ Best Practices #
- Initialize once at application startup
- Use consistent event names (snake_case recommended)
- Add relevant properties for easier analysis
- Call
flush()before closing the app to avoid losing events - Enable debug mode during development
๐ License #
MIT License
๐ค Contributing #
Contributions are welcome! Feel free to open an issue or pull request.