Google Analytics in C#
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 = "开发者_JAVA百科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 ?
It should be a GET
request instead of a POST
. 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.
精彩评论