使用Google Analytics(分析)追踪ASP.NET Web API [英] Tracking ASP.NET Web API with google analytics

查看:63
本文介绍了使用Google Analytics(分析)追踪ASP.NET Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm developing an API for my website that users can access and use own their own websites/apps.

我想保留允许使用的不同API调用的使用情况统计信息,并希望像我在自己的网站上一样使用Google Analytics(分析).是否可以在服务器端代码上使用Google Analytics(分析)进行跟踪?还是专门的网络API?

I would like to keep usage statistics for the use of the different API calls I allow, and would like to use Google Analytics like I do for my website. Is it possible to track with Google Analytics on server side code? or specifically web api?

谢谢

推荐答案

Google分析已为

Google analytics have give the mobile code to track the statistics from server side. You may use that code to archive your goal.

这实际上是要从服务器端向Google发出网络请求,并提供您提供给他的信息.对该调用的gif图片进行了调用,其url参数为:

What this actually do is to make a web request from server side to google with the informations you give him. The call is done to a gif image with url parameters as:

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网站上获得完整的示例,并且对原始代码的更改很少,以使其适合您.

You can get the full example from google site and with little change on original code, to make it work for you.

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

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

This is a general idea, you have some work to do, but all the code you need is on that file : http://dl.google.com/gaformobileapps/googleanalyticsformobile.zip

这篇关于使用Google Analytics(分析)追踪ASP.NET Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆