update: 重构用户属性打点代码, CodeReview

main
胡宇飞 2024-08-09 02:40:36 +08:00
parent a063c4866d
commit c001fe09ac
8 changed files with 186 additions and 257 deletions

View File

@ -1,3 +1,4 @@
namespace Guru
{
using System.Collections.Generic;
@ -51,11 +52,24 @@ namespace Guru
/// Debug模式下开启打点默认关闭
/// </summary>
public bool EnableDebugLogEvent = false;
private Dictionary<string, object> _defaultRemoteData = new Dictionary<string, object>();
/// <summary>
/// 云控参数的默认配置
/// </summary>
/// <returns></returns>
public Dictionary<string, object> DefaultRemoteData = new Dictionary<string, object>();
public Dictionary<string, object> DefaultRemoteData
{
set
{
if (value != null)
{
_defaultRemoteData = value;
}
}
get => _defaultRemoteData;
}
/// <summary>
/// 启用 AdjustDeeplink
/// </summary>

View File

@ -7,7 +7,7 @@ namespace Guru
/// 获取BLevel
/// </summary>
/// <returns></returns>
protected override int GetBLevel() => Model.SuccessLevelId; // BLevel
protected override int GetBLevel() => Model.BLevel; // BLevel
private GuruSDKModel Model => GuruSDKModel.Instance;

View File

@ -12,27 +12,17 @@ namespace Guru
get => _value;
set
{
if (_value.Equals(value)) return;
_value = value;
OnValueChanged?.Invoke(value);
}
}
public event Action<T> OnValueChanged;
public BindableProperty()
{
}
public BindableProperty(Action<T> onChanged)
{
OnValueChanged = onChanged;
}
public BindableProperty(T initValue, Action<T> onChanged)
public BindableProperty(T initValue)
{
_value = initValue;
OnValueChanged = onChanged;
}
}
}

View File

@ -1,18 +1,55 @@
namespace Guru
{
using System;
using UnityEngine;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Linq;
[Serializable]
class PurchasedProduct
{
public string productName;
public string productId;
public string receipt;
public bool appleProductIsRestored;
}
[Serializable]
class GuruSDKSerializedModel
{
//-------------- data ---------------
[JsonProperty(PropertyName = "uid")]
public string uid = "";
[JsonProperty(PropertyName = "b_level")]
public int bLevel = 0;
[JsonProperty(PropertyName = "b_play")]
public int bPlay = 0;
[JsonProperty(PropertyName = "no_ads")]
public bool noAds = false;
[JsonProperty(PropertyName = "purchased")]
public List<PurchasedProduct> purchased = new List<PurchasedProduct>(10);
//-------------- data ---------------
}
[Serializable]
internal class GuruSDKModel
{
private const float SaveInterval = 3;
private const string SaveKey = "com.guru.sdk.model.save";
private float _lastSavedTime = 0;
private bool _noAds = false;
private readonly BindableProperty<int> _bLevel;
private readonly BindableProperty<int> _bPlay;
private string _uid;
private List<PurchasedProduct> _purchased;
private static GuruSDKModel _instance;
@ -20,120 +57,88 @@ namespace Guru
{
get
{
if (null == _instance) _instance = Load();
if (null == _instance) _instance = new GuruSDKModel();
return _instance;
}
}
//-------------- data ---------------
public string uid = "";
public int b_level = 0;
public int b_play = 0;
public int buy_count = 0;
public bool no_ads = false;
public string first_open_time = "";
public List<PurchasedProduct> purchased;
public Dictionary<string, int> event_priorities;
//-------------- data ---------------
private float _lastSavedTime = 0;
public int SuccessLevelId
public GuruSDKModel()
{
get
{
if(_successLevel == null) InitProperties();
return _successLevel.Value;
}
GuruSDKSerializedModel model = LoadModel();
_uid = model.uid;
_noAds = model.noAds;
_bLevel = new BindableProperty<int>(model.bLevel);
_bPlay = new BindableProperty<int>(model.bPlay);
_purchased = model.purchased;
}
public int BLevel
{
get => _bLevel.Value;
set
{
if(_successLevel == null) InitProperties();
_successLevel.Value = value;
if (value < _bLevel.Value)
{
// b_level 必须比上一次的值大
Debug.LogWarning($"[SDK] :: Set b_level [{value}] should not be less than original value [{_bLevel.Value}]");
return;
}
_bLevel.Value = value;
Save();
}
}
public int TotalPlayedCount
public int BPlay
{
get
{
if(_totalPlayed == null) InitProperties();
return _totalPlayed.Value;
}
get => _bPlay.Value;
set
{
if(_totalPlayed == null) InitProperties();
_totalPlayed.Value = value;
}
}
public int PurchasedCount
{
get
{
if(_purchasedCount == null) InitProperties();
return _purchasedCount.Value;
}
set
{
if(_purchasedCount == null) InitProperties();
_purchasedCount.Value = value;
_bPlay.Value = value;
Save();
}
}
public string UserId
{
get
{
if(_uid == null) InitProperties();
return _uid.Value;
}
get => _uid;
set
{
if(_uid == null) InitProperties();
_uid.Value = value;
}
}
public string FirstOpenTime
{
get => first_open_time;
set
{
first_open_time = value;
_uid = value;
Save();
}
}
public bool IsIapUser => PurchasedCount > 0;
public bool IsIapUser => _purchased.Count > 0;
public bool IsNoAds
{
get => no_ads;
get => _noAds;
set
{
no_ads = value;
_noAds = value;
Save();
}
}
private BindableProperty<int> _successLevel;
private BindableProperty<int> _totalPlayed;
private BindableProperty<int> _purchasedCount;
private BindableProperty<string> _uid;
private BindableProperty<bool> _isIapUser;
public void SetOnBLevelChanged(Action<int> action)
{
_bLevel.OnValueChanged += action;
}
public void SetOnBPlayChanged(Action<int> action)
{
_bPlay.OnValueChanged += action;
}
public BindableProperty<int> PropBLevel => _successLevel;
public BindableProperty<int> PropBPlay => _totalPlayed;
public BindableProperty<int> PropBuyCount => _purchasedCount;
public BindableProperty<string> Uid => _uid;
#region 初始化
public static GuruSDKModel Load()
private GuruSDKSerializedModel LoadModel()
{
GuruSDKModel model = null;
GuruSDKSerializedModel model = null;
if (PlayerPrefs.HasKey(SaveKey))
{
var json = PlayerPrefs.GetString(SaveKey, "");
@ -141,38 +146,34 @@ namespace Guru
{
try
{
model = JsonUtility.FromJson<GuruSDKModel>(json);
model = JsonUtility.FromJson<GuruSDKSerializedModel>(json);
}
catch (Exception e)
{
UnityEngine.Debug.LogError(e);
Debug.LogError(e);
}
}
}
if(model == null) model = new GuruSDKModel();
model.InitProperties();
if(model == null) model = new GuruSDKSerializedModel();
return model;
}
public static string GetFirstOpenTime() => TimeUtil.GetCurrentTimeStamp().ToString();
/// <summary>
/// 保存至数据
/// </summary>
private void SaveToPlayerPrefs()
{
var json = JsonUtility.ToJson(this);
PlayerPrefs.SetString(SaveKey, json);
}
var model = new GuruSDKSerializedModel()
{
uid = _uid,
bLevel = _bLevel.Value,
bPlay = _bPlay.Value,
noAds = _noAds,
purchased = _purchased,
};
public void InitProperties()
{
if (_successLevel == null) _successLevel = new BindableProperty<int>(b_level, OnLevelChanged);
if (_totalPlayed == null) _totalPlayed = new BindableProperty<int>(b_play, OnPlayedChanged);
if (_purchasedCount == null) _purchasedCount = new BindableProperty<int>(buy_count, OnPurchasedNumChanged);
if (_uid == null) _uid = new BindableProperty<string>(uid, OnUidChanged);
if (purchased == null) purchased = new List<PurchasedProduct>(20);
if (event_priorities == null) event_priorities = new Dictionary<string, int>(10);
var json = JsonUtility.ToJson(model);
PlayerPrefs.SetString(SaveKey, json);
}
/// <summary>
@ -189,67 +190,15 @@ namespace Guru
}
}
internal void SetBLevelValue(int value) => OnLevelChanged(value);
internal void SetBPlayValue(int value) => OnPlayedChanged(value);
#endregion
#region 数据绑定变化
private void OnLevelChanged(int value)
{
b_level = value;
Save();
}
private void OnPlayedChanged(int value)
{
b_play = value;
Save();
}
private void OnPurchasedNumChanged(int value)
{
buy_count = value;
Save();
}
private void OnUidChanged(string value)
{
uid = value;
Save();
}
#endregion
#region 启动配置
/// <summary>
/// 从 Streaming 加载 AppServices 配置
/// </summary>
/// <returns></returns>
public string LoadDefaltServicesConfigJson()
{
try
{
var txt = Resources.Load<TextAsset>(GuruSDK.ServicesConfigKey);
return txt?.text ?? "";
}
catch (Exception e)
{
Log.Exception(e);
}
return "";
}
#endregion
#region 订单记录
public bool HasPurchasedProduct(string receipt)
{
if(purchased == null || purchased.Count == 0) return false;
return purchased.Exists(p => p.receipt == receipt);
if(_purchased.Count == 0) return false;
return _purchased.Exists(p => p.receipt == receipt);
}
/// <summary>
@ -258,12 +207,12 @@ namespace Guru
/// <param name="receipt"></param>
/// <param name="productName"></param>
/// <param name="productId"></param>
/// <param name="appleProductIsRestored"></param>
public void AddReceipt(string receipt, string productName, string productId, bool appleProductIsRestored = false)
{
if (purchased == null) purchased = new List<PurchasedProduct>(20);
if (!HasPurchasedProduct(receipt))
{
purchased.Add(new PurchasedProduct()
_purchased.Add(new PurchasedProduct()
{
receipt = receipt,
productName = productName,
@ -276,22 +225,16 @@ namespace Guru
public string[] GetReceipts(string productName)
{
int count = purchased?.Count ?? 0;
if (count == 0) count = 20;
if (purchased == null) purchased = new List<PurchasedProduct>(count);
var receipts = new List<string>(count);
receipts.AddRange(from purchasedProduct in purchased where purchasedProduct.productName == productName select purchasedProduct.receipt);
var receipts = new List<string>();
receipts.AddRange(from purchasedProduct in _purchased where purchasedProduct.productName == productName select purchasedProduct.receipt);
return receipts.ToArray();
}
public string[] GetReceiptsById(string productId)
{
int count = purchased?.Count ?? 0;
if (count == 0) count = 20;
if (purchased == null) purchased = new List<PurchasedProduct>(count);
var receipts = new List<string>(count);
receipts.AddRange(from purchasedProduct in purchased where purchasedProduct.productId == productId select purchasedProduct.receipt);
var receipts = new List<string>();
receipts.AddRange(from purchasedProduct in _purchased where purchasedProduct.productId == productId select purchasedProduct.receipt);
return receipts.ToArray();
}
@ -304,27 +247,12 @@ namespace Guru
public void ClearData()
{
uid = "";
b_level = 0;
b_play = 0;
buy_count = 0;
no_ads = false;
first_open_time = "";
purchased?.Clear();
event_priorities?.Clear();
PlayerPrefs.DeleteKey(SaveKey);
}
#endregion
}
[Serializable]
internal class PurchasedProduct
{
public string productName;
public string productId;
public string receipt;
public bool appleProductIsRestored;
}

View File

@ -55,22 +55,12 @@ namespace Guru
public static void SetUserBLevel(int bLevel)
{
if (!InitConfig.AutoRecordFinishedLevels)
{
Model.SetBLevelValue(bLevel);
Analytics.BLevel = bLevel;
}
Analytics.SetBLevel(bLevel);
Model.BLevel = bLevel;
}
public static void SetUserBPlay(int bPlay)
{
if (!InitConfig.AutoRecordFinishedLevels)
{
Model.SetBPlayValue(bPlay);
Analytics.BPlay = bPlay;
}
Analytics.SetBPlay(bPlay);
Model.BPlay = bPlay;
}
/// <summary>
@ -202,6 +192,9 @@ namespace Guru
$"{Tag} :: LogLevelEnd {levelId} :: Please call <GuruSDK.Start()> first, before you call <LogLevelEnd>.");
return;
}
if (extra == null) extra = new Dictionary<string, object>();
// 优先打 level_end 事件
Analytics.LogLevelEnd(levelId, result, levelName, levelType, itemId, duration, step, score, extra);
@ -212,15 +205,12 @@ namespace Guru
{
if (levelType.ToLower() == Consts.LevelTypeMain)
{
if (levelId >= Model.SuccessLevelId) Model.SuccessLevelId = levelId; // 自动记录关卡完成次数
Model.BLevel = levelId; // 自动记录 [主线] 关卡完成次数
}
Model.TotalPlayedCount++; // 自动记录关卡总次数
Analytics.LevelEndSuccess(Model.TotalPlayedCount); // 自动 level_end_success
Model.BPlay++; // 自动记录关卡总次数
Analytics.LevelEndSuccess(Model.BPlay, extra); // 自动 level_end_success
}
Analytics.BLevel = Model.SuccessLevelId; // 记录 BLevel
Analytics.BPlay = Model.TotalPlayedCount; // 记录 BPlay
}
}
@ -247,7 +237,7 @@ namespace Guru
/// /// <param name="extra">扩展参数</param>
public static void LogLevelEndSuccess(int bPlay, Dictionary<string, object> extra = null)
{
if (InitConfig.AutoRecordFinishedLevels) return;
if (extra == null) extra = new Dictionary<string, object>();
Analytics.LevelEndSuccess(bPlay, extra);
}
@ -1179,6 +1169,7 @@ namespace Guru
#region 优先级设置
private static readonly Dictionary<string, int> _eventPriorities = new Dictionary<string, int>(10);
/// <summary>
/// 设置
@ -1187,15 +1178,13 @@ namespace Guru
/// <param name="eventNames"></param>
public static void SetEventPriority(EventPriority priority, string[] eventNames)
{
if (Model.event_priorities == null) Model.event_priorities = new Dictionary<string, int>(10);
int i = 0;
while (i < eventNames.Length)
{
var evt = eventNames[i];
if (!string.IsNullOrEmpty(evt))
{
Model.event_priorities[evt] = (int)priority;
_eventPriorities[evt] = (int)priority;
}
i++;
}
@ -1208,15 +1197,14 @@ namespace Guru
public static EventPriority GetEventPriority(string eventName)
{
if (Model.event_priorities != null
&& Model.event_priorities.TryGetValue(eventName, out int p))
if (_eventPriorities.TryGetValue(eventName, out int p))
{
return (EventPriority)p;
}
return EventPriority.Default;
}
internal static int GetEventPriorityInt(string eventName)
public static int GetEventPriorityInt(string eventName)
{
return (int)GetEventPriority(eventName);
}
@ -1224,7 +1212,7 @@ namespace Guru
/// <summary>
/// set all events as 'Emergence' event, which will be triggered immediately
/// </summary>
internal void SetSDKEventPriority()
private void SetSDKEventPriority()
{
SetEventPriority(EventPriority.Emergence, new []
{

View File

@ -180,7 +180,6 @@ namespace Guru
/// <param name="success"></param>
private static void OnBuyEnd(string productName, bool success)
{
if (success) Model.PurchasedCount++;
InvokeOnPurchaseCallback?.Invoke(productName, success);
Callbacks.IAP.InvokeOnPurchaseEnd(productName, success);
}

View File

@ -83,8 +83,8 @@ namespace Guru
/// </summary>
public static int SuccessLevelCount
{
get => GuruSDKModel.Instance.SuccessLevelId;
set => GuruSDKModel.Instance.SuccessLevelId = value;
get => GuruSDKModel.Instance.BLevel;
set => GuruSDKModel.Instance.BLevel = value;
}
/// <summary>
@ -92,8 +92,8 @@ namespace Guru
/// </summary>
public static int TotalPlayedCount
{
get => GuruSDKModel.Instance.TotalPlayedCount;
set => GuruSDKModel.Instance.TotalPlayedCount = value;
get => GuruSDKModel.Instance.BPlay;
set => GuruSDKModel.Instance.BPlay = value;
}

View File

@ -1,10 +1,11 @@
using System.Linq;
namespace Guru
{
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Debug = UnityEngine.Debug;
using Guru.Network;
@ -111,8 +112,8 @@ namespace Guru
/// <param name="onComplete"></param>
private void StartWithConfig(GuruSDKInitConfig config, Action<bool> onComplete)
{
Model.PropBLevel.OnValueChanged += OnBLevelChanged;
Model.PropBPlay.OnValueChanged += OnBPlayChanged;
Model.SetOnBLevelChanged(OnBLevelChanged);
Model.SetOnBPlayChanged(OnBPlayChanged);
IsInitialSuccess = false;
_initConfig = config;
@ -137,8 +138,8 @@ namespace Guru
Analytics.Init();
// 从 Model 中注入打点属性初始值
Analytics.SetIsIapUser(Model.IsIapUser);
Analytics.SetBLevel(Model.SuccessLevelId);
Analytics.SetBPlay(Model.TotalPlayedCount);
Analytics.SetBLevel(Model.BLevel);
Analytics.SetBPlay(Model.BPlay);
//---- Start All tools ----
LogI($"#2 --- InitFirebase ---");
@ -154,17 +155,16 @@ namespace Guru
/// <summary>
/// 注入云控参数基础数据
/// </summary>
/// <param name="dict"></param>
/// <returns></returns>
private Dictionary<string, object> BuildDefaultRemoteData(Dictionary<string, object> dict)
private string LoadDefaultGuruServiceJson()
{
if (dict == null) dict = new Dictionary<string, object>(3);
// 注入默认的 Services 配置值
string json = Model.LoadDefaltServicesConfigJson();
if (!string.IsNullOrEmpty(json)) dict[ServicesConfigKey] = json;
return dict;
// 加载本地 Services 配置值
var txtAsset = Resources.Load<TextAsset>(ServicesConfigKey);
if (txtAsset != null)
{
return txtAsset.text;
}
return "";
}
/// <summary>
@ -306,7 +306,7 @@ namespace Guru
Try(() =>
{
LogI($"#4.4 --- Start Keywords ---");
KeywordsManager.Install(Model.IsIapUser, Model.SuccessLevelId); // 启动Keyword管理器
KeywordsManager.Install(Model.IsIapUser, Model.BLevel); // 启动Keyword管理器
}, ex =>
{
Debug.LogError($"--- ERROR on Keywords: {ex.Message}");
@ -342,7 +342,7 @@ namespace Guru
LogSDKInitTime((DateTime.Now.ToUniversalTime() - _initTime).TotalSeconds);
#if UNITY_ANDROID
LogI($"#5.1 --- Android StartAndroidDebugCmds---");
LogI($"#5.1 --- Android StartAndroidDebug Cmd lines---");
// Android 命令行调试
StartAndroidDebugCmds();
#endif
@ -442,28 +442,28 @@ namespace Guru
#region Logging
internal static void LogI(object message)
private static void LogI(object message)
{
Debug.Log($"{Tag} {message}");
}
internal static void LogW(object message)
private static void LogW(object message)
{
Debug.LogWarning($"{Tag} {message}");
}
internal static void LogE(object message)
private static void LogE(object message)
{
Debug.LogError($"{Tag} {message}");
}
internal static void LogException(string message)
private static void LogException(string message)
{
LogException( new Exception($"{Tag} {message}"));
}
internal static void LogException(Exception e)
private static void LogException(Exception e)
{
Debug.LogException(e);
}
@ -689,8 +689,6 @@ namespace Guru
Callbacks.SDK.InvokeDeeplinkCallback(deeplink); // 尝试调用回调
}
#endregion
#region 网络状态上报
@ -779,7 +777,19 @@ namespace Guru
LogI($"#3.5 --- Call InitRemoteConfig ---");
// 开始Remote Manager初始化
RemoteConfigManager.Init(BuildDefaultRemoteData(_initConfig.DefaultRemoteData));
var defaultGuruServiceJson = LoadDefaultGuruServiceJson();
var dict = _initConfig.DefaultRemoteData.ToDictionary(
entry => entry.Key,
entry => entry.Value);
if (!string.IsNullOrEmpty(defaultGuruServiceJson))
{
dict[ServicesConfigKey] = defaultGuruServiceJson;
}
RemoteConfigManager.Init(dict);
RemoteConfigManager.OnFetchCompleted += OnFetchRemoteCallback;
LogI($"#4 --- Apply remote services config ---");