layers_flutter 1.0.0
layers_flutter: ^1.0.0 copied to clipboard
Layers SDK for Flutter. Analytics with ATT, SKAN, and deep link integrations.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:layers_flutter/layers_flutter.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Optional: register an error handler before init
Layers.onError = (error) {
debugPrint('SDK error: $error');
};
// 1. Initialize the SDK
await Layers.init(LayersConfig(
apiKey: 'YOUR_API_KEY',
appId: 'YOUR_APP_ID',
enableDebug: true,
environment: LayersEnvironment.development,
));
// 2. Identify the user (after login or from stored session)
Layers.identify('example-user-42');
// 3. Request ATT authorization on iOS (no-op on Android)
final attStatus = await ATT.requestAuthorization();
Layers.track('att_result', properties: {'status': attStatus.name});
// 4. Set consent preferences
Layers.setConsent(const ConsentSettings(
analytics: true,
advertising: false,
));
// 5. Debug: print full SDK state
Layers.debugPrintState();
runApp(const LayersExampleApp());
}
class LayersExampleApp extends StatelessWidget {
const LayersExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Layers SDK Example',
// 6. Automatic screen tracking via NavigatorObserver
navigatorObservers: [LayersNavigatorObserver()],
theme: ThemeData(
colorSchemeSeed: Colors.deepPurple,
useMaterial3: true,
),
home: const ExampleHome(),
);
}
}
class ExampleHome extends StatelessWidget {
const ExampleHome({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Layers SDK Example')),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
// Track a custom event
FilledButton(
onPressed: () {
Layers.track('button_tapped', properties: {'screen': 'home'});
},
child: const Text('Track Event'),
),
const SizedBox(height: 12),
// Track a screen view
FilledButton.tonal(
onPressed: () {
Layers.screen('SettingsScreen', properties: {'tab': 'general'});
},
child: const Text('Track Screen'),
),
const SizedBox(height: 12),
// Identify a different user
OutlinedButton(
onPressed: () {
Layers.identify('new-user-99');
},
child: const Text('Identify User'),
),
const SizedBox(height: 12),
// Update consent
OutlinedButton(
onPressed: () {
Layers.setConsent(const ConsentSettings(
analytics: true,
advertising: true,
));
},
child: const Text('Grant All Consent'),
),
const SizedBox(height: 12),
// Reset user state (logout flow)
OutlinedButton(
onPressed: () async {
await Layers.reset();
},
child: const Text('Reset (Logout)'),
),
const SizedBox(height: 12),
// Debug print state
OutlinedButton(
onPressed: () {
Layers.debugPrintState();
},
child: const Text('Debug Print State'),
),
const SizedBox(height: 12),
// Flush events
OutlinedButton(
onPressed: () {
Layers.flush();
},
child: const Text('Flush Queue'),
),
const SizedBox(height: 24),
Text(
'Session: ${Layers.sessionId ?? "N/A"}',
style: Theme.of(context).textTheme.bodySmall,
),
Text(
'Queue: ${Layers.queueDepth} events',
style: Theme.of(context).textTheme.bodySmall,
),
],
),
);
}
}