C# 中的谷歌分析
我想使用 C# 代码代替 javascript 代码进行谷歌分析
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxx-x']);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
C#
var query = HttpUtility.ParseQueryString(String.Empty);
query.Add("utmwv", "4.9");
query.Add("utmhn", "host name");
query.Add("utmcs", "UTF-8");
query.Add("utmul", "en-us");
query.Add("utmdt", "google analysis... c#");
query.Add("utmac", "UA-xxxxxx-x");
string m = "http://www.google-analytics.com/__utm.gif?";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(m);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add("GData-Version", "2");
var uri = new UriBuilder("http://www.google-analytics.com/__utm.gif?");
uri.Query = query.ToString();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri.ToString());
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add("GData-Version", "2");
byte[] data = Encoding.ASCII.GetBytes(query.ToString());
Stream input = request.GetRequestStream();
input.Write(data, 0, data.Length);
input.Close();
HttpWebResponse nsResponse = (HttpWebResponse)request.GetResponse();
Stream streamResponse = nsResponse.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
与上面的代码我正在发出网络请求但无济于事。我错过了什么,或者有更好的方法吗?
i want to use c# code in place of javascript code for google analytics
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxx-x']);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
C#
var query = HttpUtility.ParseQueryString(String.Empty);
query.Add("utmwv", "4.9");
query.Add("utmhn", "host name");
query.Add("utmcs", "UTF-8");
query.Add("utmul", "en-us");
query.Add("utmdt", "google analysis... c#");
query.Add("utmac", "UA-xxxxxx-x");
string m = "http://www.google-analytics.com/__utm.gif?";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(m);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add("GData-Version", "2");
var uri = new UriBuilder("http://www.google-analytics.com/__utm.gif?");
uri.Query = query.ToString();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri.ToString());
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add("GData-Version", "2");
byte[] data = Encoding.ASCII.GetBytes(query.ToString());
Stream input = request.GetRequestStream();
input.Write(data, 0, data.Length);
input.Close();
HttpWebResponse nsResponse = (HttpWebResponse)request.GetResponse();
Stream streamResponse = nsResponse.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
with above code i am making a web request but no avail. am i missing something, or any better approach for it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它应该是
GET
请求,而不是POST
。不确定这是否会产生影响,但您肯定在上面的示例中错过了相当多的参数。您应该使用 Firebug 或 Live HTTP Headers 之类的工具来查看发送到 Google Analytics 的内容并进行模仿。我也看不到查询被添加到代码中的请求中,但也许您没有在此处发布该位。
It should be a
GET
request instead of aPOST
. Not sure if that makes a difference but you've certainly missed out quite a few parameters in the example above. You should use something like Firebug or Live HTTP Headers to see what's being sent to Google Analytics and mimic that.I also can't see the query being added to the request in your code but maybe you didn't post that bit here.