flutter_estatisticas 0.1.0
flutter_estatisticas: ^0.1.0 copied to clipboard
A Flutter package for integrating Umami Analytics into your apps. Track navigation and custom events easily, providing privacy-friendly analytics for your users.
Flutter Umami Analytics Integration #
This package integrates Umami Analytics into your Flutter app, allowing navigation tracking and sending custom events.
Installation #
Add to your pubspec.yaml:
dependencies:
flutter_estatisticas: ^0.1.0
Usage #
1. Import the main Umami service #
import 'package:flutter_estatisticas/umami_service.dart';
import 'package:flutter_estatisticas/umami_navigation_observer.dart';
2. Instantiate the Umami service #
final umamiService = UmamiService(
endpoint: 'https://michelmelo.pt', // Your Umami endpoint
website: 'id-website', // Your website ID in Umami
hostname: 'seusite.com.pt', // Your app's hostname
);
3. Add the observer to your MaterialApp #
MaterialApp(
// ... other parameters ...
navigatorObservers: [
UmamiNavigationObserver(umamiService),
],
)
4. Send custom events #
Simple click event
umamiService.enviarClique(
context,
'Page Title', // Page or event title
'spotify', // Name of the clicked widget
);
Custom event
umamiService.enviarEvento(
context,
title: 'Custom Page Title',
name: 'custom_event',
data: {
'key': 'value',
'foo': 'bar',
},
);
5. Advanced usage: Umami REST Services #
5.1. Import the services #
import 'package:flutter_estatisticas/services/umami_auth_service.dart';
import 'package:flutter_estatisticas/services/umami_user_service.dart';
import 'package:flutter_estatisticas/services/umami_team_service.dart';
import 'package:flutter_estatisticas/services/umami_website_service.dart';
import 'package:flutter_estatisticas/services/umami_event_service.dart';
import 'package:flutter_estatisticas/services/umami_session_service.dart';
5.2. Instantiate the services #
final authService = UmamiAuthService(endpoint: 'https://your-umami-endpoint.com');
final userService = UmamiUserService(endpoint: 'https://your-umami-endpoint.com');
final teamService = UmamiTeamService(endpoint: 'https://your-umami-endpoint.com');
final websiteService = UmamiWebsiteService(endpoint: 'https://your-umami-endpoint.com');
final eventService = UmamiEventService(endpoint: 'https://your-umami-endpoint.com');
final sessionService = UmamiSessionService(endpoint: 'https://your-umami-endpoint.com');
5.3. Authentication #
final token = await authService.login(
username: 'your-username',
password: 'your-password',
);
if (token != null) {
// Use this token in authenticated requests
print('Token: $token');
}
Verify the token
final isValid = await authService.verifyToken(token);
if (isValid) {
print('Token is valid!');
}
Note: For Umami Cloud, use an API key instead of username/password.
5.4. Example usage of other services #
// List all users (admin)
final users = await userService.getAllUsers(token: token!);
// Create a website
final website = await websiteService.createWebsite(
token: token!,
domain: 'mywebsite.com',
name: 'My Website',
);
// Get website events
final events = await eventService.getWebsiteEvents(
token: token!,
websiteId: 'website-id',
startAt: 1725580800000,
endAt: 1725667199999,
);
// Get website sessions
final sessions = await sessionService.getWebsiteSessions(
token: token!,
websiteId: 'website-id',
startAt: 1725580800000,
endAt: 1725667199999,
);
6. Sending events without authentication (Umami /api/send) #
You can send pageviews and custom events directly to Umami or Umami Cloud using the /api/send endpoint, without authentication.
This is useful for public analytics or when you don't want to manage tokens.
6.1. Import the send service #
import 'package:flutter_estatisticas/services/umami_send_service.dart';
6.2. Instantiate the send service #
final sendService = UmamiSendService(
website: 'your-website-id',
hostname: 'your-hostname.com',
endpoint: 'https://cloud.umami.is', // or your self-hosted endpoint
);
6.3. Send a pageview #
await sendService.sendPageview(
context: context,
title: 'Home',
url: '/home',
);
6.4. Send a custom event #
await sendService.sendCustomEvent(
context: context,
title: 'Home',
url: '/home',
name: 'button_click',
data: {'button': 'subscribe'},
);
Note:
- No authentication is required for these requests.
- The service automatically sets the required User-Agent and gathers device/language info.
- For Umami Cloud, use
https://cloud.umami.isas the endpoint.
Notes #
- The
websiteparameter should be your site ID from the Umami dashboard. - The
hostnameparameter should be your app's domain or identifier. - The
endpointparameter should be your Umami server URL. - The observer will automatically track navigation events in your app.
- Use
enviarCliquefor simple click events andenviarEventofor custom events.
License #
MIT