Track ASP.NET Web APIs with Google Analytics

I am developing an API for my site so that users can get and use their own websites / applications.

I would like to keep usage statistics for using the various API calls that I allow, and I would like to use Google Analytics, as I do for my site. Can I track server-side code using Google Analytics? or specifically web api?

thanks

+4
source share
2 answers

Google Analytics provides a mobile code to track server-side statistics. You can use this code to archive your purpose.

- google , . 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

+6

Google Analytics Tracker NuGet. .. https://github.com/maartenba/GoogleAnalyticsTracker

0

Source: https://habr.com/ru/post/1524001/


All Articles