Mixpanel.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 Windows.ApplicationModel.Email.DataProvider;
  14. using Windows.Data.Json;
  15. using Windows.Media.Protection.PlayReady;
  16. namespace KdanCommon.Mixpanel
  17. {
  18. public class Mixpanel
  19. {
  20. private static string MixpanelDomain = "https://api.mixpanel.com";
  21. private HttpClient _httpClient = null;
  22. private string _projectId = 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)
  28. {
  29. _projectId = projectId;
  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 ImportEvents(EventData[] events)
  42. {
  43. var eventsJsonStr = JsonConvert.SerializeObject(events);
  44. var content = new StringContent(eventsJsonStr, System.Text.Encoding.UTF8, "application/json");
  45. var res = await _httpClient.PostAsync(_importUri, content);
  46. if(res.Content != null)
  47. {
  48. var jsonString = await res.Content.ReadAsStringAsync();
  49. var response = JsonTool.DeserializeJSON<ImportResult>(jsonString);
  50. if(response.Code != 200)
  51. {
  52. // log
  53. }
  54. }
  55. }
  56. public async Task SetProfile(ProfileData[] profiles)
  57. {
  58. var profilesJsonStr = JsonConvert.SerializeObject(profiles);
  59. var content = new StringContent(profilesJsonStr, System.Text.Encoding.UTF8, "application/json");
  60. var res = await _httpClient.PostAsync(_setProfileUri, content);
  61. if (res.Content != null)
  62. {
  63. var jsonString = await res.Content.ReadAsStringAsync();
  64. var response = JsonTool.DeserializeJSON<SetProfileResult>(jsonString);
  65. if (response.Error != null)
  66. {
  67. // log
  68. }
  69. }
  70. }
  71. public async Task CreateAlias(string distinctId, string aliasTo, string projectToken)
  72. {
  73. var alias = new AliasData()
  74. {
  75. Event = "$create_alias",
  76. Properties = new Properties()
  77. {
  78. DistinctId = distinctId,
  79. Alias = aliasTo,
  80. Token = projectToken
  81. }
  82. };
  83. var aliasJsonStr = JsonConvert.SerializeObject(alias);
  84. var formData = new FormUrlEncodedContent(new[]
  85. {
  86. new KeyValuePair<string, string>("data", aliasJsonStr),
  87. new KeyValuePair<string, string>("strict", "1"),
  88. });
  89. var res = await _httpClient.PostAsync(_createAlias, formData);
  90. if (res.Content != null)
  91. {
  92. var jsonString = await res.Content.ReadAsStringAsync();
  93. var response = JsonTool.DeserializeJSON<SetProfileResult>(jsonString);
  94. if (response.Error != null)
  95. {
  96. // log
  97. }
  98. }
  99. }
  100. }
  101. }