ImportRequest.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.ComponentModel.Design;
  6. using System.Linq;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using Windows.Data.Json;
  11. using Windows.Globalization;
  12. namespace KdanCommon.Mixpanel.Data
  13. {
  14. public class EventData
  15. {
  16. [JsonProperty("event")]
  17. public string Event { get; }
  18. [JsonProperty("properties")]
  19. public Dictionary<string, object> Properties
  20. {
  21. get
  22. {
  23. return GetDictionary();
  24. }
  25. }
  26. private string _distinctId { get; }
  27. private Dictionary<string, object> _customProperties { get; }
  28. public EventData(string eventName, string distinctId, Dictionary<string, object> customProperties = null)
  29. {
  30. Event = eventName;
  31. _distinctId = distinctId;
  32. _customProperties = customProperties;
  33. }
  34. private Dictionary<string, object> GetDictionary()
  35. {
  36. DateTime gtm = new DateTime(1970, 1, 1);
  37. long timeStamp = Convert.ToInt64(((TimeSpan)DateTime.UtcNow.Subtract(gtm)).TotalSeconds);
  38. var properties = new Dictionary<string, object>
  39. {
  40. { "time", timeStamp },
  41. { "distinct_id", _distinctId },
  42. { "$insert_id", Guid.NewGuid().ToString("N") },
  43. { "UwpRegion", new GeographicRegion().CodeTwoLetter}
  44. };
  45. if (_customProperties != null)
  46. {
  47. foreach (var kvp in _customProperties)
  48. properties.Add(kvp.Key, kvp.Value);
  49. }
  50. return properties;
  51. }
  52. }
  53. public class ProfileData
  54. {
  55. [JsonProperty("token")]
  56. public string Token { get; }
  57. [JsonProperty("$distinct_id")]
  58. public string DistinctId { get; }
  59. [JsonProperty("$set")]
  60. public Dictionary<string, object> Set { get; }
  61. public ProfileData(string projectToken, string distinctId, Dictionary<string, object> customProperties = null)
  62. {
  63. Token = projectToken;
  64. DistinctId = distinctId;
  65. Set = customProperties;
  66. }
  67. }
  68. public class AliasData
  69. {
  70. [JsonProperty("event")]
  71. public string Event { get; set; }
  72. [JsonProperty("properties")]
  73. public Properties Properties { get; set; }
  74. }
  75. public class Properties
  76. {
  77. [JsonProperty("distinct_id")]
  78. public string DistinctId { get; set; }
  79. [JsonProperty("alias")]
  80. public string Alias { get; set; }
  81. [JsonProperty("token")]
  82. public string Token { get; set; }
  83. }
  84. }