opus_tracking 0.0.4 copy "opus_tracking: ^0.0.4" to clipboard
opus_tracking: ^0.0.4 copied to clipboard

@opus/tracking

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:opus_tracking/shelf.dart';

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

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  late final EventClient _eventClient;

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

    EventClient eventClient = EventTrackingFactory.create(
        tenantId: 'your-tenant-id', apiKey: 'your-api-key');
    eventClient.setSource('your-source');

    eventClient
        .addScreenName(IScreen(uiKey: generateRandomString(10), name: 'home'));

    setState(() {
      _eventClient = eventClient;
    });
  }

  void _incrementCounter() {
    IClickParams params = IClickParams(additionalData: {});
    _eventClient.logClickEvent('skip-for-now_link', params: params);

    final screenName = _eventClient.getCurrentScreenName();

    final uiKey = screenName?.uiKey;

    print('uiKey: $uiKey');

    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
            ElevatedButton(
                onPressed: () {
                  _eventClient.addScreenName(IScreen(
                      uiKey: generateRandomString(10), name: 'credential'));

                  final uiKey = _eventClient.getCurrentScreenName()?.uiKey;

                  print('current uiKey: $uiKey');
                },
                child: const Text('Add screen')),
            ElevatedButton(
                onPressed: () {
                  _eventClient.removeScreenName();

                  final uiKey = _eventClient.getCurrentScreenName()?.uiKey;

                  print('current uiKey: $uiKey');
                },
                child: const Text('Remove screen'))
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}