Unity plugin V6 integration guide—API reference

At a glance: Technical descriptions of classes and methods included in the AppsFlyer SDK. 

 Related reading

For a complete picture of integrating the Unity plugin with your apps, be sure to read these articles:

Android and iOS APIs

initSDK

Description

Initialize the plugin with the dev key and app ID. The dev key is required for all app. The app ID is required for iOS only. If your app is for Android only, pass null for the app ID. The game object contains the IAppsFlyerConversionData interface.

Method signature

void initSDK(string devKey, string appID, MonoBehaviour gameObject)

Example

AppsFlyer.initSDK("dev_key", "app_id"); // without deeplinking
            AppsFlyer.initSDK("dev_key", "app_id", this); // with deeplinking

startSDK

Description

Once this API is invoked, the SDK will start, sessions will be immediately sent, and all background foreground transitions will record a session.

Method signature

void startSDK()

Example

AppsFlyer.startSDK();

sendEvent

Description

Recording of in-app events is performed by calling sendEvent with the event name and value parameters.

Method signature

void sendEvent(string eventName, Dictionary<string, string> eventValues)

Example

Dictionary<string, string> 
            eventValues = new Dictionary<string, string>();
            eventValues.Add(AFInAppEvents.CURRENCY, "USD");
            eventValues.Add(AFInAppEvents.REVENUE, "0.99");
            eventValues.Add("af_quantity", "1");
            AppsFlyer.sendEvent(AFInAppEvents.PURCHASE, eventValues);

stopSDK

Description

In some extreme cases, you might want to shut down all SDK functions for regulators and privacy compliance requirements. To do so use stopSDK API. Once invoked, the SDK no longer communicates with AppsFLyer servers and stops functioning.

Method signature

void stopSDK(bool isSDKStopped)

Example

AppsFlyer.stopSDK(true);

isSDKStopped

Description

Was the stopSDK (boolean) API set to true.

Method signature

bool isSDKStopped()

Example

if (!AppsFlyer.isSDKStopped())
{

}

getSdkVersion

Description

Get the AppsFlyer SDK version

Method signature

string getSdkVersion()

Example

string version = AppsFlyer.getSdkVersion();

setIsDebug

Description

Enables debug logs

Method signature

void setIsDebug(bool shouldEnable)

Example

AppsFlyer.setIsDebug(true);

setCustomerUserID

Description

Set the customer user ID (CUID). 

Method signature

void setCustomerUserId(string id)

Example

AppsFlyer.setCustomerUserId("custom_user_id");

setAppInviteOneLink

Description

Set the OneLink template ID used to create custom attribution links for user invites.

Method signature

setAppInviteOneLinkID(string oneLinkId)

Example

AppsFlyer.setAppInviteOneLinkID("abcd");

setAdditionalData

Description

Add additional data to send to external partner platforms.

Method signature

setAdditionalData(Dictionary<string, string> customData)

Example

Dictionary<string, string> customData = new Dictionary<string, string>();
customData.Add("custom1", "someData");
AppsFlyer.setAdditionalData(customData);

setResolveDeepLinkURLs

Description

Resolve OneLink from click domains. Learn more about resolving wrapped deep link URLs.

Method signature

setResolveDeepLinkURLs(params string[] urls)

Example

AppsFlyer.setResolveDeepLinkURLs("example.com", "click.example.com");

setOneLinkCustomDomain

Description

Advertisers can use this method to set vanity OneLink domains.

Method signature

void setOneLinkCustomDomain(params string[] domains)

Example

AppsFlyer.setOneLinkCustomDomain("test.domain", "test2.domain");

setcurrencyCode

Description

Use for events with revenue. Accepts ISO currency codes.

Method signature

public void setCurrencyCode(String currencyCode);

Example

AppsFlyer.setCurrencyCode("GBP");

recordLocation

Description

Manually record user location

Method signature

void recordLocation(double latitude, double longitude)

Example

AppsFlyer.recordLocation(40.7128, 74.0060);

anonymizeUser

Description

Use during initialization to explicitly anonymize the user install, events, and sessions. To stop anonymizing, call anonymizeUser again, set to false.

Method signature

void anonymizeUser(bool shouldAnonymizeUser)

Example

AppsFlyer.anonymizeUser(true);

getAppsFlyerId

Description

To get the AppsFlyer Unique ID for a new install.

Method signature

string getAppsFlyerId()

Example

string uid = AppsFlyer.getAppsFlyerId();

setMinTimeBetweenSessions

Description

By default, at least 5 seconds must elapse between 2 app launches to count as separate sessions. Set a custom value for the minimum required time between sessions.

Method signature

void setMinTimeBetweenSessions(int seconds)

Example

AppsFlyer.setMinTimeBetweenSessions(4);

setUserEmails

Description

Set user emails and encrypt them.

Method signature

void setUserEmails(EmailCryptType cryptMethod, params string[] emails)

Example

AppsFlyer.setUserEmails(EmailCryptType.EmailCryptTypeSHA256, "test1@test1.com", "test2@test2.com");

setHost

Description

Set a custom host

Method signature

void setHost(string hostPrefixName, string hostName)

Example

AppsFlyer.setHost("hostPrefixName","hostName");

getConversionData

Description

Register a conversion data listener to allow access to user attribution data in real-time for every new install, directly from the SDK level. By doing so, serve users with personalized content or send them to specific activities within the app, which can significantly enhance their engagement with your app.

Method signature

void getConversionData(string objectName);

Example

AppsFlyer.getConversionData(gameObject.name);

attributeAndOpenStore

Description

To attribute the click and launch the app store app page.

Method signature

void attributeAndOpenStore
(string appID, string campaign,
Dictionary<string, string> userParams, MonoBehaviour gameObject)

Example


Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("af_sub1", "val");
parameters.Add("custom_param", "val2");
AppsFlyer.attributeAndOpenStore("123456789", "test campaign", parameters, this);            

recordCrossPromoteImpression

Description

To attribute an impression, use the following API call. Make sure to use the promoted App ID as it appears within the AppsFlyer dashboard.

Method signature

void recordCrossPromoteImpression(string appID, string campaign);

Example

Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("af_sub1", "val");
parameters.Add("custom_param", "val2");
AppsFlyer.recordCrossPromoteImpression("appID", "campaign", parameters);

generateUserInviteLink

Description

The LinkGenerator class builds the invite URL according to various setter methods, which allow passing on additional information on the click.

Method signature

void generateUserInviteLink
(Dictionary<string, string> parameters, MonoBehaviour gameObject)

Example

AppsFlyer.generateUserInviteLink(params, this);

onAppOpenAttribution

Description

Get deep-link data when an app opens via a deep link.

Method signature

public void onAppOpenAttribution(string validateResult)

Example

public void onAppOpenAttribution(string attributionData)
            { AppsFlyer.AFLog("onAppOpenAttribution", attributionData);
            Dictionary<string, object> attributionDataDictionary = AppsFlyer.CallbackStringToDictionary(attributionData);
            // add direct deeplink logic here }

onAppOpenAttributionFailure

Description

Obtains errors while getting deep-link data.

Method signature

public void onAppOpenAttributionFailure (string error)

Example

public void onAppOpenAttributionFailure(string error)
            {AppsFlyer.AFLog("onAppOpenAttributionFailure", error); }

onConversionDataSuccess

Description

Method used to get conversion data. Useful for deferred deep linking. Learn more.

  • From SDK v5: onConversionDataSuccess
  • Before SDK v5: onConversionDataReceived 

Method signature

public void onConversionDataSuccess(string conversionData)

Example

public void onConversionDataSuccess(string conversionData)
            {
            AppsFlyer.AFLog("onConversionDataSuccess", conversionData);
            Dictionary<string, object> conversionDataDictionary = AppsFlyer.CallbackStringToDictionary(conversionData);
            // add deferred deeplink logic here
            }

onConversionDataFail

Description

Handles errors when failing to get conversion data from installs.

Method signature

public void onConversionDataFail(string error)

Example

public void onConversionDataFail(string error)
            {
            AppsFlyer.AFLog("onConversionDataFail", error);
            }

onInviteLinkGenerated

Description

Success callback for generating OneLink URLs.

Method signature

public void onInviteLinkGenerated(string link)

Example

public void onInviteLinkGenerated(string link)
            {
            }

onInviteLinkGeneratedFailure

Description

Error callback for generating OneLink URLs.

Method signature

public void onInviteLinkGeneratedFailure(string error)

Example

public void onInviteLinkGeneratedFailure(string error)
            {
            AppsFlyer.AFLog("onInviteLinkGeneratedFailure", error);
            }

didFinishValidateReceipt

Description

Success callback for validateAndSendInAppPurchase API.

For Android: Callback returns "Validate success".
For iOS: Callback returns a JSON string from the Apple verifyReceipt API.

Method signature

public void didFinishValidateReceipt(string result)

Example

public void didFinishValidateReceipt(string result)
            {
            }

didFinishValidateReceiptWithError

Description

Error callback for validating receipts.

Method signature

public void didFinishValidateReceiptWithError(string error)

Example

public void didFinishValidateReceiptWithError(string error)
            {
            }

setPhoneNumber

Description

Used to set the user phone number.

Method signature

void setPhoneNumber(string phoneNumber)

Example

AppsFlyer.setPhoneNumber("4166358181");

setSharingFilterForAllPartners (deprecated)

Description

Used by advertisers to exclude all networks/integrated partners from getting data. Learn more

Method signature

void setSharingFilterForAllPartners()

Example

AppsFlyer.setSharingFilterForAllPartners();

setSharingFilter (deprecated)

Description

Used by advertisers to set some (one or more) networks/integrated partners to exclude from getting data. Learn more

Method signature

void setSharingFilter(params string[] partners)

Example

AppsFlyer.setSharingFilter
("googleadwords_int","snapchat_int","doubleclick_int");

setSharingFilterForPartners

Description

Used by advertisers to set some (one or more) networks/integrated partners to exclude from getting data.

Method signature

void setSharingFilterForPartners(params string[] partners)

Example

AppsFlyer.setSharingFilterForPartners("partner1_int"); // Single partner
AppsFlyer.setSharingFilterForPartners("partner1_int", "partner2_int");// Multiple partners
AppsFlyer.setSharingFilterForPartners("all"); // All partners
AppsFlyer.setSharingFilterForPartners(""); // Reset list (default)
AppsFlyer.setSharingFilterForPartners(); // Reset list (default)

Android APIs

updateServerUninstallToken

Description

Manually pass the Firebase Device Token for uninstall measurement.

Method signature

void updateServerUninstallToken(string token)

Example

#if UNITY_ANDROID && !UNITY_EDITOR
            AppsFlyerAndroid.updateServerUninstallToken("token");
            #endif

setImeiData

Description

To explicitly send the IMEI to AppsFlyer.

Method signature

void setImeiData(string imei)

Example

#if UNITY_ANDROID && !UNITY_EDITOR
            AppsFlyerAndroid.setImeiData("imei");
            #endif

setAndroidIdData

Description

 To explicitly send Android ID to AppsFlyer.

Method signature

void setAndroidIdData(string androidId)

Example

#if UNITY_ANDROID && !UNITY_EDITOR
            AppsFlyerAndroid.setAndroidIdData("androidId");
            #endif

waitForCustomerUserId

Description

This feature makes sure that the SDK doesn't begin functioning until the customer user ID is provided.

Method signature

void waitForCustomerUserId(bool wait)

Example

#if UNITY_ANDROID && !UNITY_EDITOR
            AppsFlyerAndroid.waitForCustomerUserId(true);
            #endif

setCustomerIdAndStartSDK

Description

To provide the SDK with the relevant customer user ID and trigger the SDK to begin its normal activity.

Method signature

void setCustomerIdAndStartSDK(string id)

Example

#if UNITY_ANDROID && !UNITY_EDITOR
            AppsFlyerAndroid.setCustomerIdStartSDK("id");
            #endif

getOutOfStore

Description

Get the current AF_STORE value.

Method signature

string getOutOfStore()

Example

#if UNITY_ANDROID && !UNITY_EDITOR
            string af_store = AppsFlyerAndroid.getOutOfStore();
            #endif

setOutOfStore

Description

Manually set the AF_STORE value.

Method signature

void setOutOfStore(string sourceName)

Example

#if UNITY_ANDROID && !UNITY_EDITOR
            AppsFlyerAndroid.setOutOfStore("sourceName");
            #endif

setCollectAndroidID

Description

Opt-out of Android ID collection. If the app does NOT contain Google Play Services, Android ID is collected by the SDK. However, apps with Google play services should avoid Android ID collection as this violates the Google Play policy.

Method signature

void setCollectAndroidID(bool isCollect)

Example

#if UNITY_ANDROID && !UNITY_EDITOR
            AppsFlyerAndroid.setCollectAndroidID(true);
            #endif

setCollectIMEI

Description

Opt-out of IMEI collection. If the app does NOT contain Google Play Services, device IMEI is collected by the plugin. However, apps with Google play services should avoid IMEI collection as this violates the Google Play policy.

Method signature

void setCollectIMEI(bool isCollect)

Example

#if UNITY_ANDROID && !UNITY_EDITOR
            AppsFlyerAndroid.setCollectIMEI(true);
            #endif

setIsUpdate

Description

Manually set that the application was updated.

Method signature

void setIsUpdate(bool isUpdate)

Example

#if UNITY_ANDROID && !UNITY_EDITOR
            AppsFlyerAndroid.setIsUpdate(true);
            #endif

setPreinstallAttribution

Description

Specify the manufacturer or media source name to which the pre-install is attributed.

Method signature

void setPreinstallAttribution
(string mediaSource, string campaign, string siteId)

Example

#if UNITY_ANDROID && !UNITY_EDITOR
            AppsFlyerAndroid.setPreinstallAttribution("mediaSource", "campaign", "siteId");
            #endif

isPreInstalledApp

Description

Boolean indicator for pre-install by the manufacturer.

Method signature

bool isPreInstalledApp()

Example

#if UNITY_ANDROID && !UNITY_EDITOR
            if (AppsFlyerAndroid.isPreInstalledApp())
            {
            }
            #endif

 getAttributionId

Description

Get the Facebook attribution ID, if one exists.

Method signature

string getAttributionId()

Example

#if UNITY_ANDROID && !UNITY_EDITOR
            string attributionId = AppsFlyerAndroid.getAttributionId();
            #endif

handlePushNotifications

Description

Get push notifications recorded.

Method signature

void handlePushNotifications()

Example

#if UNITY_ANDROID && !UNITY_EDITOR
            AppsFlyerAndroid.handlePushNotifications();
            #endif

validateAndSendInAppPurchase

Description

API for server verification of in-app purchases. An af_purchase event with the relevant values will be automatically sent if the validation is successful.

Method signature

void validateAndSendInAppPurchase
(string publicKey, string signature, string purchaseData,
string price, string currency, Dictionary<string, string> additionalParameters,
MonoBehaviour gameObject)

Example

#if UNITY_ANDROID && !UNITY_EDITOR
            AppsFlyerAndroid.validateAndSendInAppPurchase(
            "publicKey", 
            "signature", 
            "purchaseData", 
            "price", 
            "currency", 
            null, 
            this);
            #endif

iOS APIs

setShouldCollectDeviceName

Description

Set this flag to true, to collect the current device name (for example, "My iPhone"). The default value is "false". 

Method signature

void setShouldCollectDeviceName(bool shouldCollectDeviceName)

Example

#if UNITY_IOS && !UNITY_EDITOR
            AppsFlyeriOS.setShouldCollectDeviceName(true);
            #endif

setDisableCollectIAd

Description

Opt-out for Apple Search Ads attributions.

Method signature

void setDisableCollectIAd(bool disableCollectIAd)

Example

#if UNITY_IOS && !UNITY_EDITOR
            AppsFlyeriOS.setDisableCollectIAd(true);
            #endif

setUseReceiptValidationSandbox

Description

In-app purchase receipt validation in Apple environment (production or sandbox). The default value is "false".

Method signature

void setUseReceiptValidationSandbox(bool useReceiptValidationSandbox)

Example

#if UNITY_IOS && !UNITY_EDITOR
            AppsFlyeriOS.setUseReceiptValidationSandbox(true);
            #endif

setUseUninstallSandbox

Description

Set this flag to test uninstall in Apple environment (production or sandbox). The default value is "false".

Method signature

void setUseUninstallSandbox(bool useUninstallSandbox)

Example

#if UNITY_IOS && !UNITY_EDITOR
            AppsFlyeriOS.setUseUninstallSandbox(true);
            #endif

validateAndSendInAppPurchase

Description

To send and validate in-app purchases, call this method from the processPurchase method.

Method signature

void validateAndSendInAppPurchase
(string productIdentifier, string price, string currency, string tranactionId,
Dictionary<string, string> additionalParameters, MonoBehaviour gameObject)

Example

#if UNITY_IOS && !UNITY_EDITOR
            AppsFlyeriOS.validateAndSendInAppPurchase(
            "productIdentifier", 
            "price", 
            "currency", 
            "tranactionId", 
            null, 
            this);
            #endif

registerUninstall

Description

Register for remote notification and provide AppsFlyer the push device token.

Method signature

void registerUninstall(byte[] deviceToken)

Example

private bool tokenSent;
            void Update()
            {
            #if UNITY_IOS && !UNITY_EDITOR
            if (!tokenSent)
            {
            byte[] token = UnityEngine.iOS.NotificationServices.deviceToken;
            if (token != null)
            {
            AppsFlyeriOS.registerUninstall(token);
            tokenSent = true;
            }
            }
            #endif
            }

handleOpenUrl

Description

Used to manually record deep linking.

Method signature

void handleOpenUrl(string url, string sourceApplication, string annotation)

Example

#if UNITY_IOS && !UNITY_EDITOR
            AppsFlyeriOS.handleOpenUrl(string url, string sourceApplication, string annotation);
            #endif

onOpenStoreLinkGenerated

Description

Allows you to utilize the StoreKit component to open the App Store while remaining in the context of your app. Learn more.

Method signature

public void onOpenStoreLinkGenerated(string link)

Example

public void onOpenStoreLinkGenerated(string link)
            {
            }

disableSKAdNetwork

Description

Allows you to disable SKAdNetwork attribution. Set to "true" to disable.

Method signature

bools disableSKAdNetwork(int isDisabled)

Example

#if UNITY_IOS && !UNITY_EDITOR
    AppsFlyeriOS.disableSKAdNetwork(true);
#endif

waitForATTUserAuthorizationWithTimeoutInterval

Description

Used if you want to request user authorization via a popup before accessing app-related data for recording the user or the device (for example, IDFA). If the user opts-in, the IDFA will be passed to the SDK. The timeout interval gives the user a set amount of time to opt-in to IDFA collection. After the timer expires, the IDFA is not collected.

Method signature

void waitForATTUserAuthorizationWithTimeoutInterval(int timeoutInterval)

Example

#if UNITY_IOS && !UNITY_EDITOR
    AppsFlyeriOS.waitForATTUserAuthorizationWithTimeoutInterval(60);
#endif