如何使用ASP.net谷歌分析API版本3(Google.Apis.Analytics.v3.dll)发送谷歌分析报表查询?如何使用、报表、版本、net

2023-09-04 02:19:00 作者:白露饮尘霜

我目前正在开发GA报告Web应用程序使用asp.net谷歌API V3我公司( Google.Apis.Analytics.v3.dll )。据我所知,.NET API的旧版本和一些例子,但是当有新的版本,我还不如用它。这下code段是如何发送查询中使用获取遗传数据的 Google.GData.Analytics.dll 。但它的旧版本。

I am currently developing GA reporting web app for my company using asp.net Google api V3(Google.Apis.Analytics.v3.dll). I understand there is older version of .net api and some example for it but when there is newer version I might as well use it. This following code snippet is how you send query to retrieve GA data using Google.GData.Analytics.dll. But it's older version.

AnalyticsService _service = new AnalyticsService("GoogleAnalytics");
_service.setUserCredentials("YourUsername", "YourPassword");
DataQuery dataQuery = new DataQuery(Conststr_Url);
dataQuery.Ids = "ga:xxxxxx";
dataQuery.Dimensions = "ga:date";
dataQuery.Metrics = "ga:visits";
dataQuery.GAStartDate = "2012-05-10";
dataQuery.GAEndDate = "2012-05-24";
DataFeed visits = _service.Query(dataQuery);
foreach (DataEntry entry in visits.Entries)
{
      Response.Write("Date: " + entry.Title.Text.Replace("ga:date=", "") + " Visits: " +   entry.Metrics[0].Value + "<br />");
}

使用版本3,我设法成功地利用他们提供的Tasks.ASP.NET.SimpleOAuth2示例应用程序做oauth2授权的一部分。但是,当我试图取代谷歌分析服务的任务服务,我只是不知道从哪里开始。我所知道的是,宣布了解析服务,从这一点我没有什么可去:)。可谁能帮助我的code段升技或直接我一个〔实施例的网站。

With version3, I managed to do oauth2 authorisation part successfully using Tasks.ASP.NET.SimpleOAuth2 sample application they provide. But when I try to replace the task service with google analytic service, I just don't know where to start. All I know is declaring the analytic service, and from that point I have nothing to go on :) .Can anyone help me with abit of code snippets or direct me to an exmaple site.

非常感谢你提前。

推荐答案

在计算器摸索和谷歌几天之后,这里是我发现了什么。不像previous版本,你需要创建一个使用AnalyticService一个的GetRequest对象,并使用该对象来获得GaData结果对象返回获取数据。这里是code:

After searching around on stackoverflow and google for a few days, here is what I found out. Unlike previous version, you need to create a "GetRequest" object using AnalyticService and fetch the data using that object to get "GaData" result object back. Here is the code:

var request = _analyticsService.Data.Ga.Get(ProfileID, StartDate, EndDate, Metrics);
request.Dimensions = Dimensions;
request.Segment = Segment;
request.Sort = Sort;
request.StartIndex = StartIndex;
request.MaxResults = MaxResult;
GaData results = request.Fetch();