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
This would allow me to see how often my app is starting up (or whatever).
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 :)
IT also helps if you remove the filter that causes analytics not to log your own requests (by IP) when testing ;)
A project i have released under open source allows for easy integration with Google Analytics from .net native code to fire page views, events etc through code.
It does similar things to what you're trying to achieve above except it acts as a nice c# wrapper over the top
It's called GaDotNet and can be found here: http://www.diaryofaninja.com/projects/details/ga-dot-net
You could run a sniffer like WireShark to capture the GIF request from your app and compare it with a 'genuine' Analytics ping back. Alternatively, hardcode a genuine __utm.gif request into your app with utmn randomized, make a few requests, and see whether pageviews show up.