导致Google Analytics日志来自非Web应用程序(例如,通过WebClient)

我想收集一些有关我的应用程序使用情况的统计数据,因为我已经在Google Analytics中拥有网络统计数据,所以我认为如果我可以从应用程序发送导致Google Analytics受访的请求,那就太酷了,例如。

/app/v1.0/debug

这样我就可以看到我的应用程序启动的频率(或其他)。

我在网上看了一下,发现了一些人做类似事情的例子(有些是workaroudn Javascript被禁用,有些人做的和我一样),但在C#中没有。 我尽可能地翻译了代码,但几天前我已经调用了几次,日志中没有显示任何内容:(

// 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://sofzh.miximages.com/c%23/__utm.gif" + "?utmwv=4.3" + "&utmn=" + rnd.Next(10000) + // Used only to stop browser caching "&utmhn=myhost.com" + // Hostname //"&utmhid=" + "&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会更好,这样人们在多次运行应用程序时被视为“回访者”,但这并不重要。

我设法让这个在广告中工作了很多摆弄:)

如果您在测试时删除导致分析不会记录您自己的请求(通过IP)的filter,IT也会有所帮助;)

 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://sofzh.miximages.com/c%23/__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(); 

我在开源下发布的项目允许从.net本机代码轻松集成Google Analytics,以通过代码触发页面视图,事件等。

它与你想要实现的function类似,除了它在顶部充当了一个不错的c#包装器

它叫做GaDotNet,可以在这里找到: http : //www.diaryofaninja.com/projects/details/ga-dot-net

您可以像WireShark一样运行嗅探器来捕获应用程序中的GIF请求,并将其与“真正的”分析ping回来进行比较。 或者,将一个真正的__utm.gif请求硬编码到您的应用程序中,随机添加,发出一些请求,并查看是否显示了综合浏览量。