DataEyeGA.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. DataEyeGA.accountId = accountId;
  139. DataEyeGA.accountType = accountType.GetHashCode();
  140. if(sendOnline)
  141. Online();
  142. isAccountSet = true;
  143. }
  144. public static void ActOrReg()
  145. {
  146. List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
  147. paramList.Add(new KeyValuePair<string, string>("actTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
  148. paramList.Add(new KeyValuePair<string, string>("regTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
  149. SendUrl(RequestType.ActOrReg, paramList);
  150. }
  151. public static void Online(int level = 0)
  152. {
  153. if (DateUtil.GetRealTimeSinceStart() <= 60)
  154. return;
  155. List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
  156. paramList.Add(new KeyValuePair<string, string>("loginTime", DateUtil.GetStandardTimeExistByTimeStamp().ToString()));
  157. paramList.Add(new KeyValuePair<string, string>("onlineTime", DateUtil.GetRealTimeSinceStart().ToString()));
  158. paramList.Add(new KeyValuePair<string, string>("level", level.ToString()));
  159. SendUrl(RequestType.Online, paramList);
  160. }
  161. /// <param name="currencyAmount"></param>
  162. /// <param name="currencyType"></param>
  163. /// <param name="iapid">付费点</param>
  164. /// <param name="orderId">订单ID</param>
  165. public static void Pay(int currencyAmount, string currencyType, string iapid, string orderId)
  166. {
  167. List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
  168. paramList.Add(new KeyValuePair<string, string>("currencyAmount", currencyAmount.ToString()));
  169. paramList.Add(new KeyValuePair<string, string>("currencyType", currencyType));
  170. paramList.Add(new KeyValuePair<string, string>("iapid", iapid));
  171. paramList.Add(new KeyValuePair<string, string>("orderId", orderId));
  172. paramList.Add(new KeyValuePair<string, string>("payTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
  173. SendUrl(RequestType.Pay, paramList);
  174. }
  175. /// <param name="coinNum"></param>
  176. /// <param name="coinType">虚拟币类型</param>
  177. /// <param name="type">获得虚拟的途径</param>
  178. /// <param name="isGain">是否获得 0 消耗 1 获得</param>
  179. /// <param name="totalCoin">该玩家手里最终持有的货币数量</param>
  180. public static void VirtualCurrency(int coinNum, string coinType, string type, bool isGain, int totalCoin)
  181. {
  182. List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
  183. paramList.Add(new KeyValuePair<string, string>("coinNum", coinNum.ToString()));
  184. paramList.Add(new KeyValuePair<string, string>("coinType", coinType));
  185. paramList.Add(new KeyValuePair<string, string>("type", type));
  186. paramList.Add(new KeyValuePair<string, string>("isGain", isGain ? "1" : "0"));
  187. paramList.Add(new KeyValuePair<string, string>("totalCoin", totalCoin.ToString()));
  188. paramList.Add(new KeyValuePair<string, string>("msgTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
  189. SendUrl(RequestType.VirtualCurrency, paramList);
  190. }
  191. /// <param name="itemId"></param>
  192. /// <param name="itemType"></param>
  193. /// <param name="itemCnt">购买的道具数量</param>
  194. /// <param name="coinNum">消耗虚拟币数量</param>
  195. /// <param name="coinType">虚拟币种类</param>
  196. public static void BuyItem(string itemId, string itemType, string itemCnt, int coinNum, string coinType)
  197. {
  198. List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
  199. paramList.Add(new KeyValuePair<string, string>("itemId", itemId));
  200. paramList.Add(new KeyValuePair<string, string>("itemType", itemType));
  201. paramList.Add(new KeyValuePair<string, string>("itemCnt", itemCnt));
  202. paramList.Add(new KeyValuePair<string, string>("coinNum", coinNum.ToString()));
  203. paramList.Add(new KeyValuePair<string, string>("coinType", coinType));
  204. paramList.Add(new KeyValuePair<string, string>("msgTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
  205. SendUrl(RequestType.BuyItem, paramList);
  206. }
  207. /// <param name="itemId"></param>
  208. /// <param name="itemType"></param>
  209. /// <param name="itemCnt">购买的道具数量</param>
  210. /// <param name="reason">道具消耗的途径</param>
  211. public static void UseItem(string itemId, string itemType, string itemCnt, string reason)
  212. {
  213. List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
  214. paramList.Add(new KeyValuePair<string, string>("itemId", itemId));
  215. paramList.Add(new KeyValuePair<string, string>("itemType", itemType));
  216. paramList.Add(new KeyValuePair<string, string>("itemCnt", itemCnt));
  217. paramList.Add(new KeyValuePair<string, string>("reason", reason));
  218. paramList.Add(new KeyValuePair<string, string>("msgTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
  219. SendUrl(RequestType.UseItem, paramList);
  220. }
  221. /// <param name="itemId"></param>
  222. /// <param name="itemType"></param>
  223. /// <param name="itemCnt">购买的道具数量</param>
  224. /// <param name="reason">道具获得的途径</param>
  225. public static void GetItem(string itemId, string itemType, int itemCnt, string reason)
  226. {
  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.ToString()));
  231. paramList.Add(new KeyValuePair<string, string>("reason", reason));
  232. paramList.Add(new KeyValuePair<string, string>("msgTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
  233. SendUrl(RequestType.GetItem, paramList);
  234. }
  235. /// <param name="taskId">任务ID</param>
  236. /// <param name="taskType">任务类型</param>
  237. /// <param name="duration">任务耗时</param>
  238. /// <param name="isSucc">是否失败 0 任务失败 1 任务完成</param>
  239. /// <param name="reason">任务失败原因</param>
  240. public static void Task(string taskId, TaskType taskType, int duration, bool isSucc, string reason)
  241. {
  242. List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
  243. paramList.Add(new KeyValuePair<string, string>("taskId", taskId));
  244. paramList.Add(new KeyValuePair<string, string>("taskType", taskType.GetHashCode().ToString()));
  245. paramList.Add(new KeyValuePair<string, string>("duration", duration.ToString()));
  246. paramList.Add(new KeyValuePair<string, string>("isSucc", isSucc ? "1" : "0"));
  247. paramList.Add(new KeyValuePair<string, string>("reason", reason));
  248. paramList.Add(new KeyValuePair<string, string>("msgTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
  249. SendUrl(RequestType.Task, paramList);
  250. }
  251. /// <param name="startLevel">开始等级</param>
  252. /// <param name="endLevel">结束等级</param>
  253. /// <param name="interval">升级时长(秒)</param>
  254. public static void LevelUp(int startLevel, int endLevel, int interval)
  255. {
  256. List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
  257. paramList.Add(new KeyValuePair<string, string>("startLevel", startLevel.ToString()));
  258. paramList.Add(new KeyValuePair<string, string>("endLevel", endLevel.ToString()));
  259. paramList.Add(new KeyValuePair<string, string>("interval", interval.ToString()));
  260. paramList.Add(new KeyValuePair<string, string>("msgTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
  261. SendUrl(RequestType.LevelUp, paramList);
  262. }
  263. /// <summary>
  264. /// 触发自定义事件
  265. /// </summary>
  266. /// <param name="eventId">事件id</param>
  267. /// <param name="duration">事件耗时(秒)</param>
  268. /// <param name="keyValueList">参数列表</param>
  269. public static void CustomEvent(string eventId, int duration, List<KeyValuePair<string, string>> labelMapList)
  270. {
  271. List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
  272. paramList.Add(new KeyValuePair<string, string>("eventId", eventId));
  273. paramList.Add(new KeyValuePair<string, string>("duration", duration.ToString()));
  274. string labelMap = "{";
  275. for(int i=0; i<labelMapList.Count; i++)
  276. labelMap += labelMapList[i].Key + ":" + labelMapList[i].Value;
  277. labelMap += "}";
  278. paramList.Add(new KeyValuePair<string, string>("labelMap", labelMap));
  279. SendUrl(RequestType.CustomEvent, paramList);
  280. }
  281. private static string SendUrl(RequestType requestType, List<KeyValuePair<string, string>> specialParamList = null)
  282. {
  283. string url = urlPrefix + requestDict[requestType];
  284. url += "appId=" + appId;
  285. url += "&accountId=" + accountId;
  286. url += "&platform=" + platform;
  287. url += "&gameRegion=" + gameRegion;
  288. url += "&channel=" + channel;
  289. url += "&appVersion=" + appVersion;
  290. url += "&accountType=" + accountType;
  291. // url += "&mac=" + mac;
  292. url += "&ip=" + ip;
  293. url += "&imei=" + imei;
  294. url += "&resolution=" + resolution;
  295. url += "&osVersion=" + WWW.EscapeURL(osVersion);
  296. url += "&brand=" + WWW.EscapeURL(brand);
  297. url += "&language=" + language;
  298. url += "&netType=" + netType;
  299. if(specialParamList != null)
  300. {
  301. for(int i=0; i<specialParamList.Count; i++)
  302. url += "&" + specialParamList[i].Key + "=" + WWW.EscapeURL(specialParamList[i].Value);
  303. }
  304. //Debuger.LogError("[DataEye GA]" + url);
  305. URLRequest.CreateStrURLRequest(url, null, null, URLRequest.Method.GET);
  306. return url;
  307. }
  308. }