问题描述
我想收集一些统计数据对我的应用程序的使用,因为我已经有网站统计在谷歌Analytics(分析),我认为这会是冷静,如果我可以把从应用程序,导致一击的请求分析,如:
I'd like to gather some stats about the usage of my application, and since I already have web stats in Google Analytics, I thought it'd be cool if I could send a request from the app that causes a hit in Analytics, eg.
/app/v1.0/debug
/app/v1.0/debug
这也让我看到多久我的应用程序启动时(或其他)。
This would allow me to see how often my app is starting up (or whatever).
我上网一看,发现人在做类似的事情的一些例子(有些人workaroudn JavaScript作为残疾人,和其他人做同样的我),但没有在C#。我翻译了code在作为最好的,我可以,但我已经叫了几次,几天以前,并没有表现出在日志中:(
I had a look online and found some examples of people doing similar things (some to workaroudn Javascript being disabled, and others doing the same as me), but none in C#. I translated the code over as best as I could, but I've called it a few times a couple of days ago, and nothing showed up in the logs :(
// Send a hit to Google Analytics so we can track which versions are being used
Random rnd = new Random();
int cookie = rnd.Next(10000000, 99999999);
string statsRequest = "http://www.google-analytics.com/__utm.gif" +
"?utmwv=4.3" +
"&utmn=" + rnd.Next(10000) + // Used only to stop browser caching
"&utmhn=myhost.com" + // Hostname
//"&utmhid=<random#>" +
"&utmr=-" + // Referer
"&utmp=/app/v0.4/DEBUG/Test" + // Requested page
"&utmac=UA-123456-7" + // Google Analytics ID
"&utmcc=__utma%3D" + cookie + "3B%2B__utmz%3D" + cookie + "%3B";
using (var client = new WebClient())
{
client.DownloadData(statsRequest);
}
有谁知道怎样做才能使这项工作?如果我可以存储在某种程度上饼干它甚至会更好,让人们被认为是回头客,当他们运行的应用程序多次,但这是不太重要的。
Does anyone know what to do to make this work? It would be even better if I could store the cookie in some way, so that people are considered "returning visitors" when they run the app multiple times, but that's less important.
推荐答案
我设法得到这个工作在广告中有很多摆弄的:)
I managed to get this working in the ad with a lot of fiddling :)
它也可以帮助,如果你删除,导致分析不记录自己的请求(通过IP)过滤器进行测试时;)
IT also helps if you remove the filter that causes analytics not to log your own requests (by IP) when testing ;)
Random rnd = new Random();
long timestampFirstRun, timestampLastRun, timestampCurrentRun, numberOfRuns;
// Get the first run time
timestampFirstRun = Settings.Default.FirstRun;
timestampLastRun = Settings.Default.LastRun;
timestampCurrentRun = GetEpochTime();
numberOfRuns = Settings.Default.NumberOfRuns + 1;
// If we've never run before, we need to set the same values
if (numberOfRuns == 1)
{
timestampFirstRun = timestampCurrentRun;
timestampLastRun = timestampCurrentRun;
}
// Some values we need
string domainHash = "123456789"; // This can be calcualted for your domain online
int uniqueVisitorId = rnd.Next(100000000, 999999999); // Random
string source = "source";
string medium = "medium";
string sessionNumber = "1";
string campaignNumber = "1";
string culture = Thread.CurrentThread.CurrentCulture.Name;
string screenRes = Screen.PrimaryScreen.Bounds.Width + "x" + Screen.PrimaryScreen.Bounds.Height;
#if DEBUG
string requestPath = "%2FAppStartup%2FDEBUG%2F" + SettingsWrapper.CurrentVersion.ToString(2);
string requestName = "AppStartup%20(Debug)%20v" + SettingsWrapper.CurrentVersion.ToString(2);
#else
string requestPath = "%2FAppStartup%2FRELEASE%2F" + SettingsWrapper.CurrentVersion.ToString(2);
string requestName = "AppStartup%20v" + SettingsWrapper.CurrentVersion.ToString(2);
#endif
string statsRequest = "http://www.google-analytics.com/__utm.gif" +
"?utmwv=4.6.5" +
"&utmn=" + rnd.Next(100000000, 999999999) +
"&utmhn=hostname.mydomain.com" +
"&utmcs=-" +
"&utmsr=" + screenRes +
"&utmsc=-" +
"&utmul=" + culture +
"&utmje=-" +
"&utmfl=-" +
"&utmdt=" + requestName +
"&utmhid=1943799692" +
"&utmr=0" +
"&utmp=" + requestPath +
"&utmac=UA-123656-7" + // Account number
"&utmcc=" +
"__utma%3D" + domainHash + "." + uniqueVisitorId + "." + timestampFirstRun + "." + timestampLastRun + "." + timestampCurrentRun + "." + numberOfRuns +
"%3B%2B__utmz%3D" + domainHash + "." + timestampCurrentRun + "." + sessionNumber + "." + campaignNumber + ".utmcsr%3D" + source + "%7Cutmccn%3D(" + medium + ")%7Cutmcmd%3D" + medium + "%7Cutmcct%3D%2Fd31AaOM%3B";
using (var client = new WaveWebClient())
{
client.DownloadData(statsRequest);
}
// Now save some of the values
Settings.Default.NumberOfRuns = numberOfRuns;
Settings.Default.FirstRun = timestampFirstRun;
Settings.Default.LastRun = timestampCurrentRun;
Settings.Default.Save();
这篇关于导致谷歌Analytics(分析)从非Web应用程序日志(如通过Web客户端)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!