소스 검색

修正mixpanel

姜青儀 1 년 전
부모
커밋
84991196cd
2개의 변경된 파일36개의 추가작업 그리고 91개의 파일을 삭제
  1. 4 65
      Mixpanel/Data/ImportRequest.cs
  2. 32 26
      Mixpanel/Mixpanel.cs

+ 4 - 65
Mixpanel/Data/ImportRequest.cs

@@ -45,8 +45,7 @@ namespace KdanCommon.Mixpanel.Data
             {
                 { "time", timeStamp },
                 { "distinct_id", _distinctId },
-                { "$insert_id", GetInsertId(_distinctId, timeStamp, _customProperties) },
-                { "$city", "台南"}
+                { "$insert_id", Guid.NewGuid().ToString("N") },
             };
 
             if (_customProperties != null)
@@ -57,38 +56,6 @@ namespace KdanCommon.Mixpanel.Data
 
             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
     {
@@ -99,41 +66,13 @@ namespace KdanCommon.Mixpanel.Data
         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 Dictionary<string, object> Set { get; }
 
-        public ProfileData(string projectToken, string distinctId, string name, string mail, Dictionary<string, object> customProperties = null)
+        public ProfileData(string projectToken, string distinctId, 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;
+            Set = customProperties;
         }
     }
 

+ 32 - 26
Mixpanel/Mixpanel.cs

@@ -10,6 +10,7 @@ using System.Net.Http;
 using System.Security.Cryptography;
 using System.Text;
 using System.Threading.Tasks;
+using System.Xml.Linq;
 using Windows.ApplicationModel.Email.DataProvider;
 using Windows.Data.Json;
 using Windows.Media.Protection.PlayReady;
@@ -21,16 +22,15 @@ namespace KdanCommon.Mixpanel
         private static string MixpanelDomain = "https://api.mixpanel.com";
 
         private HttpClient _httpClient = null;
-        private string _projectId = null;
         private string _projectToken = null;
 
         private Uri _importUri = null;
         private Uri _setProfileUri = null;
         private Uri _createAlias = null;
 
-        public Mixpanel(string userName, string userSecret, string projectId)
+        public Mixpanel(string userName, string userSecret, string projectId, string projectToken)
         {
-            _projectId = projectId;
+            _projectToken = projectToken;
             _httpClient = new HttpClient();
             string authValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(userName + ":" + userSecret));
             _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", authValue);
@@ -45,39 +45,51 @@ namespace KdanCommon.Mixpanel
             _httpClient.Dispose();
         }
 
-        public async Task ImportEvents(EventData[] events)
+        public async Task<bool> ImportEvents(string distinctId, string eventName, Dictionary<string, object> customProperties)
         {
+            var events = new EventData[]
+            {
+                new EventData(eventName, distinctId, customProperties)
+            };
             var eventsJsonStr = JsonConvert.SerializeObject(events);
             var content = new StringContent(eventsJsonStr, System.Text.Encoding.UTF8, "application/json");
             var res = await _httpClient.PostAsync(_importUri, content);
-            if(res.Content != null)
+            if(res.StatusCode == System.Net.HttpStatusCode.OK)
             {
-                var jsonString = await res.Content.ReadAsStringAsync();
-                var response = JsonTool.DeserializeJSON<ImportResult>(jsonString);
-                if(response.Code != 200)
+                if (res.Content != null)
                 {
-                    // log
+                    var jsonString = await res.Content.ReadAsStringAsync();
+                    var response = JsonTool.DeserializeJSON<ImportResult>(jsonString);
+                    if (response.Code == 200)
+                        return true;
                 }
             }
+            return false;
         }
 
-        public async Task SetProfile(ProfileData[] profiles)
+        public async Task<bool> SetProfile(string distinctId, Dictionary<string, object> customProperties)
         {
+            var profiles = new ProfileData[]
+            {
+                new ProfileData(_projectToken, distinctId, customProperties)
+            };
             var profilesJsonStr = JsonConvert.SerializeObject(profiles);
             var content = new StringContent(profilesJsonStr, System.Text.Encoding.UTF8, "application/json");
             var res = await _httpClient.PostAsync(_setProfileUri, content);
-            if (res.Content != null)
+            if(res.StatusCode == System.Net.HttpStatusCode.OK)
             {
-                var jsonString = await res.Content.ReadAsStringAsync();
-                var response = JsonTool.DeserializeJSON<SetProfileResult>(jsonString);
-                if (response.Error != null)
+                if (res.Content != null)
                 {
-                    // log
+                    var jsonString = await res.Content.ReadAsStringAsync();
+                    var response = JsonTool.DeserializeJSON<SetProfileResult>(jsonString);
+                    if (response.Error == null)
+                        return true;
                 }
             }
+            return false;
         }
 
-        public async Task CreateAlias(string distinctId, string aliasTo, string projectToken)
+        public async Task<bool> CreateAlias(string distinctId, string aliasTo)
         {
             var alias = new AliasData()
             {
@@ -86,7 +98,7 @@ namespace KdanCommon.Mixpanel
                 {
                     DistinctId = distinctId,
                     Alias = aliasTo,
-                    Token = projectToken
+                    Token = _projectToken
                 }
             };
             var aliasJsonStr = JsonConvert.SerializeObject(alias);
@@ -96,15 +108,9 @@ namespace KdanCommon.Mixpanel
                 new KeyValuePair<string, string>("strict", "1"),
             });
             var res = await _httpClient.PostAsync(_createAlias, formData);
-            if (res.Content != null)
-            {
-                var jsonString = await res.Content.ReadAsStringAsync();
-                var response = JsonTool.DeserializeJSON<SetProfileResult>(jsonString);
-                if (response.Error != null)
-                {
-                    // log
-                }
-            }
+            if (res.StatusCode == System.Net.HttpStatusCode.OK)
+                return true;
+            return false;
         }
     }
 }