c# - 如何在非基于网络的应用程序中向 Google Analytics 发送请求

标签 c# winforms google-analytics

我想在非基于 Web 的应用程序(基于 Windows 的应用程序)中向 Google Analytics 发送请求? 我尝试了以下方法。

public string  trackingId = "UA-XXXXXXXX-2";

private void button1_Click(object sender, EventArgs e)
{
    string shopname = "ShopTestng";
    string pagename="Testing_MyApp";
    callAnalyticsmethod2(pagename, shopname);
}

private void callAnalyticsmethod2(string pageName, string shopname)
{
    // create hash code base on pc name 7 user name    
    string visitorId = GetUniqueUserId(); 
    if (string.IsNullOrEmpty(pageName))
        pageName = visitorId;

    string utmGifLocation = "http://www.google-analytics.com/__utm.gif";

    string GifUrl = "utmwv=4.9" +
        "&utmn=" + GetRandomNumber() +
        "&utmp=" + pageName +
        "&utmac=" + trackingId +
        "&utmcc=__utma%3D999.999.999.999.999.1%3B" +
        "&utmvid=" + visitorId;// + "?" +

    string shop = shopname.Replace(" ", "_");
    string addipara = GifUrl+"&utmr=http://" + shop;

    byte[] dataStream = Encoding.UTF8.GetBytes(addipara);

    string request = utmGifLocation;

    WebRequest webRequest = WebRequest.Create(request);
    webRequest.Method = "POST";
    webRequest.ContentType = "application/x-www-form-urlencoded";
    webRequest.ContentLength = dataStream.Length;
    Stream newStream = webRequest.GetRequestStream();
    // Send the data.
    newStream.Write(dataStream, 0, dataStream.Length);
    newStream.Close();
    WebResponse webResponse = webRequest.GetResponse();
    MessageBox.Show((((HttpWebResponse)webResponse).StatusDescription));

    newStream = webResponse.GetResponseStream();
    StreamReader reader = new StreamReader(newStream);
    string responseFromServer = reader.ReadToEnd();
    MessageBox.Show(responseFromServer);

    reader.Close();
    newStream.Close();
    webResponse.Close();
}

根据上面的代码示例

MessageBox.Show((((HttpWebResponse)webResponse).StatusDescription));

行显示为“OK”。但是当我检查谷歌分析时,访问次数并没有增加。这是什么原因? 有没有我遗漏的东西或任何其他方式向分析发送请求?

最佳答案

我从 SOF ( Cause Google Analytics log from non-web application 中找到了类似的答案,并根据我的情况对其进行了编辑。

    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 +
            "&utmhid=1943799692" +
            "&utmr=0" +
            "&utmp=" + pagename +
            "&utmac=" +trackingId+ // 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 WebClient())
        {
            client.DownloadData(statsRequest);
            //Stream data = client.OpenRead(statsRequest);
            //StreamReader reader = new StreamReader(data);
            //string s = reader.ReadToEnd();
        }

    }

我找到了关于 -

的好文章

Google Analytics for Desktop Application

关于c# - 如何在非基于网络的应用程序中向 Google Analytics 发送请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12851868/

相关文章:

c# - 如何在 Dict<int,List<Tuple<string,string>>> 中查找键,以便列表包含具有给定 Item1 和 Items 的元素

c# - 如何将带有日期时间的列中的每个单元格从 ms excel 插入到 mysql 数据库?

C# Windows 应用程序访问数据库数据不会在关闭时保留

c# - 使用静态类来收集公共(public)事件处理程序是否可行?

c# - 如何大写正则表达式模式?

c# - `Where` 子句的意外行为

c# - 使用日期过滤数据 GridView

google-analytics - 带有url参数的Google Analytics(分析)目标跟踪

xcode - 钛 iPhone 模组

google-analytics - 在 Google Analytics 中,如何忽略特定子域作为推荐? _addIgnoredRef 的正确使用