nemu_tracking_flutter 1.2.0
nemu_tracking_flutter: ^1.2.0 copied to clipboard
Nemu Smart Links attribution SDK for Flutter - UTM tracking, deep linking, and install attribution.
example/lib/main.dart
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:nemu_tracking_flutter/nemu_tracking_flutter.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Nemu SDK Example',
theme: ThemeData(
colorSchemeSeed: Colors.blue,
useMaterial3: true,
),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String _status = 'Initializing...';
final _clickIdController = TextEditingController();
String? _activeClickId;
@override
void initState() {
super.initState();
_activeClickId = getInstallReferrerClickIdOverride();
_initSdk();
}
void _initSdk() {
try {
NemuTracking.instance.init(
const NemuInitParams(
apiKey: 'deb2b453-8e0c-4edc-9980-45f599397e4d',
// `uriScheme` é opcional; omita para apps que usam só Universal Links.
uriScheme: 'nemu.fluttersdk.example://',
trackingId: 'VaCItBH8b4',
isDebugMode: true
),
);
setState(() => _status = 'SDK loaded successfully');
NemuTracking.instance.onDeepLink((data) {
debugPrint('Deep link data: $data');
});
} catch (error) {
setState(() => _status = 'Error: $error');
}
}
void _handleSetClickId() {
final trimmed = _clickIdController.text.trim();
if (trimmed.isEmpty) return;
setInstallReferrerClickIdOverride(trimmed);
setState(() => _activeClickId = trimmed);
}
void _handleClearClickId() {
setInstallReferrerClickIdOverride(null);
setState(() {
_activeClickId = null;
_clickIdController.clear();
});
}
Future<void> _handleReset() async {
final prefs = await SharedPreferences.getInstance();
final keys = prefs.getKeys().where((k) => k.startsWith('@nemu_sdk:'));
for (final key in keys) {
await prefs.remove(key);
}
setState(() => _status = 'Storage cleared! Reinitializing...');
_initSdk();
}
@override
void dispose() {
_clickIdController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'Nemu SDK Example',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 12),
Text(
_status,
style: const TextStyle(fontSize: 14, color: Colors.grey),
),
const SizedBox(height: 24),
if (kDebugMode) _buildDebugSection(),
],
),
),
),
),
);
}
Widget _buildDebugSection() {
return Container(
width: double.infinity,
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.grey[100],
borderRadius: BorderRadius.circular(12),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Install Referrer Override',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
),
const SizedBox(height: 12),
TextField(
controller: _clickIdController,
decoration: const InputDecoration(
hintText: 'Enter click_id (e.g. uuid)',
border: OutlineInputBorder(),
contentPadding: EdgeInsets.symmetric(horizontal: 12, vertical: 10),
),
autocorrect: false,
),
const SizedBox(height: 12),
Row(
children: [
Expanded(
child: ElevatedButton(
onPressed: _handleSetClickId,
child: const Text('Set click_id'),
),
),
const SizedBox(width: 8),
ElevatedButton(
onPressed: _handleClearClickId,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.grey,
foregroundColor: Colors.white,
),
child: const Text('Clear'),
),
],
),
if (_activeClickId != null) ...[
const SizedBox(height: 8),
Text(
'Active: $_activeClickId',
style: const TextStyle(
fontSize: 13,
color: Colors.blue,
fontWeight: FontWeight.w500,
),
),
],
const SizedBox(height: 4),
const Text(
'Set the click_id before resetting the SDK to simulate an install referrer on the next deferred deep link check.',
style: TextStyle(fontSize: 12, color: Colors.grey),
),
const SizedBox(height: 12),
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: _handleReset,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
foregroundColor: Colors.white,
),
child: const Text('Reset SDK (test deferred deep link)'),
),
),
],
),
);
}
}