|
@@ -0,0 +1,160 @@
|
|
|
|
+using Newtonsoft.Json;
|
|
|
|
+using System;
|
|
|
|
+using System.Collections;
|
|
|
|
+using System.Collections.Generic;
|
|
|
|
+using System.ComponentModel.Design;
|
|
|
|
+using System.Linq;
|
|
|
|
+using System.Security.Cryptography;
|
|
|
|
+using System.Text;
|
|
|
|
+using System.Threading.Tasks;
|
|
|
|
+using Windows.Data.Json;
|
|
|
|
+
|
|
|
|
+namespace KdanCommon.Mixpanel.Data
|
|
|
|
+{
|
|
|
|
+ public class EventData
|
|
|
|
+ {
|
|
|
|
+ [JsonProperty("event")]
|
|
|
|
+ public string Event { get; }
|
|
|
|
+
|
|
|
|
+ [JsonProperty("properties")]
|
|
|
|
+ public Dictionary<string, object> Properties
|
|
|
|
+ {
|
|
|
|
+ get
|
|
|
|
+ {
|
|
|
|
+ return GetDictionary();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private string _distinctId { get; }
|
|
|
|
+
|
|
|
|
+ private Dictionary<string, object> _customProperties { get; }
|
|
|
|
+
|
|
|
|
+ public EventData(string eventName, string distinctId, Dictionary<string, object> customProperties = null)
|
|
|
|
+ {
|
|
|
|
+ Event = eventName;
|
|
|
|
+ _distinctId = distinctId;
|
|
|
|
+ _customProperties = customProperties;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private Dictionary<string, object> GetDictionary()
|
|
|
|
+ {
|
|
|
|
+ DateTime gtm = new DateTime(1970, 1, 1);
|
|
|
|
+ long timeStamp = Convert.ToInt64(((TimeSpan)DateTime.UtcNow.Subtract(gtm)).TotalSeconds);
|
|
|
|
+
|
|
|
|
+ var properties = new Dictionary<string, object>
|
|
|
|
+ {
|
|
|
|
+ { "time", timeStamp },
|
|
|
|
+ { "distinct_id", _distinctId },
|
|
|
|
+ { "$insert_id", GetInsertId(_distinctId, timeStamp, _customProperties) },
|
|
|
|
+ { "$city", "台南"}
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ if (_customProperties != null)
|
|
|
|
+ {
|
|
|
|
+ foreach (var kvp in _customProperties)
|
|
|
|
+ properties.Add(kvp.Key, kvp.Value);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return properties;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private string GetInsertId(string distinctId, long timestamp, Dictionary<string, object> properties)
|
|
|
|
+ {
|
|
|
|
+ return Guid.NewGuid().ToString("N");
|
|
|
|
+ /*
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ var str = Guid.NewGuid();
|
|
|
|
+ var str2 = Guid.NewGuid().ToString("N");
|
|
|
|
+
|
|
|
|
+ string semanticallyUniqueStr = $"{distinctId}{timestamp}";
|
|
|
|
+ if (properties != null)
|
|
|
|
+ {
|
|
|
|
+ foreach (var value in properties.Values)
|
|
|
|
+ semanticallyUniqueStr += value;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ byte[] semanticallyUniqueBytes = Encoding.UTF8.GetBytes(semanticallyUniqueStr);
|
|
|
|
+ using (var sha256 = SHA256.Create())
|
|
|
|
+ {
|
|
|
|
+ byte[] hashBytes = sha256.ComputeHash(semanticallyUniqueBytes);
|
|
|
|
+ string hashStr = Convert.ToBase64String(hashBytes);
|
|
|
|
+ return hashStr.Substring(0, 36);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ catch
|
|
|
|
+ {
|
|
|
|
+ return "";
|
|
|
|
+ //return Guid.NewGuid().ToString("N").Substring(0, 36);
|
|
|
|
+ }
|
|
|
|
+ */
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ public class ProfileData
|
|
|
|
+ {
|
|
|
|
+ [JsonProperty("token")]
|
|
|
|
+ public string Token { get; }
|
|
|
|
+
|
|
|
|
+ [JsonProperty("$distinct_id")]
|
|
|
|
+ public string DistinctId { get; }
|
|
|
|
+
|
|
|
|
+ [JsonProperty("$set")]
|
|
|
|
+ public Dictionary<string, object> Set
|
|
|
|
+ {
|
|
|
|
+ get
|
|
|
|
+ {
|
|
|
|
+ return GetDictionary();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ private string _name { get; }
|
|
|
|
+ private string _mail { get; }
|
|
|
|
+
|
|
|
|
+ private Dictionary<string, object> _customProperties { get; }
|
|
|
|
+
|
|
|
|
+ public ProfileData(string projectToken, string distinctId, string name, string mail, Dictionary<string, object> customProperties = null)
|
|
|
|
+ {
|
|
|
|
+ Token = projectToken;
|
|
|
|
+ DistinctId = distinctId;
|
|
|
|
+ _name = name;
|
|
|
|
+ _mail = mail;
|
|
|
|
+ _customProperties = customProperties;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private Dictionary<string, object> GetDictionary()
|
|
|
|
+ {
|
|
|
|
+ var properties = new Dictionary<string, object>
|
|
|
|
+ {
|
|
|
|
+ { "$name", _name },
|
|
|
|
+ { "$email", _mail }
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ if (_customProperties != null)
|
|
|
|
+ {
|
|
|
|
+ foreach (var kvp in _customProperties)
|
|
|
|
+ properties.Add(kvp.Key, kvp.Value);
|
|
|
|
+ }
|
|
|
|
+ return properties;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public class AliasData
|
|
|
|
+ {
|
|
|
|
+ [JsonProperty("event")]
|
|
|
|
+ public string Event { get; set; }
|
|
|
|
+
|
|
|
|
+ [JsonProperty("properties")]
|
|
|
|
+ public Properties Properties { get; set; }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public class Properties
|
|
|
|
+ {
|
|
|
|
+ [JsonProperty("distinct_id")]
|
|
|
|
+ public string DistinctId { get; set; }
|
|
|
|
+
|
|
|
|
+ [JsonProperty("alias")]
|
|
|
|
+ public string Alias { get; set; }
|
|
|
|
+
|
|
|
|
+ [JsonProperty("token")]
|
|
|
|
+ public string Token { get; set; }
|
|
|
|
+ }
|
|
|
|
+}
|