Google Analytics: как отправить "PageView" через asmx WebService?
Это функция, которую я взял по этой ссылке: 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]
Кто-нибудь может мне помочь? (Извините за мой английский)