C# 中的谷歌分析

发布于 2024-11-03 19:21:29 字数 2059 浏览 1 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春有你 2024-11-10 19:21:30

它应该是 GET 请求,而不是 POST。不确定这是否会产生影响,但您肯定在上面的示例中错过了相当多的参数。您应该使用 Firebug 或 Live HTTP Headers 之类的工具来查看发送到 Google Analytics 的内容并进行模仿。

我也看不到查询被添加到代码中的请求中,但也许您没有在此处发布该位。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文