Flutter SDK
Complete reference for the AppRefer Flutter SDK. Supports iOS and Android with a single integration.
Installation#
Add the SDK as a git dependency in your pubspec.yaml:
dependencies:
apprefer_flutter_sdk:
git:
url: https://github.com/AppAgentic/apprefer-flutter-sdk.git
ref: mainflutter pub getAndroid Setup#
To enable Google Play Install Referrer attribution on Android, add the Install Referrer library to your app-level build.gradle:
dependencies {
implementation "com.android.installreferrer:installreferrer:2.2"
}iOS
Configuration#
The AppReferConfig object accepts the following parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
apiKey | String | Yes | Your SDK key (pk_live_... or pk_test_...) |
userId | String? | No | Optional user ID to associate with attribution (e.g., RevenueCat app user ID) |
debug | bool | No | Enable verbose logging. Defaults to false. |
API Reference#
AppReferSDK.configure(config)#
Initialize the SDK and resolve attribution. Call once at app launch. Returns the attribution result or null if the device is organic (no matching click found).
| Details | |
|---|---|
| Signature | static Future<Attribution?> configure(AppReferConfig config) |
| Returns | Attribution? — the resolved attribution, or null |
| Throws | Never — errors are caught internally and logged |
final attribution = await AppReferSDK.configure(AppReferConfig(
apiKey: 'pk_live_your_key_here',
debug: true,
));AppReferSDK.trackEvent(name, ...)#
Send a custom event to AppRefer. Events can trigger ad network forwarding if configured.
| Parameter | Type | Required | Description |
|---|---|---|---|
name | String | Yes | Event name (e.g., 'signup', 'tutorial_complete') |
properties | Map<String, dynamic>? | No | Arbitrary key-value metadata |
revenue | double? | No | Revenue amount (e.g., 9.99) |
currency | String? | No | ISO 4217 currency code (e.g., 'USD') |
await AppReferSDK.trackEvent(
'tutorial_complete',
properties: {'level': 3, 'time_spent': 42},
);
// With revenue
await AppReferSDK.trackEvent(
'purchase',
revenue: 9.99,
currency: 'USD',
);AppReferSDK.setAdvancedMatching(...)#
Send hashed PII to improve ad network match rates. All values are SHA256-hashed on the device before being sent to the server. The server never receives plaintext PII.
| Parameter | Type | Required | Description |
|---|---|---|---|
email | String? | No | User's email address |
phone | String? | No | Phone number in E.164 format |
firstName | String? | No | User's first name |
lastName | String? | No | User's last name |
dateOfBirth | String? | No | Date of birth (YYYYMMDD) |
await AppReferSDK.setAdvancedMatching(
email: 'user@example.com',
phone: '+15551234567',
firstName: 'Jane',
lastName: 'Doe',
);AppReferSDK.setUserId(userId)#
Associate a user ID with the device's attribution. This provides an additional matching signal for purchase webhooks.
Set appreferId on RevenueCat FIRST
setUserId(), you must set the appreferId attribute on RevenueCat. This is the primary mechanism for matching RevenueCat webhook events back to AppRefer attributions. setUserId() alone is not sufficient — without appreferId, purchase events cannot be attributed.| Details | |
|---|---|
| Signature | static Future<void> setUserId(String userId) |
| Returns | void |
// REQUIRED: Set appreferId on RevenueCat first
await Purchases.shared.setAttributes({"appreferId": attribution.deviceId});
// Then optionally set user ID for additional matching
await AppReferSDK.setUserId(Purchases.appUserID);AppReferSDK.getAttribution()#
Returns the cached attribution from the last configure() call. Returns null if configure() has not been called yet or the device is organic.
| Details | |
|---|---|
| Signature | static Attribution? getAttribution() |
| Returns | Attribution? — cached attribution result |
AppReferSDK.getDeviceId()#
Returns the device ID generated by AppRefer. This is a stable UUID created on first launch and persisted across app restarts.
| Details | |
|---|---|
| Signature | static String? getDeviceId() |
| Returns | String? — the device UUID, or null before configure() |
Attribution Model#
The Attribution object returned by configure() contains the following fields:
| Field | Type | Description |
|---|---|---|
network | String? | Ad network name (e.g., 'meta', 'google', 'tiktok', 'apple_search_ads') |
matchType | String? | Confidence level of the attribution match |
campaignName | String? | Campaign name from the tracking link |
adsetName | String? | Ad set / ad group name |
adName | String? | Ad creative name |
fbclid | String? | Facebook click ID |
gclid | String? | Google click ID |
ttclid | String? | TikTok click ID |
clickId | String? | Generic click ID (AppRefer internal) |
deviceId | String? | Device UUID assigned by AppRefer |
createdAt | String? | ISO 8601 timestamp of the attribution |
Full Integration Example#
This example shows a complete integration with RevenueCat, including attribution resolution, user ID linking, and advanced matching after signup:
import 'package:flutter/material.dart';
import 'package:apprefer_flutter_sdk/apprefer_flutter_sdk.dart';
import 'package:purchases_flutter/purchases_flutter.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 1. Initialize AppRefer
final attribution = await AppReferSDK.configure(AppReferConfig(
apiKey: 'pk_live_your_key_here',
));
// 2. Initialize RevenueCat
await Purchases.configure(
PurchasesConfiguration('your_revenuecat_api_key'),
);
// 3. CRITICAL: Link AppRefer device ID to RevenueCat
// This is REQUIRED for purchase attribution via webhooks
await Purchases.shared.setAttributes({"appreferId": attribution.deviceId});
// 4. Optional: Link RevenueCat user ID to AppRefer
final customerInfo = await Purchases.getCustomerInfo();
await AppReferSDK.setUserId(customerInfo.originalAppUserId);
runApp(const MyApp());
}
// Later, after user signs up...
Future<void> onUserSignup(String email, String firstName) async {
// 5. Send advanced matching data (hashed on-device)
await AppReferSDK.setAdvancedMatching(
email: email,
firstName: firstName,
);
// 6. Track the signup event
await AppReferSDK.trackEvent('signup');
}Best Practices#
- Call
configure()as early as possible in the app lifecycle (beforerunApp()). The sooner you call it, the more accurate the attribution window. - Always set
appreferIdon RevenueCat immediately afterconfigure(). This is the primary mechanism for matching RevenueCat purchase webhooks back to AppRefer attributions. Without it, purchases cannot be attributed. - Optionally call
setUserId()with the RevenueCat user ID for additional matching. This supplementsappreferIdbut is not a replacement for it. - Call
setAdvancedMatching()after the user signs up or logs in. More matching data means higher match rates on Meta and other networks. - Use
pk_test_keys during development. Sandbox events are completely isolated from production and never forwarded to ad networks. - Enable
debug: truein development to see verbose logs. Disable it in production.
Requirements#
| Platform | Minimum Version |
|---|---|
| Flutter | 3.0+ |
| Dart | 3.0+ |
| iOS | 14+ |
| Android | API 21+ (Lollipop) |