123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- 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<string, object> {{"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;
- }
- }
|