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
  • tugboat 0.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 tugboat

There 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, and TugboatReplay.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') to showDialog or showModalBottomSheet.
  • Nested navigators: give each one its own observer from TugboatReplay.createNavigatorObserver(). Create it once, not in build.
  • go_router: pages created with builder: are named after the route. For pages created with pageBuilder:, pass name: state.fullPath to 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_dio
import '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

  1. Run a debug build on a device with --dart-define=TUGBOAT_HTTP_PROOF=true.
  2. Use the app for a minute.
  3. Check the logs for TUGBOAT_HTTP lines with path=/v1/sessions, /v1/events/batch and /v1/frames, and status 202.
  4. 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 = true switches 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.

OptionDefaultDescription
enabledfalseStart recording when the app starts.
collectornoneWhere to send sessions. Without it, sessions stay on the device.
screenshotMaskLevelallTextAndMediaHow much of each screenshot is masked.
userIdnoneA starting user ID.
widgetNamesemptyReadable names for your widgets, for obfuscated builds.
capturePixelRatio0.75Screenshot scale.
captureFocusChangesfalseRecord when a text field gains focus (never what's typed).
captureSystemInputfalseRecord presses of Android back and hardware buttons.
captureScrollScreenshotsfalseTake screenshots during scrolling. Costs more.

More resources


Did this page help you?