【问题标题】:Tracking ASP.NET Web API with google analytics使用谷歌分析跟踪 ASP.NET Web API
【发布时间】:2014-01-28 05:51:58
【问题描述】:

我正在为我的网站开发一个 API,用户可以访问和使用自己的网站/应用程序。

我想保留我允许的不同 API 调用的使用情况统计信息,并想像我在我的网站上一样使用 Google Analytics。是否可以在服务器端代码上使用 Google Analytics 进行跟踪?或者特别是 web api?

谢谢

【问题讨论】:

    标签: asp.net asp.net-web-api google-analytics


    【解决方案1】:

    Google 分析已从服务器端向mobile code to track 提供统计信息。您可以使用该代码来归档您的目标。

    这实际上是用你给他的信息从服务器端向谷歌发出网络请求。调用 gif 图像,url 参数为:

    string utmGifLocation = "http://www.google-analytics.com/__utm.gif";
    
    string utmUrl = utmGifLocation + "?" +
        "utmwv="   + Version +
        "&utmn="   + RandomNumber +
        "&utmhn="  + HttpUtility.UrlEncode(domainName) +
        "&utmr="   + HttpUtility.UrlEncode(documentReferer) +
        "&utmp="   + HttpUtility.UrlEncode(documentPath) +
        "&utmac="  + account +
        "&utmcc=__utma%3D999.999.999.999.999.1%3B" +
        "&utmvid=" + visitorId +
        "&utmip="  + GlobalContext.Request.ServerVariables["REMOTE_ADDR"];
    
    SendRequestToGoogleAnalytics(utmUrl);
    

    请求是这样的:

    private void SendRequestToGoogleAnalytics(string utmUrl)
    {
        try
        {
            WebRequest connection = WebRequest.Create(utmUrl);
    
            ((HttpWebRequest)connection).UserAgent = GlobalContext.Request.UserAgent;
            connection.Headers.Add("Accepts-Language",
                GlobalContext.Request.Headers.Get("Accepts-Language"));
    
            using (WebResponse resp = connection.GetResponse())
            {
                // Ignore response
            }
        }
        catch (Exception ex)
        {
            if (GlobalContext.Request.QueryString.Get("utmdebug") != null)
            {
                throw new Exception("Error contacting Google Analytics", ex);
            }
        }
    }
    

    您可以从 google 网站获取完整示例,只需对原始代码进行少量更改,即可使其适合您。

    https://developers.google.com/analytics/devguides/collection/?csw=1

    这是一个普遍的想法,您还有一些工作要做,但您需要的所有代码都在该文件中:http://dl.google.com/gaformobileapps/googleanalyticsformobile.zip

    【讨论】:

    【解决方案2】:

    您可以使用 Google Analytics Tracker NuGet 包。链接在这里.. https://github.com/maartenba/GoogleAnalyticsTracker

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-06
      • 2013-09-18
      • 2018-11-02
      相关资源
      最近更新 更多