using System; using System.Collections; using System.Collections.Generic; using Facebook.Unity; using Firebase.Analytics; using Firebase.Extensions; using UnityEngine; public static class GameStatistics { private const string TalkingDataAppId = "5EF5C3D840224F39A08E85F38214EC0C"; private const string TalkingDataChannelId = "play.google.com"; private const string FirstLoginTimeKey = "GS_FirstLoginTime"; private const string ShowInterAdsTimesKey = "GS_ShowInterAdsTimes"; private const string TotalOnlineTimeKey = "GS_TotalOnlineTime"; private const string HasSend1Day19InterAdsEventKey = "GS_HasSend1Day19InterAdsEvent"; private const string HasSend1Day1500SecondsEventKey = "GS_HasSend1Day1500SecondsEvent"; private static long _firstLoginTime = -1; private static long FirstLoginTime { get { if (_firstLoginTime > 0) return _firstLoginTime; _firstLoginTime = long.Parse(PlayerPrefs.GetString(FirstLoginTimeKey, "0")); if (_firstLoginTime > 0) return _firstLoginTime; _firstLoginTime = DateTime.Now.TotalSeconds(); PlayerPrefs.SetString(FirstLoginTimeKey, $"{_firstLoginTime}"); return _firstLoginTime; } } private static bool IsOver1DayFromFirstLoginTime => DateTime.Now.TotalSeconds() - FirstLoginTime > 1 * 24 * 60 * 60; private static int _showInterAdsTimes = -1; private static int ShowInterAdsTimes { get { if (_showInterAdsTimes >= 0) return _showInterAdsTimes; _showInterAdsTimes = PlayerPrefs.GetInt(ShowInterAdsTimesKey, 0); return _showInterAdsTimes; } set { _showInterAdsTimes = value; PlayerPrefs.SetInt(ShowInterAdsTimesKey, _showInterAdsTimes); } } private static float _currOnlineTime; private static int _totalOnlineTime = -1; private static int TotalOnlineTime { get { if (_totalOnlineTime < 0) { _totalOnlineTime = PlayerPrefs.GetInt(TotalOnlineTimeKey, 0); } if (_currOnlineTime > 1) { _totalOnlineTime += 1; PlayerPrefs.SetInt(TotalOnlineTimeKey, _totalOnlineTime); _currOnlineTime -= 1; } return _totalOnlineTime + (int) _currOnlineTime; } } private static int _hasSend1Day19InterAdsEvent = -1; private static bool HasSend1Day19InterAdsEvent { get { if (_hasSend1Day19InterAdsEvent >= 0) return _hasSend1Day19InterAdsEvent > 0; _hasSend1Day19InterAdsEvent = PlayerPrefs.GetInt(HasSend1Day19InterAdsEventKey, 0); return _hasSend1Day19InterAdsEvent > 0; } set { _hasSend1Day19InterAdsEvent = value ? 1 : 0; PlayerPrefs.SetInt(HasSend1Day19InterAdsEventKey, _hasSend1Day19InterAdsEvent); } } private static int _hasSend1Day1500SecondsEvent = -1; private static bool HasSend1Day1500SecondsEvent { get { if (_hasSend1Day1500SecondsEvent >= 0) return _hasSend1Day1500SecondsEvent > 0; _hasSend1Day1500SecondsEvent = PlayerPrefs.GetInt(HasSend1Day1500SecondsEventKey, 0); return _hasSend1Day1500SecondsEvent > 0; } set { _hasSend1Day1500SecondsEvent = value ? 1 : 0; PlayerPrefs.SetInt(HasSend1Day1500SecondsEventKey, _hasSend1Day1500SecondsEvent); } } public static void Init() { // TalkingData TalkingDataSDK.SetVerboseLogDisable(); TalkingDataSDK.BackgroundSessionEnabled(); TalkingDataSDK.Init(TalkingDataAppId, TalkingDataChannelId, ""); // Facebook FB.Init(FBInitCallback, OnHideUnity); // Firebase Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => { var dependencyStatus = task.Result; if (dependencyStatus == Firebase.DependencyStatus.Available) { // Create and hold a reference to your FirebaseApp, // where app is a Firebase.FirebaseApp property of your application class. var app = Firebase.FirebaseApp.DefaultInstance; FirebaseAnalytics.SetAnalyticsCollectionEnabled(true); // Set a flag here to indicate whether Firebase is ready to use by your app. } else { UnityEngine.Debug.LogError(System.String.Format( "Could not resolve all Firebase dependencies: {0}", dependencyStatus)); // Firebase Unity SDK is not safe to use here. } }); } public static void OnUpdate() { _currOnlineTime += Time.deltaTime; Check1Day1500SecondsEvent(); } public static void OnDestroy() { TalkingDataSDK.OnPause(); } private static void FBInitCallback() { if (FB.IsInitialized) { // Signal an app activation App Event FB.ActivateApp(); // Continue with Facebook SDK // ... } else { Debug.Log("Failed to Initialize the Facebook SDK"); } } private static void OnHideUnity(bool isGameShown) { if (!isGameShown) { // Pause the game - we will need to hide Time.timeScale = 0; } else { // Resume the game - we're getting focus again Time.timeScale = 1; } } public static void OnLevelSelect(int level) { if (level > 200) { return; } TalkingDataSDK.OnEvent($"LevelSelect_{level}", null); } public static void OnLevelStart(int level) { if (level > 200) { return; } TalkingDataSDK.OnEvent($"LevelStart_{level}", null); } public static void OnLevelSuccess(int level, int star) { if (level > 200) { return; } TalkingDataSDK.OnEvent($"LevelSuccess_{level}", new Dictionary {{"star", $"{star}"}}); } public static void OnLevelFail(int level) { if (level > 200) { return; } TalkingDataSDK.OnEvent($"LevelFail_{level}", null); } public static void OnLevelQuit(int level) { // TalkingDataSDK.OnEvent($"LevelFail_{level}", null); } public static void OnLevelReplay(int level) { // TalkingDataSDK.OnEvent($"LevelFail_{level}", null); } public static void OnBannerAdLoaded() { TalkingDataSDK.OnEvent($"ShowAd_Banner", null); } public static void OnShowInterstitialAd() { TalkingDataSDK.OnEvent($"ShowAd_Inter", null); ShowInterAdsTimes++; Check1Day19InterAdsEvent(); } public static void OnShowRewardedAd() { TalkingDataSDK.OnEvent($"ShowAd_Reward", null); } private static void Check1Day19InterAdsEvent() { // Debug.Log($"ShowInterAdsTimes: {ShowInterAdsTimes}, OverFirstLogin: {DateTime.Now.TotalSeconds() - FirstLoginTime}"); if (IsOver1DayFromFirstLoginTime) return; if (HasSend1Day19InterAdsEvent) return; if (ShowInterAdsTimes < 19) return; FirebaseAnalytics.LogEvent("interstitial_ads_19"); HasSend1Day19InterAdsEvent = true; // Debug.Log($"ShowInterAdsTimes: {ShowInterAdsTimes}, Send Event, HasSend1Day19InterAdsEvent: {HasSend1Day19InterAdsEvent}"); } private static void Check1Day1500SecondsEvent() { // Debug.Log($"TotalOnlineTime: {TotalOnlineTime}"); if (IsOver1DayFromFirstLoginTime) return; if (HasSend1Day1500SecondsEvent) return; if (TotalOnlineTime < 1500) return; FirebaseAnalytics.LogEvent("day1_1500s"); HasSend1Day1500SecondsEvent = true; } }