我如何在谷歌分析中语法正确地插入数据



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

代码未插入页面视图。

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。

尝试直接在浏览器中测试完整的请求字符串。像这样的短请求也可以通过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。

BTW您在url中丢失/收集:)

最新更新