Код: Выделить всё
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;
using System.Threading.Tasks;
public MainWindow()
{
// Send analytics data when the application starts
SendAnalyticsDataAsync("mainWindowOpened").ConfigureAwait(false);
}
private async Task SendAnalyticsDataAsync(string eventName)
{
var httpClient = new HttpClient();
var measurementId = "MYMEASUREMENTID"; // Replace with Measurement ID
var jsonPayload = JsonConvert.SerializeObject(new
{
client_id = Guid.NewGuid().ToString(),
events = new List
{
new
{
name = eventName
}
}
});
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
// Log additional information about the request
Console.WriteLine($"Sending analytics data at {DateTime.UtcNow}");
Console.WriteLine($"Event name: {eventName}");
Console.WriteLine($"Payload: {jsonPayload}");
var response = await httpClient.PostAsync($"https://www.google-analytics.com/mp/collect?measurement_id={measurementId}", content);
// Log the status code regardless of success or failure
Console.WriteLine($"Status code: {response.StatusCode}");
if (response.IsSuccessStatusCode)
{
// Handle success
Console.WriteLine("Analytics data sent successfully.");
}
else
{
// Handle error
// Optionally, log the response body for more details
var responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Failed to send analytics data. Response body: {responseBody}");
}
}
Код: Выделить всё
Waiting for connection....
Sending analytics data at 4/17/2024 6:29:36 PM
Event name: mainWindowOpened
Payload: {"client_id":"CLEINTIDNUMBERREDACTED","events":[{"name":"mainWindowOpened"}]}
Status code: NoContent
Analytics data sent successfully.
Подробнее здесь: https://stackoverflow.com/questions/783 ... studio-app