cxorbi_flutter 1.8.0 copy "cxorbi_flutter: ^1.8.0" to clipboard
cxorbi_flutter: ^1.8.0 copied to clipboard

Cxorbi analytics for Flutter: session replay, heatmaps, screens, gestures, funnels, performance, error capture and in-app surveys for iOS and Android.

example/lib/main.dart

import 'dart:io' show Platform;

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

/// Cxorbi Flutter example β€” consent, Ask surveys, identity and event capture.
///
/// Run it against your own project:
///   flutter run --dart-define=CXORBI_SOURCE_TOKEN=`<your organization API key>`
///
/// `apiUrl` defaults to https://api.cxorbi.com/api; override it with
/// --dart-define=CXORBI_API_URL only if you were given a different endpoint.
Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Cxorbi.init(CxorbiConfig(
    apiKey: const String.fromEnvironment('CXORBI_SOURCE_TOKEN',
        defaultValue: 'YOUR_PUBLIC_SOURCE_TOKEN'),
    apiUrl: const String.fromEnvironment('CXORBI_API_URL',
        defaultValue: 'https://api.cxorbi.com/api'),
    environment: CxorbiEnvironment.development,
    debugMode: true,
    logLevel: LogLevel.debug,
    surveysEnabled: true, // Ask layer on
  ));
  await Cxorbi.instance.optIn();

  runApp(const ExampleApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Cxorbi Example',
      theme: ThemeData(useMaterial3: true, colorSchemeSeed: const Color(0xFF10B981)),
      navigatorObservers: [CxorbiNavigatorObserver()],
      // Mount the survey host once so Ask surveys present over the app.
      builder: (context, child) => CxorbiSurveyHost(child: child!),
      home: const TestPanel(),
    );
  }
}

class TestPanel extends StatefulWidget {
  const TestPanel({super.key});
  @override
  State<TestPanel> createState() => _TestPanelState();
}

class _TestPanelState extends State<TestPanel> {
  final List<String> _log = [];
  String _userId = '(anonymous)';

  String get _platform => Platform.isIOS ? 'ios' : (Platform.isAndroid ? 'android' : 'other');

  @override
  void initState() {
    super.initState();
    // Surface Ask lifecycle in the on-screen log.
    Cxorbi.instance.setSurveyCallbacks(CxorbiSurveyCallbacks(
      onSurveyDisplayed: (s) => _add('πŸ“‹ survey displayed: ${s.title}'),
      onQuestionAnswered: (s, qid, v) => _add('✏️  answered β†’ $v'),
      onSurveyClosed: (s) => _add('βœ–οΈ  survey closed (dismissed)'),
      onSurveyCompleted: (s, a) => _add('βœ… survey completed (${a.length} answers)'),
    ));
  }

  void _add(String msg) {
    if (!mounted) return;
    setState(() => _log.insert(0, msg));
  }

  Future<void> _showSurvey() async {
    _userId = 'manual_${_platform}_tester';
    Cxorbi.instance.identify(_userId);
    await Cxorbi.instance.addUserProperties(properties: {
      'demo_source': 'example_app',
      'plan': 'pro',
      'country': 'IN',
    });
    _add('πŸ‘€ identified $_userId + attributes set');
    await Cxorbi.instance.showSurvey();
    _add('πŸ”” showSurvey() requested (platform=$_platform)');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Cxorbi Example  Β·  $_platform')),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Text('User: $_userId', style: const TextStyle(fontWeight: FontWeight.w600)),
            const SizedBox(height: 12),
            _btn('πŸ””  Show Ask Survey (all 11 types)', _showSurvey, primary: true),
            _btn('πŸ›’  Track event: checkout_completed', () {
              Cxorbi.instance.trackEvent(eventName: 'checkout_completed', properties: {'amount': 250, 'currency': 'INR'});
              _add('πŸ›’ tracked checkout_completed');
            }),
            _btn('⭐  Track event: item_favorited', () {
              Cxorbi.instance.trackEvent(eventName: 'item_favorited', properties: {'item_id': 'sku_1234'});
              _add('⭐ tracked item_favorited');
            }),
            _btn('🏷️  Set attribute: vip = true', () async {
              await Cxorbi.instance.addUserProperties(properties: {'vip': true});
              _add('🏷️ set attribute vip=true');
            }),
            _btn('πŸ‘€  New anonymous user (reset)', () {
              Cxorbi.instance.reset();
              setState(() => _userId = '(anonymous)');
              _add('πŸ‘€ identity reset');
            }),
            const SizedBox(height: 12),
            const Text('Activity log', style: TextStyle(fontWeight: FontWeight.w600)),
            const Divider(),
            Expanded(
              child: ListView(
                children: [for (final l in _log) Padding(padding: const EdgeInsets.symmetric(vertical: 3), child: Text(l))],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _btn(String label, VoidCallback onTap, {bool primary = false}) => Padding(
        padding: const EdgeInsets.symmetric(vertical: 4),
        child: primary
            ? FilledButton(onPressed: onTap, child: Text(label))
            : OutlinedButton(onPressed: onTap, child: Text(label)),
      );
}
32
likes
0
points
668
downloads

Publisher

unverified uploader

Weekly Downloads

Cxorbi analytics for Flutter: session replay, heatmaps, screens, gestures, funnels, performance, error capture and in-app surveys for iOS and Android.

Homepage

Topics

#analytics #session-replay #heatmaps #monitoring

License

unknown (license)

Dependencies

crypto, cryptography, ffi, flutter, flutter_secure_storage, http, path_provider, sqflite

More

Packages that depend on cxorbi_flutter

Packages that implement cxorbi_flutter