Google Analytics:如何通过asmx WebService发送“PageView”?

时间:2016-09-15 12:27:43

标签: c# google-analytics measurement-protocol

这是我从此链接中获取的一项功能:https://gist.github.com/0liver/11229128

namespace something
{
    /// <summary>
    /// Summary description for GA
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    public class GA : System.Web.Services.WebService
    {
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public object TrackEvent(string _tid, string _cid, string _t, string _cn, string _cs, string _cm, string _ua, string _uip, string _dl, string _ul, string _sr)
        {
            var request = (HttpWebRequest)WebRequest.Create("http://www.google-analytics.com/collect");

            request.Timeout = 600000;

            request.Method = "POST";

            // the request body we want to send
            var postData = new Dictionary<string, string>
                           {
                                { "v", "1" },
                                { "tid", _tid },
                                { "cid", _cid },
                                { "t", _t },
                                { "cn", _cn },
                                { "cs", _cs },
                                { "cm", _cm },
                                { "ua", _ua },
                                { "uip", _uip },
                                { "dl", _dl },
                                { "ul", _ul },
                                { "sr", _sr },
                           };

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

                request.KeepAlive = false;
                webResponse.Close();

                return "something";
            }
            catch (Exception ex)
            {
                return "something";
            }
        }
    }
}

尝试执行时遇到的问题:

System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 64.233.190.139:80
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
   at System.Net.HttpWebRequest.GetRequestStream()
   at Something.Services.GA.TrackEvent(String _tid, String _cid, String _t, String _cn, String _cs, String _cm, String _ua, String _uip, String _dl, String _ul, String _sr) in [path]

有人能帮帮我吗? (对不起我的英文)

0 个答案:

没有答案
相关问题