问题
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 />");
}
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.
Thank you very much in advance.
回答1:
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();
来源:https://stackoverflow.com/questions/10752697/how-to-send-google-analytic-report-query-using-asp-net-google-analytic-api-versi