123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- using KdanCommon.CMSCollection.Data;
- using KdanCommon.GoogleCloud.Data.Vision;
- using KdanCommon.Helpers;
- using KdanCommon.Mixpanel.Data;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- 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;
- namespace KdanCommon.Mixpanel
- {
- public class Mixpanel
- {
- private static string MixpanelDomain = "https://api.mixpanel.com";
- private HttpClient _httpClient = 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, string projectToken)
- {
- _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);
- _importUri = new Uri($"{MixpanelDomain}/import?strict=1&project_id={projectId}");
- _setProfileUri = new Uri($"{MixpanelDomain}/engage?verbose=1#profile-set");
- _createAlias = new Uri($"{MixpanelDomain}/import?project_id={projectId}#identity-create-alias");
- }
- ~Mixpanel()
- {
- _httpClient.Dispose();
- }
- 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.StatusCode == System.Net.HttpStatusCode.OK)
- {
- if (res.Content != null)
- {
- var jsonString = await res.Content.ReadAsStringAsync();
- var response = JsonTool.DeserializeJSON<ImportResult>(jsonString);
- if (response.Code == 200)
- return true;
- }
- }
- return false;
- }
- 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.StatusCode == System.Net.HttpStatusCode.OK)
- {
- if (res.Content != null)
- {
- var jsonString = await res.Content.ReadAsStringAsync();
- var response = JsonTool.DeserializeJSON<SetProfileResult>(jsonString);
- if (response.Error == null)
- return true;
- }
- }
- return false;
- }
- public async Task<bool> CreateAlias(string distinctId, string aliasTo)
- {
- var alias = new AliasData()
- {
- Event = "$create_alias",
- Properties = new Properties()
- {
- DistinctId = distinctId,
- Alias = aliasTo,
- Token = _projectToken
- }
- };
- var aliasJsonStr = JsonConvert.SerializeObject(alias);
- var formData = new FormUrlEncodedContent(new[]
- {
- new KeyValuePair<string, string>("data", aliasJsonStr),
- new KeyValuePair<string, string>("strict", "1"),
- });
- var res = await _httpClient.PostAsync(_createAlias, formData);
- if (res.StatusCode == System.Net.HttpStatusCode.OK)
- return true;
- return false;
- }
- }
- }
|