DataEyeGA.cs 15 KB

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