Skip to content

Commit

Permalink
Fix the problem of forced termination in certain countries
Browse files Browse the repository at this point in the history
  • Loading branch information
Laeng committed May 5, 2021
1 parent 8f7bcf1 commit 8c57c52
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
59 changes: 36 additions & 23 deletions SCTools/SCTool_Redesigned/Utils/GoogleAnalytics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace SCTool_Redesigned.Utils
{
class GoogleAnalytics
{
private static bool Enable = true;

private static readonly string UA = $"SCTools Redesigned ({Environment.OSVersion.VersionString})";
private static readonly string UL = CultureInfo.CurrentCulture.Name;

Expand All @@ -35,16 +37,28 @@ class GoogleAnalytics

public static void Track(string uuid, string type, Dictionary<string, string> data, bool wait = false)
{
if (!Enable)
{
return;
}


string trackingId = Properties.Resources.Google_Analytics_TrackingID;

if (string.IsNullOrEmpty(trackingId)) return;

var task = Task.Run(() => {
var request = (HttpWebRequest) WebRequest.Create("https://www.google-analytics.com/collect");
request.Method = "POST";
if (string.IsNullOrEmpty(trackingId))
{
return;
}

var task = Task.Run(() =>
{
try
{
var request = (HttpWebRequest)WebRequest.Create("https://google-analytics.com/collect");
request.Method = "POST";

// the request body we want to send
var postData = new Dictionary<string, string>
// the request body we want to send
var postData = new Dictionary<string, string>
{
{ "v", "1" },
{ "tid", Properties.Resources.Google_Analytics_TrackingID },
Expand All @@ -55,36 +69,35 @@ public static void Track(string uuid, string type, Dictionary<string, string> da
{ "ul", UL }
};

data.ToList().ForEach(x => postData.Add(x.Key, x.Value));
data.ToList().ForEach(x => postData.Add(x.Key, x.Value));

var postDataString = postData
.Aggregate("", (data, next) => string.Format("{0}&{1}={2}", data, next.Key, HttpUtility.UrlEncode(next.Value)))
.TrimEnd('&');
var postDataString = postData
.Aggregate("", (data, next) => string.Format("{0}&{1}={2}", data, next.Key, HttpUtility.UrlEncode(next.Value)))
.TrimEnd('&');

// set the Content-Length header to the correct value
request.ContentLength = Encoding.UTF8.GetByteCount(postDataString);
// set the Content-Length header to the correct value
request.ContentLength = Encoding.UTF8.GetByteCount(postDataString);

// write the request body to the request
using (var writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(postDataString);
}
// write the request body to the request
using (var writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(postDataString);
}

try
{
var webResponse = (HttpWebResponse) request.GetResponse();

var webResponse = (HttpWebResponse)request.GetResponse();
var statusCode = webResponse.StatusCode;

webResponse.Close();

if (statusCode != HttpStatusCode.OK)
{
throw new HttpException((int) statusCode, "Google Analytics tracking did not return OK 200");
throw new HttpException((int)statusCode, "Google Analytics tracking did not return OK 200");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
App.Logger.Warn("GoogleAnalytics: " + ex.Message);
}
});

Expand Down
4 changes: 2 additions & 2 deletions SCTools/SCTool_Redesigned/Utils/RepositoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public static string GetReleaseNote(bool cache = true)
sb.Append($"<br/> \n");
}

App.Logger.Info("finish releases note parseing.");
App.Logger.Info("Finish releases note parseing.");
}

if (sb.Length <= 0)
Expand Down Expand Up @@ -313,7 +313,7 @@ public static string GetMarkdownDocument(string documentName)

markdown = content.Result;

App.Logger.Info("Finish Markdown document download");
App.Logger.Info("Finish markdown document download");
}
break;

Expand Down

0 comments on commit 8c57c52

Please sign in to comment.