From b025200f8416d407bc653e45d11b89a3cb25563b Mon Sep 17 00:00:00 2001 From: Matteo Cominetti Date: Fri, 5 Apr 2024 13:38:43 +0100 Subject: [PATCH] Fix(logging): profile union and set calls to analytics (#3256) * fix(core): logging of connectors and profile identified * fix(core): remove profile-set from profile-union call in logging * fix: Incorrect formatting --------- Co-authored-by: Alan Rynne --- Core/Core/Logging/Analytics.cs | 35 ++++++++++++++++++++++++++++++---- Core/Core/Logging/Setup.cs | 1 + 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/Core/Core/Logging/Analytics.cs b/Core/Core/Logging/Analytics.cs index 319e21ce83..c96fa5b28b 100644 --- a/Core/Core/Logging/Analytics.cs +++ b/Core/Core/Logging/Analytics.cs @@ -266,10 +266,6 @@ internal static void AddConnectorToProfile(string hashedEmail, string connector) new List { connector } } } - }, - { - "set", - new Dictionary { { "Identified", true } } } }; string json = JsonConvert.SerializeObject(data); @@ -288,6 +284,37 @@ internal static void AddConnectorToProfile(string hashedEmail, string connector) }); } + internal static void IdentifyProfile(string hashedEmail, string connector) + { + Task.Run(async () => + { + try + { + var data = new Dictionary + { + { "$token", MIXPANEL_TOKEN }, + { "$distinct_id", hashedEmail }, + { + "$set", + new Dictionary { { "Identified", true } } + } + }; + string json = JsonConvert.SerializeObject(data); + + var query = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes("data=" + HttpUtility.UrlEncode(json)))); + using HttpClient client = Http.GetHttpProxyClient(); + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain")); + query.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + var res = await client.PostAsync(MIXPANEL_SERVER + "/engage#profile-set", query).ConfigureAwait(false); + res.EnsureSuccessStatusCode(); + } + catch (Exception ex) when (!ex.IsFatal()) + { + SpeckleLog.Logger.ForContext("connector", connector).Warning(ex, "Failed identify profile"); + } + }); + } + private static string GetOs() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) diff --git a/Core/Core/Logging/Setup.cs b/Core/Core/Logging/Setup.cs index f835af6199..3c40989651 100644 --- a/Core/Core/Logging/Setup.cs +++ b/Core/Core/Logging/Setup.cs @@ -78,6 +78,7 @@ public static void Init( foreach (var account in AccountManager.GetAccounts()) { Analytics.AddConnectorToProfile(account.GetHashedEmail(), hostApplication); + Analytics.IdentifyProfile(account.GetHashedEmail(), hostApplication); } }