Flutter
Add the Tugboat SDK to your Flutter app.
Add Tugboat to a Flutter app that runs on Android and iOS. You'll need your app's SDK keys (one for Android, one for iOS). See SDK overview.
Requirements
- Flutter 3.35.0 or later, and Dart 3.9.2 or later
- Android 5.0 (API 21) or later
- iOS 15 or later
tugboat0.12.8 or later. Earlier versions could leave some content unmasked.
Tugboat records on Android and iOS only. On web and desktop, guard every Tugboat call so it doesn't run there.
Install
flutter pub add tugboatThere are no other native steps. Make sure your iOS deployment target is 15 or higher in your Podfile and Xcode project.
Start the SDK
Wrap your app with TugboatReplay.wrapApp and add the navigator observer. Create the collector settings with your SDK key before runApp; they're read once, when the wrapper mounts.
import 'package:flutter/material.dart';
import 'package:tugboat/tugboat.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
final collector = await TugboatCollectorHost.fromPlatform(
apiKey: tugboatSdkKey, // your app's SDK key for this platform
);
runApp(MyApp(collector: collector));
}
class MyApp extends StatelessWidget {
const MyApp({super.key, required this.collector});
final TugboatCollectorConfig collector;
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorObservers: [TugboatReplay.navigatorObserver],
builder: (context, child) => TugboatReplay.wrapApp(
child: child!,
config: TugboatReplayConfig(enabled: true, collector: collector),
),
home: const HomeScreen(),
);
}
}fromPlatform fills in your app and device details and uses Tugboat's address, https://collector.gettugboat.com, by default. It doesn't make a network call.
Turn capture on
Capture is off by default. Either:
- set
enabled: true, as above, to record from launch, or - leave it off and call
TugboatReplay.activate(activationRequestId: ...)when you want to start recording, andTugboatReplay.deactivate()to stop.
Name your screens
Tugboat names each screen after its route. Give your routes names, using patterns rather than real IDs:
Navigator.of(context).push(
MaterialPageRoute<void>(
settings: const RouteSettings(name: '/orders/detail'),
builder: (context) => const OrderDetailScreen(),
),
);- Dialogs and sheets: pass
routeSettings: const RouteSettings(name: '/orders/confirm-cancel')toshowDialogorshowModalBottomSheet. - Nested navigators: give each one its own observer from
TugboatReplay.createNavigatorObserver(). Create it once, not inbuild. - go_router: pages created with
builder:are named after the route. For pages created withpageBuilder:, passname: state.fullPathto the page. - Controls: wrap important controls in
TugboatTag('checkout', child: ...)so they keep a stable name.
Don't put personal data in route names or tags; they're sent with sessions.
Identify users
await TugboatReplay.setUserId('user-123');
await TugboatReplay.setTraits({'plan': 'pro'});setTraits replaces the previous traits. When someone signs out, call setUserId(null) and setTraits({}).
Capture network calls
With Dio: add tugboat_dio (same version as tugboat) and install the interceptor after your auth, retry and cache interceptors.
flutter pub add tugboat_dioimport 'package:tugboat_dio/tugboat_dio.dart';
TugboatDioInterceptor.install(
dio,
routeResolver: (request) => routeFor(request.uri.path), // e.g. '/orders/:id'
);The route resolver turns each request into the path Tugboat records. Return a pattern such as /orders/:id so IDs stay out of it, or null to skip the request. To limit which error response bodies are kept, pass errorBodyUrls or errorBodyDenyUrls; errorBodyUrls: const [] keeps none.
With package:http: add tugboat_http and wrap your client:
import 'package:http/http.dart' as http;
import 'package:tugboat_http/tugboat_http.dart';
final client = TugboatHttpClient(
http.Client(),
routeResolver: (url) => routeFor(url.path),
);By hand:
final call = TugboatReplay.beginNetworkCall(method: 'POST', route: '/projects');
call.complete(statusCode: 201);Use one of these per client, not several for the same requests.
Send events
Send named events from your code, for example from your analytics layer:
final events = TugboatReplay.eventHook(
source: 'analytics',
parameterPolicy: TugboatParameterPolicy.allowList({'method'}),
);
events.record('LOGIN', parameters: {'method': 'email'});The parameter policy decides which parameters are sent: allowAll (the default), namesOnly, allowList or transform. Use the narrowest one that works, because allowAll can send search terms, IDs or other user content.
Mark sensitive content
By default, all text and images are masked. Wrap anything else that could show private data:
TugboatSensitive(child: AccountNumber(number: account.number))To change how much is masked, set screenshotMaskLevel on TugboatReplayConfig. See Privacy and masking.
Verify your setup
- Run a debug build on a device with
--dart-define=TUGBOAT_HTTP_PROOF=true. - Use the app for a minute.
- Check the logs for
TUGBOAT_HTTPlines withpath=/v1/sessions,/v1/events/batchand/v1/frames, and status202. - Open Findings › Sessions › All sessions in Tugboat and find your session.
Pause or stop capture
TugboatReplay.deactivate()stops recording until you activate again.TugboatReplay.disabled = trueswitches Tugboat off for the whole process, for example from a remote config kill switch.
Troubleshooting
Nothing is recorded
Check that capture is on (enabled: true or a call to activate), that the collector settings were created before the app mounted, and that you're running on Android or iOS.
Screens have generic names
Name your routes with RouteSettings, and add an observer to every navigator. Unnamed routes are recorded by their Dart type, which isn't readable in obfuscated release builds.
The app feels slower while recording
Make sure only one session replay tool is recording at a time. Two recorders duplicate work and make scrolling less smooth.
Configuration options
These are the most common options. For every option, see TugboatReplayConfig in the API reference.
| Option | Default | Description |
|---|---|---|
enabled | false | Start recording when the app starts. |
collector | none | Where to send sessions. Without it, sessions stay on the device. |
screenshotMaskLevel | allTextAndMedia | How much of each screenshot is masked. |
userId | none | A starting user ID. |
widgetNames | empty | Readable names for your widgets, for obfuscated builds. |
capturePixelRatio | 0.75 | Screenshot scale. |
captureFocusChanges | false | Record when a text field gains focus (never what's typed). |
captureSystemInput | false | Record presses of Android back and hardware buttons. |
captureScrollScreenshots | false | Take screenshots during scrolling. Costs more. |
More resources
- API reference: every class, method and option in
tugboat. - Packages on pub.dev:
tugboat,tugboat_dio,tugboat_http. - Source code on GitHub.
Updated about 3 hours ago