Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

c# - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...