SDK_UnityMoney/Assets/Script/SDKManager/Purchase/IAPEvent.cs

237 lines
8.8 KiB
C#
Raw Normal View History

2025-09-18 10:30:57 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_PURCHASE
namespace WZ
{
public static class IAPEvent
{
public static void LogPurchaseInit()
{
var eventName = "Purchase_Init";
FireBaseAnalyticsManager.Instance.LogEvent(eventName);
ShuShuEvent.Instance.Track(eventName);
}
public static void LogPurchaseInit(bool success, string message = "")
{
var eventName = success ? "Purchase_Init_Success" : "Purchase_Init_Fail";
if (success)
{
FireBaseAnalyticsManager.Instance.LogEvent(eventName);
ShuShuEvent.Instance.Track(eventName);
}
else
{
FireBaseAnalyticsManager.Instance.LogEvent(eventName, new Dictionary<string, object> { { "fail_reason", message } });
ShuShuEvent.Instance.Track(eventName, new Dictionary<string, object> { { "fail_reason", message } });
}
}
public static void LogPurchaseSuccess(PurchaseInfo info,string environment)
{
// 总支付次数
int payTimes = PlayerPrefsUtils.GetPlayerPrefsInt("PAY_TIME", 0);
if (!info.orderAlreadyExists)
{
payTimes += 1;
}
float fPrice;
if (!float.TryParse(info.price, out fPrice))
{
fPrice = 0.0f;
}
var args = new Dictionary<string, object>
{
{ "IAP", info.productName },
{ "product_id", info.productID },
{ "currency", info.currency },
{ "Price", fPrice },
{ "order_id", info.orderID },
{ "game_extra", info.gameExtra },
{ "is_first", PlayerPrefsUtils.GetPlayerPrefsInt(First_Purchase, 0) == 0},
{ "payment_type", "GooglePlay" },
{ "product_type", info.productType },
{ "orderAlreadyExists", info.orderAlreadyExists }
};
if (string.IsNullOrEmpty(environment)) environment = "";
var eventName = environment.Equals("sandbox") ? "IAP_Success_Sandbox" : "IAP_Success";
FireBaseAnalyticsManager.Instance.LogEvent(eventName, args);
ShuShuEvent.Instance.Track(eventName, args);
// 首次订单 设置支付次数/累计支付次数/累计支付金额
if (!info.orderAlreadyExists)
{
fPrice = fPrice + PlayerPrefsUtils.GetPlayerPrefsFloat(PURCHASE_PRICE, 0.0f);
PlayerPrefsUtils.SavePlayerPrefsFloat(PURCHASE_PRICE, fPrice);
ShuShuEvent.Instance.UserSet(new Dictionary<string, object>()
{
{ "total_pay_amount", fPrice },
{ "total_pay_times", payTimes }
});
ShuShuEvent.Instance.SetSuperProperties(new Dictionary<string, object>()
{
{ "pay_times", payTimes }
});
PlayerPrefsUtils.SavePlayerPrefsInt(First_Purchase, 1);
ShuShuEvent.Instance.UserSet(new Dictionary<string, object>()
{
{ "first_pay_time", DateTime.Now }
}, true);
}
PlayerPrefsUtils.SavePlayerPrefsInt(PAY_TIME, payTimes);
}
public static void LogPurchaseFail(PurchaseInfo info)
{
var eventName = "IAP_Fail";
float fPrice;
if (!float.TryParse(info.price, out fPrice))
{
fPrice = 0.0f;
}
var para = new Dictionary<string, object>
{
{ "IAP", info.productName },
{ "product_id", info.productID },
{ "currency", info.currency },
{ "pay_amount", fPrice },
{ "order_id", info.orderID },
{ "game_extra", info.gameExtra ?? ""},
{ "payment_type", "GooglePlay" },
{ "fail_reason", info.failReason }
};
FireBaseAnalyticsManager.Instance.LogEvent(eventName, para);
ShuShuEvent.Instance.Track(eventName, para);
}
public static void LogIAPButtonClick(PurchaseInfo info)
{
var eventName = "IAP_Button_Click";
float fPrice;
if (!float.TryParse(info.price, out fPrice))
{
fPrice = 0.0f;
}
var args = new Dictionary<string, object>
{
{ "IAP", info.productName},
{ "product_id", info.productID},
{ "currency", info.currency },
{ "Price", fPrice },
{ "game_extra", info.gameExtra},
{ "payment_type", "GooglePlay" },
};
FireBaseAnalyticsManager.Instance.LogEvent(eventName, args);
ShuShuEvent.Instance.Track(eventName, args);
}
public static void LogClientComplete(PurchaseInfo info)
{
var eventName = "Client_Finish_Channel";
float fPrice;
if (!float.TryParse(info.price, out fPrice))
{
fPrice = 0.0f;
}
var para = new Dictionary<string, object>
{
{ "IAP", info.productName },
{ "product_id", info.productID },
{ "currency", info.currency },
{ "Price", fPrice },
{ "order_id", info.orderID },
{ "game_extra", info.gameExtra ?? ""},
{ "payment_type", "GooglePlay" },
{ "product_type",info.productType},
{ "is_first", PlayerPrefsUtils.GetPlayerPrefsInt(First_Purchase, 0) == 0 }
};
FireBaseAnalyticsManager.Instance.LogEvent(eventName, para);
ShuShuEvent.Instance.Track(eventName, para);
}
public static void LogSaveOrderBySdk(PurchaseInfo info)
{
var eventName = "IAP_SDK_Save_Order";
float fPrice;
if (!float.TryParse(info.price, out fPrice))
{
fPrice = 0.0f;
}
var para = new Dictionary<string, object>
{
{ "IAP", info.productName },
{ "product_id", info.productID },
{ "currency", info.currency },
{ "price", fPrice },
{ "order_id", info.orderID },
{ "game_extra", info.gameExtra ?? ""},
{ "payment_type", "GooglePlay" },
{ "product_type",info.productType},
{ "is_first", PlayerPrefsUtils.GetPlayerPrefsInt(First_Purchase, 0) == 0 }
};
FireBaseAnalyticsManager.Instance.LogEvent(eventName, para);
ShuShuEvent.Instance.Track(eventName, para);
}
public static void LogStarVerifyOrderBySdk(PurchaseInfo info)
{
var eventName = "IAP_Cache_Order";
float fPrice;
if (!float.TryParse(info.price, out fPrice))
{
fPrice = 0.0f;
}
var para = new Dictionary<string, object>
{
{ "IAP", info.productName },
{ "product_id", info.productID },
{ "currency", info.currency },
{ "price", fPrice },
{ "order_id", info.orderID },
{ "game_extra", info.gameExtra},
{ "payment_type", "GooglePlay" },
{ "product_type",info.productType},
{ "is_first", PlayerPrefsUtils.GetPlayerPrefsInt(First_Purchase, 0) == 0 }
};
FireBaseAnalyticsManager.Instance.LogEvent(eventName, para);
ShuShuEvent.Instance.Track(eventName, para);
}
public static void LogRemoveOrderBySdk(PurchaseInfo info)
{
var eventName = "IAP_SDK_Remove_Order";
float fPrice;
if (!float.TryParse(info.price, out fPrice))
{
fPrice = 0.0f;
}
var para = new Dictionary<string, object>
{
{ "price", fPrice },
{ "product_id", info.productID },
{ "IAP", info.productName },
{ "order_id", info.orderID },
{ "currency", info.currency },
{ "payment_method", "googleplay" },
{ "game_extra", info.gameExtra},
{ "product_type", info.productType}
};
FireBaseAnalyticsManager.Instance.LogEvent(eventName, para);
ShuShuEvent.Instance.Track(eventName, para);
}
public static string First_Purchase = "First_purchase";
public static string PAY_TIME = "PAY_TIME";
public static string PURCHASE_PRICE = "PURCHASE_PRICE";
}
}
#endif