dynatrace_flutter_plugin 3.349.1 copy "dynatrace_flutter_plugin: ^3.349.1" to clipboard
dynatrace_flutter_plugin: ^3.349.1 copied to clipboard

The Dynatrace Flutter plugin helps auto-instrument your Flutter app with Dynatrace OneAgent for Android and iOS. It also provides an API to add manual instrumentation.

example/lib/main.dart

import 'package:dynatrace_flutter_plugin/dynatrace_flutter_plugin.dart';
import 'package:flutter/material.dart';

void main() {
  // Start the Dynatrace plugin. Pass your root widget here.
  Dynatrace().start(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Dynatrace Flutter Plugin',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Color(0xFF1396FF)),
        useMaterial3: true,
      ),
      home: const DynatraceDemoPage(),
      navigatorObservers: [
        // Enables automatic view lifecycle tracking via WidgetViewBinding
        DynatraceNavigationObserver(),
      ],
    );
  }
}

class DynatraceDemoPage extends StatefulWidget {
  const DynatraceDemoPage({super.key});

  @override
  State<DynatraceDemoPage> createState() => _DynatraceDemoPageState();
}

class _DynatraceDemoPageState extends State<DynatraceDemoPage> {
  bool _privacyOptedIn = true;

  @override
  void initState() {
    super.initState();
    _loadPrivacyOptions();
  }

  Future<void> _loadPrivacyOptions() async {
    // Retrieve the current privacy settings from the Dynatrace SDK.
    final options = await Dynatrace().getUserPrivacyOptions();
    print('datacollectionlevel aa : ' + options.dataCollectionLevel.toString());
    if (mounted) {
      setState(() {
        _privacyOptedIn = options.dataCollectionLevel != DataCollectionLevel.Off;
      });
    }
  }

  void _togglePrivacy(bool optedIn) {
    // DataCollectionLevel.UserBehavior enables full data collection including both performance and user data.
    // DataCollectionLevel.Off disables all data collection.
    final options = optedIn
        ? UserPrivacyOptions(DataCollectionLevel.UserBehavior, true)
        : UserPrivacyOptions(DataCollectionLevel.Off, false);

    // Apply the new privacy settings.
    Dynatrace().applyUserPrivacyOptions(options);
    setState(() => _privacyOptedIn = optedIn);
  }

  void _sendEvent() {
    // Send a custom business event with optional key-value properties.
    // Events appear in Dynatrace under Users & Sessions in the corresponding sessions.
    Dynatrace().sendEvent(EventData(eventProperties: {
      'event_properties.source': 'demo_button',
    }));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Dynatrace Flutter Plugin'),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: Padding(
        padding: const EdgeInsets.all(24),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Card(
              child: SwitchListTile(
                title: const Text('Privacy opt-in'),
                subtitle: const Text('Enable data collection'),
                value: _privacyOptedIn,
                onChanged: _togglePrivacy,
              ),
            ),
            const SizedBox(height: 32),
            SizedBox(
              width: double.infinity,
              child: ElevatedButton(
                onPressed: _sendEvent,
                child: const Text('Send event'),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
33
likes
150
points
65.6k
downloads

Documentation

API reference

Publisher

verified publisherdynatrace.com

Weekly Downloads

The Dynatrace Flutter plugin helps auto-instrument your Flutter app with Dynatrace OneAgent for Android and iOS. It also provides an API to add manual instrumentation.

Homepage

License

unknown (license)

Dependencies

flutter, http, path, yaml

More

Packages that depend on dynatrace_flutter_plugin

Packages that implement dynatrace_flutter_plugin