C# 是否可以使用GDataAPI C将windows窗体命中信息发布到Google analytics(跟踪id)#

C# 是否可以使用GDataAPI C将windows窗体命中信息发布到Google analytics(跟踪id)#,c#,google-analytics,C#,Google Analytics,对许多人来说,这可能是一个非常愚蠢的问题 我已经创建了一个具有有效跟踪id的Google analytics a/c。现在,我已经将Google analytics提供的javascript代码放入我的*.aspx文件中,并且可以在Google analytics站点中查看我的网站点击信息 现在我可以在Windows窗体应用程序中执行同样的操作了吗 我尝试了简单的Http post到GA站点,但失败了。我提到了以下链接: 然后我遇到了gdataapi 我已经安装了GData,但不知道如何将Win

对许多人来说,这可能是一个非常愚蠢的问题

我已经创建了一个具有有效跟踪id的Google analytics a/c。现在,我已经将Google analytics提供的javascript代码放入我的*.aspx文件中,并且可以在Google analytics站点中查看我的网站点击信息

现在我可以在Windows窗体应用程序中执行同样的操作了吗

我尝试了简单的Http post到GA站点,但失败了。我提到了以下链接:

然后我遇到了gdataapi

我已经安装了GData,但不知道如何将Windows窗体应用程序命中信息发送到Google analytics

这不是一个愚蠢的问题。是的,您可以使用向Google analytics发送数据。您可以使用支持HTTP POST或HTTP GET的任何语言来实现这一点

Gdata库用于从Google analytics请求数据,实际上不应该使用,因为它很旧,您应该使用较新的API v3或v4从Google analytics请求数据


有一些第三方SDK用于.net的测量协议,我个人不喜欢它们中的任何一个,并且制作了我自己的,我还没有发布。我计划很快做这件事,它将是开源的。

现在经过一点在线搜索,我看到了下面的链接

测试。工作正常。您需要提供有效的GA轨迹Id和winform名称

我还没有更改其他GA设置(唯一访问者、位置等)。如果我完全理解方法和参数,我可能会有所帮助

检查此链接:

有关参数的详细信息,请参阅

C#winform应用程序的最终工作代码:

///<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)
    {
        //#if !DEBUG
        //visitor ID I am creating at the application start up for first time and saving it in registry. Read it from registry for subsequent uses.

        const string GA_TRACKING_ID = "UA-XXXXXXX-Y";

        Random randomNum = new Random();
        long timestampForFirstRun, timestampForLastRun, timestampForCurrentRun, numberOfRuns;

        timestampForCurrentRun = DateTimeOffset.Now.ToUnixTimeSeconds();//DateTime.Now.Ticks; //current timestamp

        string[,] temp = new string[1, 1];
        var rk = new ModifyRegistry(); // reading windows registry
        rk.SubKey = Utility.DS_REG_COMMON; //some path in registry

        //Unique timestamp to be set once per session. Else every subsequent form request will be counted as new user.
        temp[0, 0] = rk.Read(Constants.GA_NOOFRUNS); //Reading number of runs
        if (string.IsNullOrEmpty(temp[0, 0])) //On the first run of application after installation, registry values will be empty
        {
            numberOfRuns = 1; //if first run then make it 1
            timestampForFirstRun = timestampForCurrentRun; 
            timestampForLastRun = timestampForCurrentRun;
        }
        else
        {
            //On the subsequent application run, read values from registry
            numberOfRuns = Convert.ToInt64(temp[0, 0]) + 1;

            temp[0, 0] = rk.Read(Constants.GA_TIMESTAMPFORFIRSTRUN); 
            timestampForFirstRun = Convert.ToInt64(temp[0, 0]);

            temp[0, 0] = rk.Read(Constants.GA_TIMESTAMPFORLASTRUN);
            timestampForLastRun = Convert.ToInt64(temp[0, 0]);
        }

        var domainName = Environment.UserDomainName;
        //string domainHash = GenerateDomainHash(domainName);
        string domainHash = "123456789";
        string utmhid = "1943799692";
        string source = "DriveSize";
        string medium = "medium "; 
        //string sessionNumber = "1";
        //sessionNumber = 1; // numberOfRuns is used for this
        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
                //+ visitorID +  //
               "&utmn="+ randomNum.Next(100000000, 999999999) +  //Unique ID generated for each GIF request to prevent caching of the GIF image. e.g. utmn=1142651215
               "&utmhn=hostname.mydomain.com" +
               "&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=" + utmhid + //+ 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=" + "//" + //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 + "." +
                   timestampForFirstRun + "." + timestampForLastRun + "." +
                   timestampForCurrentRun + "." + numberOfRuns +
               "%3B%2B__utmz%3D" + domainHash + "." + timestampForCurrentRun + "."
                   + numberOfRuns + "." + campaignNumber + ".utmcsr%3D" +
                   source + "%7Cutmccn%3D(" + medium + ")%7Cutmcmd%3D" + medium +
              "%7Cutmcct%3D%2Fd31AaOM%3B";
        try
        {
            using (var client = new WebClient())
            {
                client.DownloadData(statsRequest); //post
            }
        }
        catch (Exception ex)
        {
            //Handle exception
        }

        //Saving number of runs and timestamp for next use.
        rk.Write(Constants.GA_NOOFRUNS, numberOfRuns.ToString());
        rk.Write(Constants.GA_TIMESTAMPFORFIRSTRUN, timestampForFirstRun.ToString());
        rk.Write(Constants.GA_TIMESTAMPFORLASTRUN, timestampForCurrentRun.ToString());
//#endif
}
///将winform命中信息发送到Google Analytical
///每个会话设置一次的唯一访客id
///每个会话设置一次时间戳
///winform的名称
公共静态void PostFormDataToGA(int visitorID、长时间戳、字符串formName)
{
//#如果!调试
//我第一次在应用程序启动时创建的访问者ID,并将其保存在注册表中。请从注册表中读取该ID以供后续使用。
const string GA_TRACKING_ID=“UA-XXXXXXX-Y”;
随机数=新随机数();
long timestampForFirstRun、timestampForLastRun、timestampForCurrentRun、numberOfRuns;
timestampForCurrentRun=DateTimeOffset.Now.ToUnixTimeSeconds();//DateTime.Now.Ticks;//当前时间戳
字符串[,]temp=新字符串[1,1];
var rk=new ModifyRegistry();//正在读取windows注册表
rk.SubKey=Utility.DS\u REG\u COMMON;//注册表中的某个路径
//每个会话设置一次的唯一时间戳。否则,每个后续表单请求都将被视为新用户。
temp[0,0]=rk.Read(Constants.GA_NOOFRUNS);//读取运行次数
安装后第一次运行应用程序时,如果(string.IsNullOrEmpty(temp[0,0])/,注册表值将为空
{
numberOfRuns=1;//如果第一次运行,则将其设为1
timestampForFirstRun=timestampForCurrentRun;
timestampForLastRun=timestampForCurrentRun;
}
其他的
{
//在后续应用程序运行时,从注册表读取值
运行次数=转换为64(温度[0,0])+1;
temp[0,0]=rk.Read(常数.GA_TIMESTAMPFORFIRSTRUN);
timestampForFirstRun=Convert.ToInt64(temp[0,0]);
temp[0,0]=rk.Read(Constants.GA_TIMESTAMPFORLASTRUN);
timestampForLastRun=Convert.ToInt64(temp[0,0]);
}
var domainName=Environment.UserDomainName;
//字符串domainHash=GenerateDomainHash(域名);
字符串domainHash=“123456789”;
字符串utmhid=“1943799692”;
string source=“DriveSize”;
字符串medium=“medium”;
//字符串sessionNumber=“1”;
//sessionNumber=1;//numberOfRuns用于此
字符串编号=“1”;
字符串区域性=Thread.CurrentThread.CurrentCulture.Name;
字符串statsRequest=”http://www.google-analytics.com/__utm.gif" +
“?utmwv=4.4”+//跟踪代码版本,例如utmwv=1
//+访问ID+//
“&utmn=“+randomNum.Next(100000000,999999999)+//为每个GIF请求生成的唯一ID,以防止缓存GIF图像。例如,utmn=1142651215
“&utmhn=hostname.mydomain.com”+
“&utmcs=-”+//浏览器的语言编码。某些浏览器不设置此项,在这种情况下,它被设置为“-”utmcs=ISO-8859-1
“&utmsc=-”+//屏幕颜色深度utmsc=24位
“&utmul=“+culture+//浏览器语言。utmul=pt br”
“&utmje=-”+//指示浏览器是否启用了Java。1为true。utmje=1
“&utmfl=-”+//Flash版本utmfl=9.0%20r48
“&utmdt=“+formName+//页面标题,是URL编码的字符串。utmdt=analytics%20page%20test-----
“&utmhid=“+utmhid++/+randomNum.Next(100000000,99999999)+///1943799692”+//用于将分析GIF请求与AdSense链接的随机数。utmhid=2059107202
“&utmr=0”+//引用,完整URL.utmr=http://www.example.com/aboutUs/index.php?var=selected
“&utmp=“+”//“+///当前页面的页面请求。utmp=/testDirectory/myPage.html”
“&utmac=“+GA\u TRACKING\u ID+//帐户字符串。出现在所有请求上。utmac=UA-2202604-2
“&utmcc=“+//Cookie值。此请求参数发送从页面请求的所有Cookie。
//utmcc=\uuuuuUTMA%3D117243.1695285.22%3B%2B\uuuUTMz%3D117945243.120241636.21.10.utmcsr%3Db%7C uTMCN%3D(参考文献)%
//7C utmcmd%3Dreferral%7C utmcct%3D%252财政支出%3B%2B
“\uuUtma%3D”+域哈希+”+访问者ID+”+
timestampForFirstRun+““+”timestampForFirstRun+