DataEyeGA.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. using System.Collections.Generic;
  2. using System.Net.NetworkInformation;
  3. using System.Net.Sockets;
  4. using UnityEngine;
  5. public class DataEyeGA
  6. {
  7. public enum PlatformType
  8. {
  9. IOS = 1,
  10. ADR = 2,
  11. WP = 3
  12. }
  13. public enum AccountType
  14. {
  15. _Anonymous = 0,
  16. _Registered = 1,
  17. _SinaWeibo = 2,
  18. _QQ = 3,
  19. _QQWeibo = 4,
  20. _ND91 = 5,
  21. _GooglePlay = 6,
  22. _Facebook = 7,
  23. Type3 = 8,
  24. Type4 = 9,
  25. Type5 = 10,
  26. Type6 = 11,
  27. Type7 = 12,
  28. Type8 = 13,
  29. Type9 = 14,
  30. Type10 = 15,
  31. Official = 16,
  32. }
  33. public enum Gender
  34. {
  35. _UNKNOWN = 0,
  36. _MALE = 1,
  37. _FEMALE = 2
  38. }
  39. public enum NetType
  40. {
  41. _3G = 1,
  42. WIFI = 2,
  43. OTHER = 3,
  44. _2G = 4,
  45. _4G = 14
  46. }
  47. public enum TaskType
  48. {
  49. GuideLine = 1,
  50. MainLine = 2,
  51. BranchLine = 3,
  52. Daily = 4,
  53. Activity = 5,
  54. Other = 6,
  55. }
  56. public enum RequestType
  57. {
  58. ActOrReg,
  59. Online,
  60. Pay,
  61. VirtualCurrency,
  62. BuyItem,
  63. UseItem,
  64. GetItem,
  65. Task,
  66. LevelUp,
  67. CustomEvent,
  68. }
  69. public static bool isInited = false;
  70. public static bool isAccountSet = false;
  71. private static Dictionary<RequestType, string> requestDict;
  72. private static string urlPrefix = "http://ext.gdatacube.net/dc/rest/";
  73. //必填
  74. private static string appId;
  75. private static string accountId;
  76. private static int platform;
  77. private static string gameRegion;
  78. private static string channel;
  79. //选填
  80. private static string appVersion;
  81. private static string roleId;
  82. private static string roleName;
  83. private static string roleClass;
  84. private static string roleRace;
  85. private static int accountType;
  86. // private static string mac;
  87. private static string imei;
  88. private static int gender;
  89. private static int age;
  90. private static string resolution;
  91. private static string osVersion;
  92. private static string brand;
  93. private static string language;
  94. private static int netType;
  95. private static string ip;
  96. private static string country;
  97. private static string province;
  98. private static string operators;
  99. public static void MakeDict()
  100. {
  101. if (requestDict == null)
  102. {
  103. requestDict = new Dictionary<RequestType, string>();
  104. requestDict.Add(RequestType.ActOrReg, "actOrReg?");
  105. requestDict.Add(RequestType.Online, "online?");
  106. requestDict.Add(RequestType.Pay, "pay?");
  107. requestDict.Add(RequestType.VirtualCurrency, "coin?");
  108. requestDict.Add(RequestType.BuyItem, "itemBuy?");
  109. requestDict.Add(RequestType.UseItem, "itemUse?");
  110. requestDict.Add(RequestType.GetItem, "itemGet?");
  111. requestDict.Add(RequestType.Task, "task?");
  112. requestDict.Add(RequestType.LevelUp, "levelUp?");
  113. requestDict.Add(RequestType.CustomEvent, "event?");
  114. }
  115. }
  116. public static void Init(string appId, PlatformType platform, string gameRegion, string channel)
  117. {
  118. MakeDict();
  119. DataEyeGA.appId = appId;
  120. DataEyeGA.platform = platform.GetHashCode();
  121. DataEyeGA.gameRegion = gameRegion;
  122. DataEyeGA.channel = channel;
  123. appVersion = Application.version;
  124. string hostName = System.Net.Dns.GetHostName();
  125. ip = System.Net.Dns.GetHostEntry(hostName).AddressList.GetValue(0).ToString();
  126. imei = SystemInfo.deviceUniqueIdentifier;
  127. resolution = Screen.width + "*" + Screen.height;
  128. osVersion = SystemInfo.operatingSystem;
  129. brand = SystemInfo.deviceModel;
  130. language = Application.systemLanguage.ToString();
  131. if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
  132. netType = NetType._4G.GetHashCode();
  133. else if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork)
  134. netType = NetType.WIFI.GetHashCode();
  135. isInited = true;
  136. }
  137. public static void SetAccount(string accountId, AccountType accountType, bool sendOnline = false)
  138. {
  139. if(!CheckIsInited())
  140. return;
  141. DataEyeGA.accountId = accountId;
  142. DataEyeGA.accountType = accountType.GetHashCode();
  143. if(sendOnline)
  144. Online();
  145. isAccountSet = true;
  146. }
  147. public static void ActOrReg()
  148. {
  149. if(!CheckIsInited())
  150. return;
  151. List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
  152. paramList.Add(new KeyValuePair<string, string>("actTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
  153. paramList.Add(new KeyValuePair<string, string>("regTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
  154. SendUrl(RequestType.ActOrReg, paramList);
  155. }
  156. public static void Online(int level = 0)
  157. {
  158. if(!CheckIsInited())
  159. return;
  160. if (DateUtil.GetRealTimeSinceStart() <= 60)
  161. return;
  162. List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
  163. paramList.Add(new KeyValuePair<string, string>("loginTime", DateUtil.GetStandardTimeExistByTimeStamp().ToString()));
  164. paramList.Add(new KeyValuePair<string, string>("onlineTime", DateUtil.GetRealTimeSinceStart().ToString()));
  165. paramList.Add(new KeyValuePair<string, string>("level", level.ToString()));
  166. SendUrl(RequestType.Online, paramList);
  167. }
  168. /// <param name="currencyAmount"></param>
  169. /// <param name="currencyType"></param>
  170. /// <param name="iapid">付费点</param>
  171. /// <param name="orderId">订单ID</param>
  172. public static void Pay(int currencyAmount, string currencyType, string iapid, string orderId)
  173. {
  174. if(!CheckIsInited())
  175. return;
  176. List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
  177. paramList.Add(new KeyValuePair<string, string>("currencyAmount", currencyAmount.ToString()));
  178. paramList.Add(new KeyValuePair<string, string>("currencyType", currencyType));
  179. paramList.Add(new KeyValuePair<string, string>("iapid", iapid));
  180. paramList.Add(new KeyValuePair<string, string>("orderId", orderId));
  181. paramList.Add(new KeyValuePair<string, string>("payTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
  182. SendUrl(RequestType.Pay, paramList);
  183. }
  184. /// <param name="coinNum"></param>
  185. /// <param name="coinType">虚拟币类型</param>
  186. /// <param name="type">获得虚拟的途径</param>
  187. /// <param name="isGain">是否获得 0 消耗 1 获得</param>
  188. /// <param name="totalCoin">该玩家手里最终持有的货币数量</param>
  189. public static void VirtualCurrency(string coinNum, string coinType, string type, bool isGain, int totalCoin)
  190. {
  191. if(!CheckIsInited())
  192. return;
  193. List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
  194. paramList.Add(new KeyValuePair<string, string>("coinNum", coinNum));
  195. paramList.Add(new KeyValuePair<string, string>("coinType", coinType));
  196. paramList.Add(new KeyValuePair<string, string>("type", type));
  197. paramList.Add(new KeyValuePair<string, string>("isGain", isGain ? "1" : "0"));
  198. paramList.Add(new KeyValuePair<string, string>("totalCoin", totalCoin.ToString()));
  199. paramList.Add(new KeyValuePair<string, string>("msgTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
  200. SendUrl(RequestType.VirtualCurrency, paramList);
  201. }
  202. /// <param name="itemId"></param>
  203. /// <param name="itemType"></param>
  204. /// <param name="itemCnt">购买的道具数量</param>
  205. /// <param name="coinNum">消耗虚拟币数量</param>
  206. /// <param name="coinType">虚拟币种类</param>
  207. public static void BuyItem(string itemId, string itemType, string itemCnt, string coinNum, string coinType)
  208. {
  209. if(!CheckIsInited())
  210. return;
  211. List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
  212. paramList.Add(new KeyValuePair<string, string>("itemId", itemId));
  213. paramList.Add(new KeyValuePair<string, string>("itemType", itemType));
  214. paramList.Add(new KeyValuePair<string, string>("itemCnt", itemCnt));
  215. paramList.Add(new KeyValuePair<string, string>("coinNum", coinNum));
  216. paramList.Add(new KeyValuePair<string, string>("coinType", coinType));
  217. paramList.Add(new KeyValuePair<string, string>("msgTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
  218. SendUrl(RequestType.BuyItem, paramList);
  219. }
  220. /// <param name="itemId"></param>
  221. /// <param name="itemType"></param>
  222. /// <param name="itemCnt">购买的道具数量</param>
  223. /// <param name="reason">道具消耗的途径</param>
  224. public static void UseItem(string itemId, string itemType, string itemCnt, string reason)
  225. {
  226. if(!CheckIsInited())
  227. return;
  228. List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
  229. paramList.Add(new KeyValuePair<string, string>("itemId", itemId));
  230. paramList.Add(new KeyValuePair<string, string>("itemType", itemType));
  231. paramList.Add(new KeyValuePair<string, string>("itemCnt", itemCnt));
  232. paramList.Add(new KeyValuePair<string, string>("reason", reason));
  233. paramList.Add(new KeyValuePair<string, string>("msgTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
  234. SendUrl(RequestType.UseItem, paramList);
  235. }
  236. /// <param name="itemId"></param>
  237. /// <param name="itemType"></param>
  238. /// <param name="itemCnt">购买的道具数量</param>
  239. /// <param name="reason">道具获得的途径</param>
  240. public static void GetItem(string itemId, string itemType, string itemCnt, string reason)
  241. {
  242. if(!CheckIsInited())
  243. return;
  244. List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
  245. paramList.Add(new KeyValuePair<string, string>("itemId", itemId));
  246. paramList.Add(new KeyValuePair<string, string>("itemType", itemType));
  247. paramList.Add(new KeyValuePair<string, string>("itemCnt", itemCnt));
  248. paramList.Add(new KeyValuePair<string, string>("reason", reason));
  249. paramList.Add(new KeyValuePair<string, string>("msgTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
  250. SendUrl(RequestType.GetItem, paramList);
  251. }
  252. /// <param name="taskId">任务ID</param>
  253. /// <param name="taskType">任务类型</param>
  254. /// <param name="duration">任务耗时</param>
  255. /// <param name="isSucc">是否失败 0 任务失败 1 任务完成</param>
  256. /// <param name="reason">任务失败原因</param>
  257. public static void Task(string taskId, TaskType taskType, int duration, bool isSucc, string reason)
  258. {
  259. if(!CheckIsInited())
  260. return;
  261. List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
  262. paramList.Add(new KeyValuePair<string, string>("taskId", taskId));
  263. paramList.Add(new KeyValuePair<string, string>("taskType", taskType.GetHashCode().ToString()));
  264. paramList.Add(new KeyValuePair<string, string>("duration", duration.ToString()));
  265. paramList.Add(new KeyValuePair<string, string>("isSucc", isSucc ? "1" : "0"));
  266. paramList.Add(new KeyValuePair<string, string>("reason", reason));
  267. paramList.Add(new KeyValuePair<string, string>("msgTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
  268. SendUrl(RequestType.Task, paramList);
  269. }
  270. /// <param name="startLevel">开始等级</param>
  271. /// <param name="endLevel">结束等级</param>
  272. /// <param name="interval">升级时长(秒)</param>
  273. public static void LevelUp(int startLevel, int endLevel, int interval)
  274. {
  275. if(!CheckIsInited())
  276. return;
  277. List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
  278. paramList.Add(new KeyValuePair<string, string>("startLevel", startLevel.ToString()));
  279. paramList.Add(new KeyValuePair<string, string>("endLevel", endLevel.ToString()));
  280. paramList.Add(new KeyValuePair<string, string>("interval", interval.ToString()));
  281. paramList.Add(new KeyValuePair<string, string>("msgTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
  282. SendUrl(RequestType.LevelUp, paramList);
  283. }
  284. /// <summary>
  285. /// 触发自定义事件
  286. /// </summary>
  287. /// <param name="eventId">事件id</param>
  288. /// <param name="duration">事件耗时(秒)</param>
  289. /// <param name="keyValueList">参数列表</param>
  290. public static void CustomEvent(string eventId, int duration, List<KeyValuePair<string, string>> labelMapList)
  291. {
  292. if(!CheckIsInited())
  293. return;
  294. List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
  295. paramList.Add(new KeyValuePair<string, string>("eventId", eventId));
  296. paramList.Add(new KeyValuePair<string, string>("duration", duration.ToString()));
  297. string labelMap = "{";
  298. for(int i=0; i<labelMapList.Count; i++)
  299. labelMap += labelMapList[i].Key + ":" + labelMapList[i].Value;
  300. labelMap += "}";
  301. paramList.Add(new KeyValuePair<string, string>("labelMap", labelMap));
  302. SendUrl(RequestType.CustomEvent, paramList);
  303. }
  304. private static bool CheckIsInited(){
  305. if(isInited)
  306. return true;
  307. else{
  308. Debug.Log("<color=#ff6600>DataEyeGA is not Inited!</color>");
  309. return false;
  310. }
  311. }
  312. private static string SendUrl(RequestType requestType, List<KeyValuePair<string, string>> specialParamList = null)
  313. {
  314. string url = urlPrefix + requestDict[requestType];
  315. url += "appId=" + appId;
  316. url += "&accountId=" + accountId;
  317. url += "&platform=" + platform;
  318. url += "&gameRegion=" + gameRegion;
  319. url += "&channel=" + channel;
  320. url += "&appVersion=" + appVersion;
  321. url += "&accountType=" + accountType;
  322. // url += "&mac=" + mac;
  323. url += "&ip=" + ip;
  324. url += "&imei=" + imei;
  325. url += "&resolution=" + resolution;
  326. url += "&osVersion=" + WWW.EscapeURL(osVersion);
  327. url += "&brand=" + WWW.EscapeURL(brand);
  328. url += "&language=" + language;
  329. url += "&netType=" + netType;
  330. if(specialParamList != null)
  331. {
  332. for(int i=0; i<specialParamList.Count; i++)
  333. url += "&" + specialParamList[i].Key + "=" + WWW.EscapeURL(specialParamList[i].Value);
  334. }
  335. //Debuger.LogError("[DataEye GA]" + url);
  336. URLRequest.CreateStrURLRequest(url, null, null, URLRequest.Method.GET, true);
  337. return url;
  338. }
  339. }