用于c#的Google Analytic产品代码

时间:2013-01-24 14:34:08

标签: c# google-analytics

我是google analytic的新手。我对此有所了解。我发现没有直接的方法来点击谷歌分析中的Windows应用程序。但我在stackoverflow中找到了一些解决方案。我试过了,但对我没用。下面是我正在使用的代码。

private void analyticsmethod4(string trackingId, string pagename) { Random rnd = new Random();

        long timestampFirstRun, timestampLastRun, timestampCurrentRun, numberOfRuns;

        // Get the first run time
        timestampFirstRun = DateTime.Now.Ticks;
        timestampLastRun = DateTime.Now.Ticks - 5;
        timestampCurrentRun = 45;
        numberOfRuns = 2;

        // Some values we need
        string domainHash = "123456789"; // This can be calcualted for your domain online
        int uniqueVisitorId = rnd.Next(100000000, 999999999); // Random
        string source = "Shop";
        string medium = "medium123";
        string sessionNumber = "1";
        string campaignNumber = "1";
        string culture = Thread.CurrentThread.CurrentCulture.Name;
        string screenRes = Screen.PrimaryScreen.Bounds.Width + "x" + Screen.PrimaryScreen.Bounds.Height;

        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=" + pagename + // Here i passed my profile name "MyWindowsApp"
            "&utmhid=1943799692" +
            "&utmr=0" +
            "&utmp=" + pagename +
            "&utmac=" + trackingId + //Tracking id : ie "UA-XXXXXXXX-X"
            "&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";

        try
        {
            using (var client = new WebClient())
            {
                //byte[] bt = client.DownloadData(statsRequest);
                Stream data = client.OpenRead(statsRequest);
                StreamReader reader = new StreamReader(data);
                string s = reader.ReadToEnd();
                MessageBox.Show(s);
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

long timestampFirstRun, timestampLastRun, timestampCurrentRun, numberOfRuns; // Get the first run time timestampFirstRun = DateTime.Now.Ticks; timestampLastRun = DateTime.Now.Ticks - 5; timestampCurrentRun = 45; numberOfRuns = 2; // Some values we need string domainHash = "123456789"; // This can be calcualted for your domain online int uniqueVisitorId = rnd.Next(100000000, 999999999); // Random string source = "Shop"; string medium = "medium123"; string sessionNumber = "1"; string campaignNumber = "1"; string culture = Thread.CurrentThread.CurrentCulture.Name; string screenRes = Screen.PrimaryScreen.Bounds.Width + "x" + Screen.PrimaryScreen.Bounds.Height; 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=" + pagename + // Here i passed my profile name "MyWindowsApp" "&utmhid=1943799692" + "&utmr=0" + "&utmp=" + pagename + "&utmac=" + trackingId + //Tracking id : ie "UA-XXXXXXXX-X" "&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"; try { using (var client = new WebClient()) { //byte[] bt = client.DownloadData(statsRequest); Stream data = client.OpenRead(statsRequest); StreamReader reader = new StreamReader(data); string s = reader.ReadToEnd(); MessageBox.Show(s); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }

此示例也来自此站点本身。我不知道问题出在哪里。请指导我,我该怎么做。这是输出我得到“GIF89a”。

由于 Bobbin Paulose

1 个答案:

答案 0 :(得分:0)

所以它正在发挥作用。 Google Analytics调用会加载一个微小的GIF图像,请求中提供的查询字符串参数会触发所有Google Analytics优势。如果您收到回复,则表示您已成功向Google注册了活动。