1

你能告诉我下面的代码在谷歌分析中以编程方式插入网页浏览有什么问题吗?

代码未插入综合浏览量。

var request = (HttpWebRequest)WebRequest.Create("http://www.google-analytics.com");
            request.Method = "POST";

            // the request body we want to send
            var postData = new Dictionary<string, string>
{
{ "v", "1" },
{ "tid", "UA-XXXXXX-1" },
{ "cid", "555" },
{ "t", "pageview" },
{"dh","www.pomroofing.com"},
{ "dp", "/phone/123/456/789/1" },
{ "dt", "homepage" },
};


            var postDataString = postData
            .Aggregate("", (data, next) => string.Format("{0}&{1}={2}", data, next.Key,
            HttpUtility.UrlEncode(next.Value)))
            .TrimEnd('&');

            // set the Content-Length header to the correct value
            request.ContentLength = Encoding.UTF8.GetByteCount(postDataString);

            // write the request body to the request
            using (var writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(postDataString);
            }

            try
            {
                var webResponse = (HttpWebResponse)request.GetResponse();
                if (webResponse.StatusCode != HttpStatusCode.OK)
                {
                    throw new HttpException((int)webResponse.StatusCode,
                    "Google Analytics tracking did not return OK 200");
                }
            }
            catch (Exception ex)
            {
                // do what you like here, we log to Elmah
                // ElmahLog.LogError(ex, "Google Analytics tracking failed");
            }

请帮忙,或者是否有任何API。

4

1 回答 1

2

尝试直接在浏览器中测试您的完整请求字符串。像这样的简短请求也可以使用 GET 发送。

检查实时报告以查看其是否显示。(我测试了这个)

http://www.google-analytics.com/collect?v=1&tid=UA-XXXX-X&cid=555&t=pageview&dh=www.pomroofing.com&dp=/phone/123/456/789/1&dt=homepage

不,没有用于此的 API。

顺便说一句,您在网址中缺少 /collect :)

于 2015-02-27T07:31:04.040 回答