using System;
using System.Collections.Generic;
using UnityEngine;
namespace AppsFlyerSDK
{
#if UNITY_ANDROID
public class AppsFlyerAndroid
{
private static AndroidJavaClass appsFlyerAndroid = new AndroidJavaClass("com.appsflyer.unity.AppsFlyerAndroidWrapper");
///
/// Use this method to init the sdk for the application.
/// Call this method before startSDK.
///
/// AppsFlyer's Dev-Key, which is accessible from your AppsFlyer account under 'App Settings' in the dashboard.
/// The current game object. This is used to get the conversion data callbacks. Pass null if you do not need the callbacks.
public static void initSDK(string devkey, MonoBehaviour gameObject)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("initSDK", devkey, gameObject ? gameObject.name : null);
#endif
}
///
/// Use this method to start the sdk for the application.
/// The AppsFlyer's Dev-Key must be provided.
///
/// AppsFlyer's Dev-Key, which is accessible from your AppsFlyer account under 'App Settings' in the dashboard.
public static void startSDK()
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("startTracking");
#endif
}
///
/// Once this API is invoked, our SDK no longer communicates with our servers and stops functioning.
/// In some extreme cases you might want to shut down all SDK activity due to legal and privacy compliance.
/// This can be achieved with the stopSDK API.
///
/// boolean should SDK be stopped.
public static void stopSDK(bool isSDKStopped)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("stopTracking", isSDKStopped);
#endif
}
///
/// Get the AppsFlyer SDK version used in app.
///
/// AppsFlyer SDK version.
public static string getSdkVersion()
{
#if !UNITY_EDITOR
return appsFlyerAndroid.CallStatic("getSdkVersion");
#else
return "";
#endif
}
///
/// Manually pass the Firebase / GCM Device Token for Uninstall measurement.
///
/// Firebase Device Token.
public static void updateServerUninstallToken(string token)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("updateServerUninstallToken", token);
#endif
}
///
/// Enables Debug logs for the AppsFlyer SDK.
/// Should only be set to true in development / debug.
///
/// shouldEnable boolean.
public static void setIsDebug(bool shouldEnable)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setIsDebug", shouldEnable);
#endif
}
///
/// By default, IMEI and Android ID are not collected by the SDK if the OS version is higher than KitKat (4.4)
/// and the device contains Google Play Services(on SDK versions 4.8.8 and below the specific app needed GPS).
/// Use this API to explicitly send IMEI to AppsFlyer.
///
/// device's IMEI.
public static void setImeiData(string aImei)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setImeiData", aImei);
#endif
}
///
/// By default, IMEI and Android ID are not collected by the SDK if the OS version is higher than KitKat(4.4)
/// and the device contains Google Play Services(on SDK versions 4.8.8 and below the specific app needed GPS).
/// Use this API to explicitly send Android ID to AppsFlyer.
///
/// device's Android ID.
public static void setAndroidIdData(string aAndroidId)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setAndroidIdData", aAndroidId);
#endif
}
///
/// Setting your own customer ID enables you to cross-reference your own unique ID with AppsFlyer’s unique ID and the other devices’ IDs.
/// This ID is available in AppsFlyer CSV reports along with Postback APIs for cross-referencing with your internal IDs.
///
/// Customer ID for client.
public static void setCustomerUserId(string id)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setCustomerUserId", id);
#endif
}
///
/// It is possible to delay the SDK Initialization until the customerUserID is set.
/// This feature makes sure that the SDK doesn't begin functioning until the customerUserID is provided.
/// If this API is used, all in-app events and any other SDK API calls are discarded, until the customerUserID is provided.
///
/// wait boolean.
public static void waitForCustomerUserId(bool wait)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("waitForCustomerUserId", wait);
#endif
}
///
/// Use this API to provide the SDK with the relevant customer user id and trigger the SDK to begin its normal activity.
///
/// Customer ID for client.
public static void setCustomerIdAndStartSDK(string id)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setCustomerIdAndTrack", id);
#endif
}
///
/// Get the current AF_STORE value.
///
/// AF_Store value.
public static string getOutOfStore()
{
#if !UNITY_EDITOR
return appsFlyerAndroid.CallStatic("getOutOfStore");
#else
return "";
#endif
}
///
/// Manually set the AF_STORE value.
///
/// value to be set.
public static void setOutOfStore(string sourceName)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setOutOfStore", sourceName);
#endif
}
///
/// Set the OneLink ID that should be used for User-Invites.
/// The link that is generated for the user invite will use this OneLink as the base link.
///
/// OneLink ID obtained from the AppsFlyer Dashboard.
public static void setAppInviteOneLinkID(string oneLinkId)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setAppInviteOneLinkID", oneLinkId);
#endif
}
///
/// Set additional data to be sent to AppsFlyer.
///
/// additional data Dictionary.
public static void setAdditionalData(Dictionary customData)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setAdditionalData", convertDictionaryToJavaMap(customData));
#endif
}
///
/// Set the user emails.
///
/// User emails.
public static void setUserEmails(params string[] emails)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setUserEmails", (object)emails);
#endif
}
///
/// Set the user phone number.
///
/// User phoneNumber.
public static void setPhoneNumber(string phoneNumber){
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setPhoneNumber", phoneNumber);
#endif
}
///
/// Set the user emails and encrypt them.
/// cryptMethod Encryption method:
/// EmailCryptType.EmailCryptTypeMD5
/// EmailCryptType.EmailCryptTypeSHA1
/// EmailCryptType.EmailCryptTypeSHA256
/// EmailCryptType.EmailCryptTypeNone
///
/// Encryption method.
/// User emails.
public static void setUserEmails(EmailCryptType cryptMethod, params string[] emails)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setUserEmails", getEmailType(cryptMethod), (object)emails);
#endif
}
///
/// Opt-out of collection of Android ID.
/// 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 is in violation of the Google Play policy.
///
/// boolean, false to opt-out.
public static void setCollectAndroidID(bool isCollect)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setCollectAndroidID", isCollect);
#endif
}
///
/// Opt-out of collection of IMEI.
/// If the app does NOT contain Google Play Services, device IMEI is collected by the SDK.
/// However, apps with Google play services should avoid IMEI collection as this is in violation of the Google Play policy.
///
/// boolean, false to opt-out.
public static void setCollectIMEI(bool isCollect)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setCollectIMEI", isCollect);
#endif
}
///
/// Advertisers can wrap AppsFlyer OneLink within another Universal Link.
/// This Universal Link will invoke the app but any deep linking data will not propagate to AppsFlyer.
///
/// Array of urls.
public static void setResolveDeepLinkURLs(params string[] urls)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setResolveDeepLinkURLs", (object)urls);
#endif
}
///
/// Advertisers can use this method to set vanity onelink domains.
///
/// Array of domains.
public static void setOneLinkCustomDomain(params string[] domains)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setOneLinkCustomDomain", (object)domains);
#endif
}
///
/// Manually set that the application was updated.
///
/// isUpdate boolean value.
public static void setIsUpdate(bool isUpdate)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setIsUpdate", isUpdate);
#endif
}
///
/// Setting user local currency code for in-app purchases.
/// The currency code should be a 3 character ISO 4217 code. (default is USD).
/// You can set the currency code for all events by calling the following method.
///
/// 3 character ISO 4217 code.
public static void setCurrencyCode(string currencyCode)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setCurrencyCode", currencyCode);
#endif
}
///
/// Manually record the location of the user.
///
/// latitude as double.
/// longitude as double.
public static void recordLocation(double latitude, double longitude)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("trackLocation", latitude, longitude);
#endif
}
///
/// Send an In-App Event.
/// In-App Events provide insight on what is happening in your app.
///
/// Event Name as String.
/// Event Values as Dictionary.
public static void sendEvent(string eventName, Dictionary eventValues)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("trackEvent", eventName, convertDictionaryToJavaMap(eventValues));
#endif
}
///
/// Anonymize user Data.
/// Use this API during the SDK Initialization to explicitly anonymize a user's installs, events and sessions.
/// Default is false.
///
/// isDisabled boolean.
public static void anonymizeUser(bool isDisabled)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setDeviceTrackingDisabled", isDisabled);
#endif
}
///
/// Enable the collection of Facebook Deferred AppLinks.
/// Requires Facebook SDK and Facebook app on target/client device.
/// This API must be invoked prior to initializing the AppsFlyer SDK in order to function properly.
///
/// should Facebook's deferred app links be processed by the AppsFlyer SDK.
public static void enableFacebookDeferredApplinks(bool isEnabled)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("enableFacebookDeferredApplinks", isEnabled);
#endif
}
///
/// Restrict reengagement via deep-link to once per each unique deep-link.
/// Otherwise deep re-occurring deep-links will be permitted for non-singleTask Activities and deep-linking via AppsFlyer deep-links.
/// The default value is false.
///
/// doConsume boolean.
public static void setConsumeAFDeepLinks(bool doConsume)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setConsumeAFDeepLinks", doConsume);
#endif
}
///
/// Specify the manufacturer or media source name to which the preinstall is attributed.
///
/// Manufacturer or media source name for preinstall attribution.
/// Campaign name for preinstall attribution.
/// Site ID for preinstall attribution.
public static void setPreinstallAttribution(string mediaSource, string campaign, string siteId)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setPreinstallAttribution", mediaSource, campaign, siteId);
#endif
}
///
/// Boolean indicator for preinstall by Manufacturer.
///
/// boolean isPreInstalledApp.
public static bool isPreInstalledApp()
{
#if !UNITY_EDITOR
return appsFlyerAndroid.CallStatic("isPreInstalledApp");
#else
return false;
#endif
}
///
/// Get the Facebook attribution ID, if one exists.
///
/// string Facebook attribution ID.
public static string getAttributionId()
{
#if !UNITY_EDITOR
return appsFlyerAndroid.CallStatic("getAttributionId");
#else
return "";
#endif
}
///
/// Get AppsFlyer's unique device ID is created for every new install of an app.
///
/// AppsFlyer's unique device ID.
public static string getAppsFlyerId()
{
#if !UNITY_EDITOR
return appsFlyerAndroid.CallStatic("getAppsFlyerId");
#else
return "";
#endif
}
///
/// 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.
///
/// License Key obtained from the Google Play Console.
/// data.INAPP_DATA_SIGNATURE
from onActivityResult(int requestCode, int resultCode, Intent data)
/// data.INAPP_PURCHASE_DATA
from onActivityResult(int requestCode, int resultCode, Intent data)
/// Purchase price, should be derived from skuDetails.getStringArrayList("DETAILS_LIST")
/// Purchase currency, should be derived from skuDetails.getStringArrayList("DETAILS_LIST")
/// additionalParameters Freehand parameters to be sent with the purchase (if validated).
public static void validateAndSendInAppPurchase(string publicKey, string signature, string purchaseData, string price, string currency, Dictionary additionalParameters, MonoBehaviour gameObject)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("validateAndTrackInAppPurchase", publicKey, signature, purchaseData, price, currency, convertDictionaryToJavaMap(additionalParameters), gameObject ? gameObject.name : null);
#endif
}
///
/// Was the stopSDK(boolean) API set to true.
///
/// boolean isSDKStopped.
public static bool isSDKStopped()
{
#if !UNITY_EDITOR
return appsFlyerAndroid.CallStatic("isTrackingStopped");
#else
return false;
#endif
}
///
/// Set a custom value for the minimum required time between sessions.
/// By default, at least 5 seconds must lapse between 2 app launches to count as separate 2 sessions.
///
/// minimum time between 2 separate sessions in seconds.
public static void setMinTimeBetweenSessions(int seconds)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setMinTimeBetweenSessions", seconds);
#endif
}
///
/// Set a custom host.
///
/// Host prefix.
/// Host name.
public static void setHost(string hostPrefixName, string hostName)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setHost", hostPrefixName, hostName);
#endif
}
///
/// Get the host name.
/// Default value is "appsflyer.com".
///
/// Host name.
public static string getHostName()
{
#if !UNITY_EDITOR
return appsFlyerAndroid.CallStatic("getHostName");
#else
return "";
#endif
}
///
/// Get the custom host prefix.
///
/// Host prefix.
public static string getHostPrefix()
{
#if !UNITY_EDITOR
return appsFlyerAndroid.CallStatic("getHostPrefix");
#else
return "";
#endif
}
///
/// Used by advertisers to exclude all networks/integrated partners from getting data.
///
public static void setSharingFilterForAllPartners()
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setSharingFilterForAllPartners");
#endif
}
///
/// Used by advertisers to set some (one or more) networks/integrated partners to exclude from getting data.
///
/// partners to exclude from getting data
public static void setSharingFilter(params string[] partners)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setSharingFilter", (object)partners);
#endif
}
///
/// Register a Conversion Data Listener.
/// Allows the developer to access the user attribution data in real-time for every new install, directly from the SDK level.
/// By doing this you can serve users with personalized content or send them to specific activities within the app,
/// which can greatly enhance their engagement with your app.
///
public static void getConversionData(string objectName)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("getConversionData", objectName);
#endif
}
///
/// Register a validation listener for the validateAndSendInAppPurchase API.
///
public static void initInAppPurchaseValidatorListener(MonoBehaviour gameObject)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("initInAppPurchaseValidatorListener", gameObject ? gameObject.name : null);
#endif
}
///
/// setCollectOaid
/// You must include the appsflyer oaid library for this api to work.
///
/// isCollect oaid - set fasle to opt out
public static void setCollectOaid(bool isCollect)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("setCollectOaid", isCollect);
#endif
}
///
/// Use the following API to attribute the click and launch the app store's app page.
///
/// promoted App ID
/// cross promotion campaign
/// additional user params
public static void attributeAndOpenStore(string promoted_app_id, string campaign, Dictionary userParams)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("attributeAndOpenStore", promoted_app_id, campaign, convertDictionaryToJavaMap(userParams));
#endif
}
///
/// To attribute an impression use the following API call.
/// Make sure to use the promoted App ID as it appears within the AppsFlyer dashboard.
///
/// promoted App ID.
/// cross promotion campaign.
/// parameters Dictionary.
public static void recordCrossPromoteImpression(string appID, string campaign, Dictionary parameters)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("recordCrossPromoteImpression", appID, campaign, convertDictionaryToJavaMap(parameters));
#endif
}
///
/// The LinkGenerator class builds the invite URL according to various setter methods which allow passing on additional information on the click.
/// See - https://support.appsflyer.com/hc/en-us/articles/115004480866-User-invite-attribution-
///
/// parameters Dictionary.
public static void generateUserInviteLink(Dictionary parameters, MonoBehaviour gameObject)
{
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("createOneLinkInviteListener", convertDictionaryToJavaMap(parameters), gameObject ? gameObject.name : null);
#endif
}
///
/// To measure push notifications as part of a retargeting campaign.
///
public static void handlePushNotifications(){
#if !UNITY_EDITOR
appsFlyerAndroid.CallStatic("handlePushNotifications");
#endif
}
///
/// Internal Helper Method.
///
private static AndroidJavaObject getEmailType(EmailCryptType cryptType)
{
AndroidJavaClass emailsCryptTypeEnum = new AndroidJavaClass("com.appsflyer.AppsFlyerProperties$EmailsCryptType");
AndroidJavaObject emailsCryptType;
switch (cryptType)
{
case EmailCryptType.EmailCryptTypeSHA256:
emailsCryptType = emailsCryptTypeEnum.GetStatic("SHA256");
break;
default:
emailsCryptType = emailsCryptTypeEnum.GetStatic("NONE");
break;
}
return emailsCryptType;
}
///
/// Internal Helper Method.
///
private static AndroidJavaObject convertDictionaryToJavaMap(Dictionary dictionary)
{
AndroidJavaObject map = new AndroidJavaObject("java.util.HashMap");
IntPtr putMethod = AndroidJNIHelper.GetMethodID(map.GetRawClass(), "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
if (dictionary != null)
{
foreach (var entry in dictionary)
{
AndroidJNI.CallObjectMethod(map.GetRawObject(), putMethod, AndroidJNIHelper.CreateJNIArgArray(new object[] { entry.Key, entry.Value }));
}
}
return map;
}
}
#endif
}