Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cause Google Analytics log from non-web application (eg. via WebClient)

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.

like image 283
Danny Tuppeny Avatar asked Dec 04 '09 11:12

Danny Tuppeny


2 Answers

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

like image 26
Doug Avatar answered Oct 27 '22 06:10

Doug


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 ;)

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();
like image 53
Danny Tuppeny Avatar answered Oct 27 '22 06:10

Danny Tuppeny