evntaly_flutter 1.0.1 copy "evntaly_flutter: ^1.0.1" to clipboard
evntaly_flutter: ^1.0.1 copied to clipboard

Official Evntaly Flutter SDK

example/lib/main.dart

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

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

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _status = 'Initializing...';
  bool _trackingEnabled = true;

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

  Future<void> _initializeSDK() async {
    try {
      // Initialize the Evntaly SDK
      Evntaly.init(
        developerSecret: 'your-secret',
        projectToken: 'your-project-token',
        verbose: true,
      );

      setState(() {
        _status = 'SDK Initialized ✓\nReady to track events!';
      });
    } catch (e) {
      setState(() {
        _status = 'Error initializing SDK: $e';
      });
    }
  }

  Future<void> _trackEvent() async {
    try {
      await Evntaly.track(
        EvntalyEvent(
          title: "Hello, World! From Evntaly!",
          description: "This is system generated event",
          message: "You can easily delete it.",
          user: EvntalyUser(
            id: "synthetic_i88thcewnnimjahx335",
          ),
          data: {
            "license": "Onboarding",
            "onboarding_period": "14 Days",
            "is_onboarding": true
          },
          icon: '😂',
          sessionID: "shopify-session-234d",
          feature: "Order Flow",
          topic: "@shopify/order_created",
        ),
      );

      setState(() {
        _status = 'Event tracked successfully! ✓';
      });
    } catch (e) {
      setState(() {
        _status = 'Error tracking event: $e';
      });
    }
  }

  Future<void> _identifyUser() async {
    try {
      // Using typed EvntalyUser for IntelliSense support
      await Evntaly.identifyUser(
        EvntalyUser(
          id: "synthetic_i88thcewnnimjahx335_00923",
          email: '[email protected]',
          fullName: 'Demo User',
          organization: "Demo Organization",
          data: {
            "license": "Onboarding",
            "onboarding_period": "14 Days",
            "is_onboarding": true
          },
        ),
      );

      setState(() {
        _status = 'User identified successfully! ✓';
      });
    } catch (e) {
      setState(() {
        _status = 'Error identifying user: $e';
      });
    }
  }

  void _disableTracking() {
    Evntaly.disableTracking();
    setState(() {
      _trackingEnabled = false;
      _status = 'Tracking disabled ✓\nEvents will not be sent.';
    });
  }

  void _enableTracking() {
    Evntaly.enableTracking();
    setState(() {
      _trackingEnabled = true;
      _status = 'Tracking enabled ✓\nEvents will be sent.';
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorObservers: [EvntalyNavigatorObserver()],
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Evntaly Flutter SDK Example'),
        ),
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              Text(
                _status,
                textAlign: TextAlign.center,
                style: const TextStyle(fontSize: 16),
              ),
              const SizedBox(height: 32),
              ElevatedButton(
                onPressed: _trackEvent,
                child: const Text('Track Event'),
              ),
              const SizedBox(height: 16),
              ElevatedButton(
                onPressed: _identifyUser,
                child: const Text('Identify User'),
              ),
              const SizedBox(height: 32),
              Row(
                children: [
                  Expanded(
                    child: ElevatedButton(
                      onPressed: _trackingEnabled ? _disableTracking : null,
                      style: ElevatedButton.styleFrom(
                        backgroundColor: Colors.red,
                        foregroundColor: Colors.white,
                      ),
                      child: const Text('Disable Tracking'),
                    ),
                  ),
                  const SizedBox(width: 16),
                  Expanded(
                    child: ElevatedButton(
                      onPressed: _trackingEnabled ? null : _enableTracking,
                      style: ElevatedButton.styleFrom(
                        backgroundColor: Colors.green,
                        foregroundColor: Colors.white,
                      ),
                      child: const Text('Enable Tracking'),
                    ),
                  ),
                ],
              ),
              const SizedBox(height: 32),
              const Text(
                'Note: This is an Evntaly example app. For more information, please visit https://evntaly.com',
                textAlign: TextAlign.center,
                style: TextStyle(fontSize: 12, fontStyle: FontStyle.italic),
              ),
            ],
          ),
        ),
      ),
    );
  }
}