Why Use affise_attribution_lib in a Flutter App?
Affise Attribution Lib is a dedicated Flutter plugin for collecting usage statistics and attribution data. It shines when you need a ready‑made networking layer for Affise without writing boilerplate HTTP code. The package is especially useful in clean‑architecture projects where you want to keep third‑party calls behind a service boundary.
Installation
Add the package to your pubspec.yaml using the Flutter CLI:
flutter pub add affise_attribution_libAfter the command finishes, run a full flutter pub get to ensure the dependency is resolved.
Basic Setup
Before you can send any attribution data, you must initialise the SDK with your Affise credentials. The most common place to do this is in the main() function or inside a dedicated initialization service.
import 'package:flutter/material.dart';
import 'package:affise_attribution_lib/affise_attribution_lib.dart';
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialise Affise with your app token and optional configuration.
await Affise.init(
token: 'YOUR_AFFISE_APP_TOKEN',
// Optional: enable debug mode while developing.
debug: true,
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Affise Demo',
home: const HomePage(),
);
}
}Replace YOUR_AFFISE_APP_TOKEN with the token you receive from the Affise dashboard.
Tracking an Event
Once the SDK is initialised, you can track custom events anywhere in your code. It is a good practice to wrap the SDK calls in a service class so the rest of your UI stays decoupled.
import 'package:affise_attribution_lib/affise_attribution_lib.dart';
class AffiseService {
// Singleton pattern (optional but convenient).
static final AffiseService _instance = AffiseService._internal();
factory AffiseService() => _instance;
AffiseService._internal();
Future trackPurchase({required double amount, required String currency}) async {
try {
await Affise.trackEvent(
eventName: 'purchase',
parameters: {
'price': amount,
'currency': currency,
},
);
} catch (e) {
// In production you might want to log this to your own error reporting.
debugPrint('Affise tracking failed: $e');
}
}
}
// Example usage inside a widget:
class PurchaseButton extends StatelessWidget {
const PurchaseButton({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () async {
await AffiseService().trackPurchase(amount: 9.99, currency: 'USD');
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Purchase event sent')),
);
},
child: const Text('Buy Now'),
);
}
}Configuration Tips & Common Mistakes
- Do not initialise the SDK multiple times. Call
Affise.initonce, preferably at app start. - Check platform compatibility. The plugin works on Android and iOS; verify that your
minSdkVersionanddeployment targetmeet the library requirements. - Handle async errors. All SDK methods return
Future; always await them or catch exceptions. - Keep the SDK behind an abstraction. This makes future migration easier if the package API changes.
💡 Tip: During development, enable
debug: trueinAffise.init. It prints detailed logs to the console, helping you verify that events are being sent correctly.
Testing the Integration
After adding the code, run the app on a real device or emulator. Open the console (run flutter run) and look for messages that confirm the SDK initialisation and event tracking. If you do not see any logs, double‑check the following:
- Is the app token correct?
- Did you add the required
INTERNETpermission on Android (android/app/src/main/AndroidManifest.xml)? - Is the iOS
Info.plistconfigured with the appropriateNSAppTransportSecuritysettings if you use a non‑HTTPS endpoint?
When to Review Before Production
Even though the package is stable, you should still perform a short review before shipping:
- Read the official documentation on pub.dev for any breaking changes.
- Confirm that the SDK version you use complies with your app’s privacy policy and GDPR/CCPA requirements.
- Run integration tests that mock the
Affiseclass to ensure your abstraction behaves as expected.
Conclusion
The affise_attribution_lib Flutter package offers a quick path to attribution and analytics without reinventing networking code. By keeping the SDK behind a service layer, you retain flexibility and protect your codebase from future API changes.
Frequently Searched Terms
- affise attribution flutter
- affise_attribution_lib example
- flutter attribution SDK
- track events with affise
- flutter pub add affise_attribution_lib
Frequently Asked Questions
Do I need to add any platform‑specific permissions for affise_attribution_lib?
Yes. On Android you must declare the <code>INTERNET</code> permission in <code>AndroidManifest.xml</code>. On iOS, ensure your <code>Info.plist</code> allows network access (e.g., configure <code>NSAppTransportSecurity</code> if needed).
Can I use affise_attribution_lib without initializing it in main()?
The SDK must be initialised once before any tracking calls. Initialising in <code>main()</code> or in an early splash screen is the recommended approach.
Is affise_attribution_lib compatible with Flutter Web?
As of the latest version, the package officially supports Android and iOS only. Web support is not listed on pub.dev, so verify the documentation before attempting to use it on the web.
How do I test event tracking without sending real data to Affise?
Wrap the <code>Affise</code> calls in an abstraction (e.g., <code>AffiseService</code>) and provide a mock implementation for unit tests. This lets you verify that your code invokes the correct methods without network calls.
Where can I find the full API reference for affise_attribution_lib?
The complete API reference is available on the package page: <a href="https://pub.dev/packages/affise_attribution_lib">https://pub.dev/packages/affise_attribution_lib</a>. Always check the README and changelog for the latest details.