amplitude_engagement_flutter 0.0.3 copy "amplitude_engagement_flutter: ^0.0.3" to clipboard
amplitude_engagement_flutter: ^0.0.3 copied to clipboard

Official Amplitude Engagement Flutter SDK, supporting Android, iOS

example/lib/main.dart

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

// Global API key configuration
const String amplitudeApiKey = '6dba5c25868be3716e69f525035e33b6';

void main() {
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Amplitude Engagement Flutter Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: const MyHomePage(title: 'Amplitude Engagement Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  AmplitudeEngagement? _amplitude;
  List<GuideOrSurvey> _guides = [];
  String _status = 'Not initialized';

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

  void _setupAmplitudeEngagement() async {
    try {
      // Initialize with your API key and optional options
      final amplitude = AmplitudeEngagement(
        amplitudeApiKey,
        options: AmplitudeInitOptions(
          logLevel: AmplitudeLogLevel.debug,
          serverZone: AmplitudeServerZone.us,
          // Optional: Set custom URLs for proxy support
          // serverUrl: 'https://your-proxy.com',
          // cdnUrl: 'https://your-cdn.com',
          // mediaUrl: 'https://your-media.com',
          // locale: 'en',
        ),
      );

      amplitude.addCallback('test', () {
        debugPrint('Callback Invoked: callback_key');
      });
      // Boot the instance with user information
      await amplitude.boot(userId: 'user123', deviceId: 'device456');

      // Set theme mode
      await amplitude.setThemeMode(AmplitudeThemeMode.light);

      setState(() {
        _amplitude = amplitude;
        _status = 'Initialized successfully';
      });

      // Load guides and surveys
      _loadGuides();
    } catch (e) {
      setState(() {
        _status = 'Error: $e';
      });
    }
  }

  void _loadGuides() async {
    if (_amplitude == null) return;

    try {
      final guides = await _amplitude!.list();
      setState(() {
        _guides = guides;
      });
    } catch (e) {
      debugPrint('Error loading guides: $e');
    }
  }

  void _showGuide(String key) async {
    if (_amplitude == null) return;

    try {
      await _amplitude!.show(key, 0);
    } catch (e) {
      debugPrint('Error showing guide: $e');
    }
  }

  void _trackScreen(String screenName) async {
    if (_amplitude == null) return;

    try {
      await _amplitude!.screen(screenName);
    } catch (e) {
      debugPrint('Error tracking screen: $e');
    }
  }

  void _addCallback(String key) async {
    if (_amplitude == null) return;

    try {
      await _amplitude!.addCallback(key, () {
        debugPrint('Callback Invoked: $key');
      });
    } catch (e) {
      debugPrint('Error adding callback: $e');
    }
  }

  void _closeAll() async {
    if (_amplitude == null) return;

    try {
      await _amplitude!.closeAll();
    } catch (e) {
      debugPrint('Error closing all: $e');
    }
  }

  void _handleURL(String url) async {
    if (_amplitude == null) return;

    try {
      final success = await _amplitude!.handleURL(url);
      debugPrint('Handle URL success: $success');
    } catch (e) {
      debugPrint('Error handling URL: $e');
    }
  }

  void _forwardEvent() async {
    if (_amplitude == null) return;

    try {
      final event = {
        'event_type': 'custom_event',
        'event_id': 12345,
        'platform': 'Flutter',
        'event_properties': {
          'custom_property': 'custom_value',
          'timestamp': DateTime.now().toIso8601String(),
        },
      };

      await _amplitude!.forwardEvent(event);
    } catch (e) {
      debugPrint('Error forwarding event: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title)),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      'Status:',
                      style: Theme.of(context).textTheme.titleMedium,
                    ),
                    const SizedBox(height: 8),
                    Text(_status),
                  ],
                ),
              ),
            ),
            const SizedBox(height: 16),
            if (_amplitude != null) ...[
              Text(
                'Available Guides & Surveys:',
                style: Theme.of(context).textTheme.titleMedium,
              ),
              const SizedBox(height: 8),
              if (_guides.isEmpty)
                const Text('No guides available')
              else
                ..._guides.map(
                  (guide) => Card(
                    child: ListTile(
                      title: Text(guide.title),
                      subtitle: Text(
                        'Status: ${guide.status}, Step: ${guide.step}',
                      ),
                      trailing: ElevatedButton(
                        onPressed: () => _showGuide(guide.id.toString()),
                        child: const Text('Show'),
                      ),
                    ),
                  ),
                ),
              const SizedBox(height: 16),
              Wrap(
                spacing: 8,
                runSpacing: 8,
                children: [
                  ElevatedButton(
                    onPressed: _loadGuides,
                    child: const Text('Refresh Guides'),
                  ),
                  ElevatedButton(
                    onPressed: () => _trackScreen('home_screen'),
                    child: const Text('Track Screen'),
                  ),
                  ElevatedButton(
                    onPressed: () => _addCallback('callback_key'),
                    child: const Text('Add Callback'),
                  ),
                  ElevatedButton(
                    onPressed: _closeAll,
                    child: const Text('Close All'),
                  ),
                  ElevatedButton(
                    onPressed: () => _handleURL('https://example.com'),
                    child: const Text('Handle URL'),
                  ),
                  ElevatedButton(
                    onPressed: _forwardEvent,
                    child: const Text('Forward Event'),
                  ),
                ],
              ),
            ],
          ],
        ),
      ),
    );
  }
}
0
likes
160
points
357
downloads

Publisher

unverified uploader

Weekly Downloads

Official Amplitude Engagement Flutter SDK, supporting Android, iOS

Homepage
Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on amplitude_engagement_flutter

Packages that implement amplitude_engagement_flutter