Mixpanel.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using KdanCommon.CMSCollection.Data;
  2. using KdanCommon.GoogleCloud.Data.Vision;
  3. using KdanCommon.Helpers;
  4. using KdanCommon.Mixpanel.Data;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Net.Http;
  10. using System.Security.Cryptography;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Xml.Linq;
  14. using Windows.ApplicationModel.Email.DataProvider;
  15. using Windows.Data.Json;
  16. using Windows.Media.Protection.PlayReady;
  17. namespace KdanCommon.Mixpanel
  18. {
  19. public class Mixpanel
  20. {
  21. private static string MixpanelDomain = "https://api.mixpanel.com";
  22. private HttpClient _httpClient = null;
  23. private string _projectToken = null;
  24. private Uri _importUri = null;
  25. private Uri _setProfileUri = null;
  26. private Uri _createAlias = null;
  27. public Mixpanel(string userName, string userSecret, string projectId, string projectToken)
  28. {
  29. _projectToken = projectToken;
  30. _httpClient = new HttpClient();
  31. string authValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(userName + ":" + userSecret));
  32. _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", authValue);
  33. _importUri = new Uri($"{MixpanelDomain}/import?strict=1&project_id={projectId}");
  34. _setProfileUri = new Uri($"{MixpanelDomain}/engage?verbose=1#profile-set");
  35. _createAlias = new Uri($"{MixpanelDomain}/import?project_id={projectId}#identity-create-alias");
  36. }
  37. ~Mixpanel()
  38. {
  39. _httpClient.Dispose();
  40. }
  41. public async Task<bool> ImportEvents(string distinctId, string eventName, Dictionary<string, object> customProperties)
  42. {
  43. var events = new EventData[]
  44. {
  45. new EventData(eventName, distinctId, customProperties)
  46. };
  47. var eventsJsonStr = JsonConvert.SerializeObject(events);
  48. var content = new StringContent(eventsJsonStr, System.Text.Encoding.UTF8, "application/json");
  49. var res = await _httpClient.PostAsync(_importUri, content);
  50. if(res.StatusCode == System.Net.HttpStatusCode.OK)
  51. {
  52. if (res.Content != null)
  53. {
  54. var jsonString = await res.Content.ReadAsStringAsync();
  55. var response = JsonTool.DeserializeJSON<ImportResult>(jsonString);
  56. if (response.Code == 200)
  57. return true;
  58. }
  59. }
  60. return false;
  61. }
  62. public async Task<bool> SetProfile(string distinctId, Dictionary<string, object> customProperties)
  63. {
  64. var profiles = new ProfileData[]
  65. {
  66. new ProfileData(_projectToken, distinctId, customProperties)
  67. };
  68. var profilesJsonStr = JsonConvert.SerializeObject(profiles);
  69. var content = new StringContent(profilesJsonStr, System.Text.Encoding.UTF8, "application/json");
  70. var res = await _httpClient.PostAsync(_setProfileUri, content);
  71. if(res.StatusCode == System.Net.HttpStatusCode.OK)
  72. {
  73. if (res.Content != null)
  74. {
  75. var jsonString = await res.Content.ReadAsStringAsync();
  76. var response = JsonTool.DeserializeJSON<SetProfileResult>(jsonString);
  77. if (response.Error == null)
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. public async Task<bool> CreateAlias(string distinctId, string aliasTo)
  84. {
  85. var alias = new AliasData()
  86. {
  87. Event = "$create_alias",
  88. Properties = new Properties()
  89. {
  90. DistinctId = distinctId,
  91. Alias = aliasTo,
  92. Token = _projectToken
  93. }
  94. };
  95. var aliasJsonStr = JsonConvert.SerializeObject(alias);
  96. var formData = new FormUrlEncodedContent(new[]
  97. {
  98. new KeyValuePair<string, string>("data", aliasJsonStr),
  99. new KeyValuePair<string, string>("strict", "1"),
  100. });
  101. var res = await _httpClient.PostAsync(_createAlias, formData);
  102. if (res.StatusCode == System.Net.HttpStatusCode.OK)
  103. return true;
  104. return false;
  105. }
  106. }
  107. }