| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- using System;
- using System.Collections.Generic;
- using Facebook.Unity;
- using UnityEngine;
- #if UNITY_IOS && !UNITY_EDITOR
- using Unity.Advertisement.IosSupport;
- #endif
- [Serializable]
- public sealed class ActivityFacebookConfig
- {
- public bool enabled;
- public string appId;
- public string clientToken;
- }
- public static class ActivityFacebookService
- {
- const int MaxPendingEvents = 32;
- public const string DefaultAppId = "1899769634516757";
- public const string DefaultClientToken = "ddd8dfb525bbe84b81fd0bb212a76475";
- static bool initializing;
- static string activeAppId;
- static readonly List<PendingFacebookEvent> pendingEvents = new List<PendingFacebookEvent>();
- #if UNITY_IOS && !UNITY_EDITOR
- static bool iosSdkInitialized;
- #endif
- [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
- static void ResetStatics()
- {
- initializing = false;
- activeAppId = null;
- pendingEvents.Clear();
- #if UNITY_IOS && !UNITY_EDITOR
- iosSdkInitialized = false;
- #endif
- }
- public static void ApplyConfig(ActivityFacebookConfig webConfig)
- {
- #if UNITY_IOS && !UNITY_EDITOR
- if (!IosAppTrackingService.IsCompleted)
- {
- IosAppTrackingService.RunOnce(() => ApplyConfig(webConfig));
- return;
- }
- #endif
- ApplyConfigImmediate(webConfig);
- }
- static void ApplyConfigImmediate(ActivityFacebookConfig webConfig)
- {
- string webAppId = webConfig != null ? webConfig.appId : null;
- string webClientToken = webConfig != null ? webConfig.clientToken : null;
- if (webConfig != null && webConfig.enabled && HasCredentials(webAppId, webClientToken))
- {
- Debug.Log("[Activity][Facebook] Using Facebook config from the activity webview.");
- Configure(true, webAppId, webClientToken);
- return;
- }
- if (webConfig == null)
- Debug.Log("[Activity][Facebook] Facebook config from the activity webview is empty; using default settings.");
- else if (!webConfig.enabled)
- Debug.Log("[Activity][Facebook] Facebook config from the activity webview is disabled; using default settings.");
- else
- Debug.LogWarning("[Activity][Facebook] Facebook config from the activity webview is incomplete; using default settings.");
- string defaultAppId = DefaultAppId;
- string defaultClientToken = DefaultClientToken;
- bool defaultEnabled = true;
- ActivityWebViewSettings settings = ActivityWebViewSettings.Instance;
- if (settings != null)
- {
- defaultEnabled = settings.facebookEnabled;
- if (HasCredentials(settings.facebookAppId, settings.facebookClientToken))
- {
- defaultAppId = settings.facebookAppId.Trim();
- defaultClientToken = settings.facebookClientToken.Trim();
- }
- }
- Configure(defaultEnabled, defaultAppId, defaultClientToken);
- }
- public static void Configure(bool enabled, string appId, string clientToken)
- {
- if (!enabled)
- {
- Debug.Log("[Activity][Facebook] Facebook SDK is disabled.");
- return;
- }
- appId = appId != null ? appId.Trim() : string.Empty;
- clientToken = clientToken != null ? clientToken.Trim() : string.Empty;
- if (!HasCredentials(appId, clientToken))
- {
- Debug.LogWarning("[Activity][Facebook] Facebook App ID or Client Token is empty.");
- return;
- }
- if (FB.IsInitialized)
- {
- if (string.IsNullOrEmpty(activeAppId) && !string.IsNullOrEmpty(FB.AppId))
- activeAppId = FB.AppId;
- if (!string.IsNullOrEmpty(activeAppId) && activeAppId != appId)
- {
- Debug.LogWarning(
- "[Activity][Facebook] Ignoring App ID '" + appId +
- "'. The SDK is already initialized with '" + activeAppId +
- "'. Apply the config before the first FB.Init call.");
- }
- #if UNITY_IOS && !UNITY_EDITOR
- LogIosTrackingStatus();
- #endif
- FB.ActivateApp();
- return;
- }
- if (initializing)
- return;
- initializing = true;
- activeAppId = appId;
- #if UNITY_IOS && !UNITY_EDITOR
- try
- {
- FacebookNativeBridge.IOS_FBSetDynamicCredentials(appId, clientToken);
- if (!iosSdkInitialized)
- {
- FacebookNativeBridge.IOS_FBInitializeSDK();
- iosSdkInitialized = true;
- }
- }
- catch (Exception exception)
- {
- Debug.LogWarning("[Activity][Facebook] Failed to initialize the iOS Facebook SDK: " + exception.Message);
- }
- #endif
- try
- {
- Debug.Log("[Activity][Facebook] Initializing Facebook SDK. App ID: " + appId + ".");
- FB.Init(appId, clientToken, onInitComplete: OnInitComplete);
- }
- catch (Exception exception)
- {
- initializing = false;
- activeAppId = null;
- Debug.LogWarning("[Activity][Facebook] Facebook initialization failed: " + exception.Message);
- }
- }
- public static void LogEvent(string action, float value, string currency = "USD")
- {
- var payload = new ActivityFacebookEventPayload { value = value, currency = currency };
- LogEvent(action, JsonUtility.ToJson(payload));
- }
- public static void LogEvent(string action, string data)
- {
- if (string.IsNullOrWhiteSpace(action))
- return;
- if (!FB.IsInitialized)
- {
- if (pendingEvents.Count < MaxPendingEvents)
- {
- pendingEvents.Add(new PendingFacebookEvent(action, data));
- Debug.Log("[Activity][Facebook] SDK is not ready; queued event: " + action);
- }
- else
- {
- Debug.LogWarning("[Activity][Facebook] Event queue is full; dropped event: " + action);
- }
- return;
- }
- ActivityFacebookEventPayload payload = null;
- if (!string.IsNullOrWhiteSpace(data))
- {
- try
- {
- payload = JsonUtility.FromJson<ActivityFacebookEventPayload>(data);
- }
- catch (Exception exception)
- {
- Debug.LogWarning("[Activity][Facebook] Invalid event payload: " + exception.Message);
- }
- }
- float value = payload != null ? payload.value : 0f;
- string currency = payload != null ? payload.currency : null;
- Dictionary<string, object> parameters = BuildParameters(currency);
- switch (action.Trim().ToLowerInvariant())
- {
- case "purchased":
- FB.LogPurchase((decimal)value, string.IsNullOrWhiteSpace(currency) ? "USD" : currency, parameters);
- break;
- case "addtocart":
- FB.LogAppEvent(AppEventName.AddedToCart, value, parameters);
- break;
- case "addtowishlist":
- FB.LogAppEvent(AppEventName.AddedToWishlist, value, parameters);
- break;
- case "completeregistration":
- FB.LogAppEvent(AppEventName.CompletedRegistration, value, parameters);
- break;
- case "rated":
- FB.LogAppEvent(AppEventName.Rated, value, parameters);
- break;
- default:
- FB.LogAppEvent(action, value, parameters);
- break;
- }
- }
- static void OnInitComplete()
- {
- initializing = false;
- if (!FB.IsInitialized)
- {
- Debug.LogWarning("[Activity][Facebook] Facebook SDK initialization failed.");
- return;
- }
- #if UNITY_IOS && !UNITY_EDITOR
- LogIosTrackingStatus();
- #endif
- FB.ActivateApp();
- string initializedAppId = string.IsNullOrEmpty(FB.AppId) ? activeAppId : FB.AppId;
- Debug.Log("[Activity][Facebook] Facebook SDK initialized. App ID: " +
- (string.IsNullOrEmpty(initializedAppId) ? "unknown" : initializedAppId) + ".");
- FlushPendingEvents();
- }
- static bool HasCredentials(string appId, string clientToken)
- {
- return !string.IsNullOrWhiteSpace(appId) && !string.IsNullOrWhiteSpace(clientToken);
- }
- static void FlushPendingEvents()
- {
- if (pendingEvents.Count == 0)
- return;
- var events = pendingEvents.ToArray();
- pendingEvents.Clear();
- foreach (var pending in events)
- LogEvent(pending.action, pending.data);
- }
- #if UNITY_IOS && !UNITY_EDITOR
- static void LogIosTrackingStatus()
- {
- if (!FB.IsInitialized)
- return;
- var status = ATTrackingStatusBinding.GetAuthorizationTrackingStatus();
- Debug.Log("[Activity][Facebook] ATT authorization status: " + status);
- }
- #endif
- static Dictionary<string, object> BuildParameters(string currency)
- {
- var parameters = new Dictionary<string, object>();
- if (!string.IsNullOrWhiteSpace(currency))
- parameters[AppEventParameterName.Currency] = currency;
- return parameters;
- }
- #pragma warning disable 0649
- [Serializable]
- sealed class ActivityFacebookEventPayload
- {
- public float value;
- public string currency;
- }
- #pragma warning restore 0649
- sealed class PendingFacebookEvent
- {
- public readonly string action;
- public readonly string data;
- public PendingFacebookEvent(string action, string data)
- {
- this.action = action;
- this.data = data;
- }
- }
- }
|