我如何以编程方式在 google 分析中插入数据

how can I pragrammatically insert data in google analytics

能否请您告诉我以下以编程方式在 google 分析中插入综合浏览量的代码有什么问题。

代码未插入综合浏览量。

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。

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