|
@@ -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;
|
|
|
}
|
|
|
}
|
|
|
}
|