3

非Webベースのアプリケーション(Windowsベースのアプリケーション)でGoogleアナリティクスにリクエストを送信したいですか?以下の方法で試してみました。

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」と表示されます。しかし、Google Analyticsをチェックすると、訪問数は増えません。これの理由は何ですか?何か足りないものや、アナリティクスにリクエストを送信する他の方法はありますか?

4

2 に答える 2

2

私はSOFから同様の答えを見つけました(非WebアプリケーションからのGoogle Analyticsログを引き起こし、私のシナリオに従ってそれを編集しました。

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

    }

私はについての良い記事を見つけました-

デスクトップアプリケーション用のGoogleAnalytics

于 2012-10-12T11:47:18.103 に答える
1

アナリティクスが自分のリクエストをログに記録しない原因となるフィルターを必ずオフにしてください。

// Send a hit to Google Analytics
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-654321-0" + // Google Analytics ID
    "&utmcc=__utma%3D" + cookie + "3B%2B__utmz%3D" + cookie + "%3B";

using (var client = new WebClient())
{
    client.DownloadData(statsRequest);
}
于 2012-10-12T05:57:36.383 に答える