sniffurl_flutter 1.0.0
sniffurl_flutter: ^1.0.0 copied to clipboard
Flutter SDK for SniffURL deep linking and attribution tracking
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:sniffurl_flutter/sniffurl_flutter.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize SniffURL SDK
await SniffURL.init(
projectKey: 'YOUR_PROJECT_KEY',
apiKey: 'YOUR_API_KEY',
appName: 'SniffURL Demo',
appVersion: '1.0.0',
bundleId: 'com.example.sniffurl_demo',
);
// Track app install
await SniffURL.track('install');
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'SniffURL Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
useMaterial3: true,
),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String? _visitorId;
String _lastEvent = 'None';
@override
void initState() {
super.initState();
_trackAppOpen();
_loadVisitorId();
}
Future<void> _trackAppOpen() async {
await SniffURL.track('app_open');
setState(() {
_lastEvent = 'app_open';
});
}
void _loadVisitorId() {
setState(() {
_visitorId = SniffURL.getVisitorId();
});
}
Future<void> _trackSignup() async {
await SniffURL.track('signup', {
'userId': 'U${DateTime.now().millisecondsSinceEpoch}',
'plan': 'basic',
'timestamp': DateTime.now().toIso8601String(),
});
setState(() {
_lastEvent = 'signup';
});
_showSnackBar('Signup event tracked!');
}
Future<void> _trackPurchase() async {
await SniffURL.track('purchase', {
'orderId': 'ORD-${DateTime.now().millisecondsSinceEpoch}',
'amount': 1499,
'currency': 'INR',
'items': ['Premium Plan'],
});
setState(() {
_lastEvent = 'purchase';
});
_showSnackBar('Purchase event tracked!');
}
Future<void> _trackCustomEvent() async {
await SniffURL.track('level_up', {
'level': 7,
'character': 'wizard',
'timestamp': DateTime.now().toIso8601String(),
});
setState(() {
_lastEvent = 'level_up';
});
_showSnackBar('Custom event tracked!');
}
Future<void> _testDeepLink() async {
final testVisitorId = 'test_${DateTime.now().millisecondsSinceEpoch}';
await SniffURL.handleUrl('yourapp://open?sn_vid=$testVisitorId');
_loadVisitorId();
_showSnackBar('Deep link handled with visitor ID: $testVisitorId');
}
void _showSnackBar(String message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
duration: const Duration(seconds: 2),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('SniffURL Flutter Demo'),
elevation: 2,
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'SDK Status',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 12),
_buildInfoRow('Visitor ID', _visitorId ?? 'Not set'),
const SizedBox(height: 8),
_buildInfoRow('Last Event', _lastEvent),
],
),
),
),
const SizedBox(height: 24),
const Text(
'Track Events',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 12),
ElevatedButton.icon(
onPressed: _trackSignup,
icon: const Icon(Icons.person_add),
label: const Text('Track Signup'),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.all(16),
),
),
const SizedBox(height: 12),
ElevatedButton.icon(
onPressed: _trackPurchase,
icon: const Icon(Icons.shopping_cart),
label: const Text('Track Purchase'),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.all(16),
),
),
const SizedBox(height: 12),
ElevatedButton.icon(
onPressed: _trackCustomEvent,
icon: const Icon(Icons.star),
label: const Text('Track Custom Event'),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.all(16),
),
),
const SizedBox(height: 24),
const Text(
'Testing',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 12),
OutlinedButton.icon(
onPressed: _testDeepLink,
icon: const Icon(Icons.link),
label: const Text('Test Deep Link'),
style: OutlinedButton.styleFrom(
padding: const EdgeInsets.all(16),
),
),
],
),
),
);
}
Widget _buildInfoRow(String label, String value) {
return Row(
children: [
Text(
'$label: ',
style: const TextStyle(
fontWeight: FontWeight.w500,
color: Colors.grey,
),
),
Expanded(
child: Text(
value,
style: const TextStyle(
fontWeight: FontWeight.w600,
),
overflow: TextOverflow.ellipsis,
),
),
],
);
}
@override
void dispose() {
SniffURL.dispose();
super.dispose();
}
}