Skip to main content

jatayu_flutter 0.9.0 copy "jatayu_flutter: ^0.9.0" to clipboard
jatayu_flutter: ^0.9.0 copied to clipboard

unlisted

Mobile analytics, attribution, observability, and remote configuration for the Jatayu platform.

example/lib/main.dart

import 'dart:async';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:jatayu_flutter/jatayu_flutter.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  try {
    await Firebase.initializeApp();
    FirebaseMessaging.onBackgroundMessage(jatayuPushBackground);
  } catch (_) {
    // Analytics still runs without google-services.json / GoogleService-Info.plist.
  }
  runApp(const ExampleApp());
}

class ExampleApp extends StatelessWidget {
  const ExampleApp({super.key});
  @override
  Widget build(BuildContext context) => MaterialApp(
    title: 'Jatayu SDK',
    theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xff9b83f4), brightness: Brightness.dark), useMaterial3: true),
    navigatorObservers: [JatayuNavigatorObserver()],
    initialRoute: '/',
    routes: {
      '/': (_) => const ExampleScreen(),
      '/second': (_) => const SecondScreen(),
    },
  );
}

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

class _ExampleScreenState extends State<ExampleScreen> {
  final endpoint = TextEditingController(text: const String.fromEnvironment('JATAYU_ENDPOINT', defaultValue: 'http://localhost:5173'));
  final keyInput = TextEditingController(text: const String.fromEnvironment('JATAYU_PUBLIC_KEY'));
  final user = TextEditingController(text: 'example-user');
  JatayuClient? client;
  bool consent = false, busy = false;
  String status = 'Not connected';

  Future<void> perform(Future<void> Function() operation) async {
    setState(() => busy = true);
    try { await operation(); if (mounted) setState(() => status = 'Done · ${client?.pendingEventCount ?? 0} queued events'); }
    catch (error) { if (mounted) setState(() => status = error.toString()); }
    finally { if (mounted) setState(() => busy = false); }
  }

  @override
  void dispose() {
    endpoint.dispose(); keyInput.dispose(); user.dispose();
    unawaited(Jatayu.shutdown()); super.dispose();
  }

  @override
  Widget build(BuildContext context) => Scaffold(appBar: AppBar(title: const Text('Jatayu SDK')), body: Center(child: ConstrainedBox(constraints: const BoxConstraints(maxWidth: 520), child: ListView(padding: const EdgeInsets.all(24), children: [
    TextField(controller: endpoint, decoration: const InputDecoration(labelText: 'Jatayu endpoint')),
    const SizedBox(height: 12),
    TextField(controller: keyInput, decoration: const InputDecoration(labelText: 'Public SDK key')),
    SwitchListTile(contentPadding: EdgeInsets.zero, title: const Text('Analytics consent'), value: consent, onChanged: busy ? null : (value) { setState(() => consent = value); if (client != null) unawaited(perform(() => client!.setConsent(value))); }),
    FilledButton(onPressed: busy || client != null ? null : () => perform(() async {
      client = await Jatayu.initialize(JatayuOptions(endpoint: endpoint.text.trim(), writeKey: keyInput.text.trim(), consent: consent, allowInsecureLocalhost: true));
    }), child: const Text('Connect')),
    const SizedBox(height: 24), Text(status),
    if (client?.installationId != null) SelectableText('Installation: ${client!.installationId}'),
    const SizedBox(height: 20),
    TextField(controller: user, decoration: const InputDecoration(labelText: 'User ID')),
    OutlinedButton(onPressed: busy || client == null || !consent ? null : () => perform(() async {
      await client!.identify(user.text, properties: {'name': 'Example user'}, setOnce: {'signup_source': 'example'});
    }), child: const Text('Identify user')),
    OutlinedButton(onPressed: busy || client == null || !consent ? null : () => perform(() => client!.setGroup('organization', 'demo', properties: {'plan': 'pro'})), child: const Text('Join demo group')),
    OutlinedButton(onPressed: busy || client == null || !consent ? null : () => perform(() async { await client!.track('product_view', {'product_id': 'premium'}); await client!.flush(); }), child: const Text('Send product_view')),
    OutlinedButton(onPressed: busy || client == null || !consent ? null : () {
      final messenger = ScaffoldMessenger.of(context);
      unawaited(perform(() async {
        await client!.fetchConfig();
        if (!mounted) return;
        messenger.showSnackBar(SnackBar(content: Text(client!.getString('welcome_message', fallback: 'No welcome_message configured'))));
      }));
    }, child: const Text('Fetch remote config')),
    OutlinedButton(onPressed: busy || client == null || !consent ? null : () => perform(() => client!.enablePush(onOpened: (open) {
      if (mounted) setState(() => status = 'Opened ${open.action ?? 'notification'} ${open.url ?? ''}');
    })), child: const Text('Enable push')),
    OutlinedButton(onPressed: busy || client == null || !consent ? null : () => perform(() => client!.resetIdentity()), child: const Text('Reset identity')),
    OutlinedButton(onPressed: busy || client == null || !consent ? null : () => Navigator.pushNamed(context, '/second'), child: const Text('Open second screen')),
  ]))));
}

class SecondScreen extends StatelessWidget {
  const SecondScreen({super.key});
  @override
  Widget build(BuildContext context) => Scaffold(
    appBar: AppBar(title: const Text('Second screen')),
    body: const Center(child: Text('screen_view · /second')),
  );
}