导致来自非 Web 应用程序的 Google Analytics 日志(例如,通过 WebClient) [英] Cause Google Analytics log from non-web application (eg. via WebClient)
问题描述
我想收集一些有关我的应用程序使用情况的统计信息,并且由于我已经在 Google 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).
我在网上看了一下,发现了一些人们做类似事情的例子(有些是为了解决 Javascript 被禁用,有些和我一样),但在 C# 中没有.我尽我所能翻译了代码,但几天前我已经调用了几次,日志中没有显示任何内容:(
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);
}
有谁知道怎么做才能完成这项工作?如果我能以某种方式存储 cookie 会更好,这样人们在多次运行应用程序时就会被视为回访者",但这并不重要.
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();
这篇关于导致来自非 Web 应用程序的 Google Analytics 日志(例如,通过 WebClient)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!