using System;
using System.Collections.Generic;
using UnityEngine;
namespace AppsFlyerSDK
{
public class AppsFlyer : MonoBehaviour
{
public static readonly string kAppsFlyerPluginVersion = "6.4.0";
public static string CallBackObjectName = null;
private static EventHandler onRequestResponse;
private static EventHandler onInAppResponse;
private static EventHandler onDeepLinkReceived;
///
/// Initialize the AppsFlyer SDK with your devKey and appID.
/// The dev key is required on all platforms, and the appID is required for iOS.
/// If you app is for Android only pass null for the appID.
///
/// AppsFlyer's Dev-Key, which is accessible from your AppsFlyer account under 'App Settings' in the dashboard.
/// Your app's Apple ID.
///
///
/// AppsFlyer.initSDK("K2***********99", "41*****44"");
///
///
public static void initSDK(string devKey, string appID)
{
initSDK(devKey, appID, null);
}
///
/// Initialize the AppsFlyer SDK with your devKey and appID.
/// The dev key is required on all platforms, and the appID is required for iOS.
/// If you app is for Android only pass null for the appID.
///
/// AppsFlyer's Dev-Key, which is accessible from your AppsFlyer account under 'App Settings' in the dashboard.
/// Your app's Apple ID.
/// pass the script of the game object being used.
///
///
/// AppsFlyer.initSDK("K2***********99", 41*****44, this);
///
///
public static void initSDK(string devKey, string appID, MonoBehaviour gameObject)
{
if(gameObject != null)
{
CallBackObjectName = gameObject.name;
}
#if UNITY_IOS && !UNITY_EDITOR
AppsFlyeriOS.setAppsFlyerDevKey(devKey);
AppsFlyeriOS.setAppleAppID(appID);
if(gameObject != null)
{
AppsFlyeriOS.getConversionData(gameObject.name);
}
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.initSDK(devKey, gameObject);
#elif UNITY_WSA_10_0 && !UNITY_EDITOR
AppsFlyerWindows.InitSDK(devKey, appID, gameObject);
if (gameObject != null)
{
AppsFlyerWindows.GetConversionData(gameObject.name);
}
#else
#endif
}
///
/// Once this API is invoked, our SDK will start.
/// Once the API is called a sessions will be immediately sent, and all background forground transitions will send a session.
///
public static void startSDK()
{
#if UNITY_IOS && !UNITY_EDITOR
AppsFlyeriOS.startSDK(onRequestResponse != null, CallBackObjectName);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.startSDK(onRequestResponse != null, CallBackObjectName);
#elif UNITY_WSA_10_0 && !UNITY_EDITOR
AppsFlyerWindows.Start();
#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_IOS && !UNITY_EDITOR
AppsFlyeriOS.sendEvent(eventName, eventValues, onInAppResponse != null, CallBackObjectName);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.sendEvent(eventName, eventValues, onInAppResponse != null, CallBackObjectName);
#elif UNITY_WSA_10_0 && !UNITY_EDITOR
AppsFlyerWindows.LogEvent(eventName, eventValues);
#else
#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.
///
/// should sdk be stopped.
public static void stopSDK(bool isSDKStopped)
{
#if UNITY_IOS && !UNITY_EDITOR
AppsFlyeriOS.stopSDK(isSDKStopped);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.stopSDK(isSDKStopped);
#else
#endif
}
//
/// Was the stopSDK(boolean) API set to true.
///
/// boolean isSDKStopped.
public static bool isSDKStopped()
{
#if UNITY_IOS && !UNITY_EDITOR
return AppsFlyeriOS.isSDKStopped();
#elif UNITY_ANDROID && !UNITY_EDITOR
return AppsFlyerAndroid.isSDKStopped();
#else
return false;
#endif
}
///
/// Get the AppsFlyer SDK version used in app.
///
/// The current SDK version.
public static string getSdkVersion()
{
#if UNITY_IOS && !UNITY_EDITOR
return AppsFlyeriOS.getSDKVersion();
#elif UNITY_ANDROID && !UNITY_EDITOR
return AppsFlyerAndroid.getSdkVersion();
#else
return "";
#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_IOS && !UNITY_EDITOR
AppsFlyeriOS.setIsDebug(shouldEnable);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.setIsDebug(shouldEnable);
#else
#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_IOS && !UNITY_EDITOR
AppsFlyeriOS.setCustomerUserID(id);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.setCustomerUserId(id);
#elif UNITY_WSA_10_0 && !UNITY_EDITOR
AppsFlyerWindows.SetCustomerUserId(id);
#else
#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_IOS && !UNITY_EDITOR
// 无接口
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.waitForCustomerUserId(wait);
#elif UNITY_WSA_10_0 && !UNITY_EDITOR
// 无接口
#else
#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_IOS && !UNITY_EDITOR
// 无接口
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.setCustomerIdAndStartSDK(id);
#elif UNITY_WSA_10_0 && !UNITY_EDITOR
// 无接口
#else
#endif
}
///
/// Set the OneLink ID that should be used for User-Invite-API.
/// 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_IOS && !UNITY_EDITOR
AppsFlyeriOS.setAppInviteOneLinkID(oneLinkId);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.setAppInviteOneLinkID(oneLinkId);
#else
#endif
}
///
/// Set additional data to be sent to AppsFlyer.
///
/// additional data Dictionary.
public static void setAdditionalData(Dictionary customData)
{
#if UNITY_IOS && !UNITY_EDITOR
AppsFlyeriOS.setAdditionalData(customData);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.setAdditionalData(customData);
#else
#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_IOS && !UNITY_EDITOR
AppsFlyeriOS.setResolveDeepLinkURLs(urls);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.setResolveDeepLinkURLs(urls);
#else
#endif
}
///
/// Advertisers can use this method to set vanity onelink domains.
///
/// Array of domains.
public static void setOneLinkCustomDomain(params string[] domains)
{
#if UNITY_IOS && !UNITY_EDITOR
AppsFlyeriOS.setOneLinkCustomDomains(domains);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.setOneLinkCustomDomain(domains);
#else
#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_IOS && !UNITY_EDITOR
AppsFlyeriOS.setCurrencyCode(currencyCode);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.setCurrencyCode(currencyCode);
#else
#endif
}
///
/// Manually record the location of the user.
///
/// latitude as double.
/// longitude as double.
public static void recordLocation(double latitude, double longitude)
{
#if UNITY_IOS && !UNITY_EDITOR
AppsFlyeriOS.recordLocation(latitude, longitude);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.recordLocation(latitude, longitude);
#else
#endif
}
///
/// Anonymize user Data.
/// Use this API during the SDK Initialization to explicitly anonymize a user's installs, events and sessions.
/// Default is false.
///
/// shouldAnonymizeUser boolean.
public static void anonymizeUser(bool shouldAnonymizeUser)
{
#if UNITY_IOS && !UNITY_EDITOR
AppsFlyeriOS.anonymizeUser(shouldAnonymizeUser);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.anonymizeUser(shouldAnonymizeUser);
#else
#endif
}
///
/// Get AppsFlyer's unique device ID which is created for every new install of an app.
///
/// AppsFlyer's unique device ID.
public static string getAppsFlyerId()
{
#if UNITY_IOS && !UNITY_EDITOR
return AppsFlyeriOS.getAppsFlyerId();
#elif UNITY_ANDROID && !UNITY_EDITOR
return AppsFlyerAndroid.getAppsFlyerId();
#else
return "";
#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_IOS && !UNITY_EDITOR
AppsFlyeriOS.setMinTimeBetweenSessions(seconds);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.setMinTimeBetweenSessions(seconds);
#else
#endif
}
///
/// Set a custom host.
///
/// Host prefix.
/// Host name.
public static void setHost(string hostPrefixName, string hostName)
{
#if UNITY_IOS && !UNITY_EDITOR
AppsFlyeriOS.setHost(hostName, hostPrefixName);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.setHost(hostPrefixName, hostName);
#else
#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_IOS && !UNITY_EDITOR
AppsFlyeriOS.setUserEmails(cryptMethod, emails.Length, emails);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.setUserEmails(cryptMethod, emails);
#else
#endif
}
///
/// Set the user phone number.
///
/// phoneNumber string
public static void setPhoneNumber(string phoneNumber)
{
#if UNITY_IOS && !UNITY_EDITOR
AppsFlyeriOS.setPhoneNumber(phoneNumber);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.setPhoneNumber(phoneNumber);
#else
#endif
}
///
/// Used by advertisers to exclude all networks/integrated partners from getting data.
///
[Obsolete("Please use setSharingFilterForPartners api")]
public static void setSharingFilterForAllPartners()
{
#if UNITY_IOS && !UNITY_EDITOR
AppsFlyeriOS.setSharingFilterForAllPartners();
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.setSharingFilterForAllPartners();
#else
#endif
}
///
/// Used by advertisers to set some (one or more) networks/integrated partners to exclude from getting data.
///
/// partners to exclude from getting data
[Obsolete("Please use setSharingFilterForPartners api")]
public static void setSharingFilter(params string[] partners)
{
#if UNITY_IOS && !UNITY_EDITOR
AppsFlyeriOS.setSharingFilter(partners);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.setSharingFilter(partners);
#else
#endif
}
///
/// Lets you configure how which partners should the SDK exclude from data-sharing.
///
/// partners to exclude from getting data
public static void setSharingFilterForPartners(params string[] partners)
{
#if UNITY_IOS && !UNITY_EDITOR
AppsFlyeriOS.setSharingFilterForPartners(partners);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.setSharingFilterForPartners(partners);
#else
#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.
///
///
///
/// AppsFlyer.getConversionData(this.name);
///
///
public static void getConversionData(string objectName)
{
#if UNITY_IOS && !UNITY_EDITOR
AppsFlyeriOS.getConversionData(objectName);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.getConversionData(objectName);
#elif UNITY_WSA_10_0 && !UNITY_EDITOR
AppsFlyerWindows.GetConversionData("");
#else
#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
///
///
/// Dictionary parameters = new Dictionary();
/// parameters.Add("af_sub1", "val");
/// parameters.Add("custom_param", "val2");
/// AppsFlyer.attributeAndOpenStore("123456789", "test campaign", parameters, this);
///
///
public static void attributeAndOpenStore(string appID, string campaign, Dictionary userParams, MonoBehaviour gameObject)
{
#if UNITY_IOS && !UNITY_EDITOR
AppsFlyeriOS.attributeAndOpenStore(appID, campaign, userParams, gameObject);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.attributeAndOpenStore(appID, campaign, userParams);
#else
#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_IOS && !UNITY_EDITOR
AppsFlyeriOS.recordCrossPromoteImpression(appID, campaign, parameters);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.recordCrossPromoteImpression(appID, campaign, parameters);
#else
#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_IOS && !UNITY_EDITOR
AppsFlyeriOS.generateUserInviteLink(parameters, gameObject);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.generateUserInviteLink(parameters, gameObject);
#else
#endif
}
///
/// Use this method if you’re integrating your app with push providers
/// that don’t use the default push notification JSON schema the SDK expects.
/// See docs for more info.
///
/// array of nested json path
public static void addPushNotificationDeepLinkPath(params string[] paths)
{
#if UNITY_IOS && !UNITY_EDITOR
AppsFlyeriOS.addPushNotificationDeepLinkPath(paths);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.addPushNotificationDeepLinkPath(paths);
#else
#endif
}
///
/// Subscribe for unified deeplink API.
/// This is called automatically from OnDeepLinkReceived.
/// CallBackObjectName is set in the init method.
///
public static void subscribeForDeepLink()
{
#if UNITY_IOS && !UNITY_EDITOR
AppsFlyeriOS.subscribeForDeepLink(CallBackObjectName);
#elif UNITY_ANDROID && !UNITY_EDITOR
AppsFlyerAndroid.subscribeForDeepLink(CallBackObjectName);
#else
#endif
}
///
/// Start callback event.
///
public static event EventHandler OnRequestResponse
{
add
{
onRequestResponse += value;
}
remove
{
onRequestResponse -= value;
}
}
///
/// In-App callback event.
///
public static event EventHandler OnInAppResponse
{
add
{
onInAppResponse += value;
}
remove
{
onInAppResponse -= value;
}
}
///
/// Unified DeepLink Event
///
public static event EventHandler OnDeepLinkReceived
{
add
{
onDeepLinkReceived += value;
subscribeForDeepLink();
}
remove
{
onDeepLinkReceived -= value;
}
}
///
/// Used to accept start callback from UnitySendMessage on native side.
///
public void inAppResponseReceived(string response)
{
if (onInAppResponse != null)
{
onInAppResponse.Invoke(null, parseRequestCallback(response));
}
}
///
/// Used to accept in-app callback from UnitySendMessage on native side.
///
public void requestResponseReceived(string response)
{
if (onRequestResponse != null)
{
onRequestResponse.Invoke(null, parseRequestCallback(response));
}
}
///
/// Used to accept deeplink callback from UnitySendMessage on native side.
///
public void onDeepLinking(string response)
{
DeepLinkEventsArgs args = new DeepLinkEventsArgs(response);
if (onDeepLinkReceived != null)
{
onDeepLinkReceived.Invoke(null, args);
}
}
private static AppsFlyerRequestEventArgs parseRequestCallback(string response)
{
int responseCode = 0;
string errorDescription = "";
try
{
Dictionary dictionary = CallbackStringToDictionary(response);
var errorResponse = dictionary.ContainsKey("errorDescription") ? dictionary["errorDescription"] : "";
errorDescription = (string)errorResponse;
responseCode = (int)(long) dictionary["statusCode"];
}
catch (Exception e)
{
AFLog("parseRequestCallback", String.Format("{0} Exception caught.", e));
}
return new AppsFlyerRequestEventArgs(responseCode, errorDescription);
}
///
/// Helper method to convert json strings to dictionary.
///
/// json string
/// dictionary representing the input json string.
public static Dictionary CallbackStringToDictionary(string str)
{
return AFMiniJSON.Json.Deserialize(str) as Dictionary;
}
///
/// Helper method to log AppsFlyer events and callbacks.
///
/// method name
/// message to log
public static void AFLog(string methodName, string str)
{
Debug.Log(string.Format("AppsFlyer_Unity_v{0} {1} called with {2}", kAppsFlyerPluginVersion, methodName, str));
}
}
public enum EmailCryptType
{
// None
EmailCryptTypeNone = 0,
// SHA256
EmailCryptTypeSHA256 = 1,
}
}