Saikat Chakraborty Saikat Chakraborty - 1 month ago 13
C# Question

Is it possible to post windows form Hit info to Google analytics(track id) using Gdata API C#

May be this is a very dumb question to many.

I have created a Google analytics a/c with a valid track id. Now I have put the javascript code provided by Google analytics to my *.aspx files and could see my Website hit information in Google analytics site.

Now can I do same from a Windows form application?

I tried simple Http post to GA site but failed. I referred following link: https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#overview

Then I came across GData API. https://code.google.com/archive/p/google-gdata/

I have installed GData but not sure how to send Windows form application hit information to Google analytics.

Answer

Now after a bit of online search, I came across of the below link http://tilr.blogspot.com/2012/10/google-analytics-use-google-analytics.html

Tested. Works correctly. You need to provide valid GA track Id and winform name.

I have not yet changed other GA settings (unique visitorid, location etc.). May be I could help when I fully understand the methods and parameters.

Check this link: https://developers.google.com/analytics/resources/concepts/gaConceptsTrackingOverview#gifRequestClassification

or

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

or

http://www.analyticsmarket.com/blog/__utmgif-data for detailed information on the parameters.

Final working code from C# winform application:

///<summary>Send winform hit info to Google Analytic</summary>
///<param name="visitorID">Unique visitor id to be set once per session</param>
///<param name="timeStamp">timestamp to be set once per session</param>
///<param name="formName">Name of the winform</param> 

public static void PostFormDataToGA(int visitorID, long timeStamp, string formName)
    {
        const string GA_TRACKING_ID = "UA-XXXXXXX-Y";

        Random randomNum = new Random();
        long timestampFirstRun, timestampLastRun, timestampCurrentRun, numberOfRuns;

        //timestamp to be set once per session. Else every subsequent form request will be counted as new user. 
        timestampFirstRun = timeStamp;
        timestampLastRun = timeStamp - 5;
        timestampCurrentRun = timeStamp;//45;

        numberOfRuns = 2;

        var domainName = Environment.UserDomainName;
        string domainHash = CreateDomainHash(domainName);
        string source = "ApplicationName"; //https://support.google.com/analytics/answer/1033173?hl=en
        string medium = "none"; //https://support.google.com/analytics/answer/1033173?hl=en
        string sessionNumber = "1";
        string campaignNumber = "1";
        string culture = Thread.CurrentThread.CurrentCulture.Name;

        string statsRequest = "http://www.google-analytics.com/__utm.gif" +
               "?utmwv=4.4" + //Tracking code version e.g. utmwv=1
               "&utmn="+ visitorID +  //+ randomNum.Next(100000000, 999999999) +  //Unique ID generated for each GIF request to prevent caching of the GIF image. e.g. utmn=1142651215
               "&utmcs=-" + //Language encoding for the browser. Some browsers don't set this, in which case it is set to "-"   utmcs=ISO-8859-1
               "&utmsc=-" + //Screen color depth    utmsc=24-bit
               "&utmul=" + culture + //Browser language.    utmul=pt-br
               "&utmje=-" + //Indicates if browser is Java-enabled. 1 is true.  utmje=1
               "&utmfl=-" + //Flash Version utmfl=9.0%20r48
               "&utmdt=" + formName + //Page title, which is a URL-encoded string.  utmdt=analytics%20page%20test
               "&utmhid=" + visitorID + //+ randomNum.Next(100000000, 999999999) + //1943799692" + //A random number used to link Analytics GIF requests with AdSense.  utmhid = 2059107202
               "&utmr=0" + //Referral, complete URL.    utmr=http://www.example.com/aboutUs/index.php?var=selected
               "&utmp=" + formName + //Page request of the current page.    utmp=/testDirectory/myPage.html
               "&utmac=" + GA_TRACKING_ID + //Account String.Appears on all requests.   utmac = UA - 2202604 - 2
               "&utmcc=" + //Cookie values. This request parameter sends all the cookies requested from the page.
                           //utmcc = __utma % 3D117243.1695285.22 % 3B % 2B __utmz% 3D117945243.1202416366.21.10.utmcsr % 3Db % 7C utmccn% 3D(referral) % 
                           //7C utmcmd% 3Dreferral % 7C utmcct% 3D % 252Fissue % 3B % 2B
               "__utma%3D" + domainHash + "." + visitorID + "." +
                   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";
        try
        {
            using (var client = new WebClient())
            {
                client.DownloadData(statsRequest);
            }
        }
        catch (Exception ex)
        {
            //Handle exception
        }
    }

static string CreateDomainHash(string d)
{
        int a = 1;
        int c = 0;
        int h;
        int o;
        if (!String.IsNullOrEmpty(d))
        {
            a = 0;
            for (h = d.Length - 1; h >= 0; h--)
            {
                o = d[h];
                a = (a << 6 & 268435455) + o + (o << 14);
                c = a & 266338304;
                a = c != 0 ? a ^ c >> 21 : a;
            }
        }
        return a.ToString();
    }

Then I found some typical issue: My application is only for windows OS but in GA it shows the application is used from Linux and Mac. How is this possible. Answer to this is : sometime GA is hit by Spams hence showing irrelevant data

https://www.en.advertisercommunity.com/t5/Referral-Spam-Traffic/Google-Analytics-showing-wrong-country-and-Operating-system/td-p/507606#

http://help.analyticsedge.com/spam-filter/definitive-guide-to-removing-google-analytics-spam/

Some more links to understand the GA parameters better way:

http://www.analytics-ninja.com/blog/2011/08/how-google-analytics-calculates-visits.html http://www.lunametrics.com/blog/2011/10/04/utmgif-request-parameters/ http://blog.diniscruz.com/2013/01/creating-server-side-google-analytics.html

Comments