且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

利用谷歌Analytics(分析)API检索数据的.NET / C#

更新时间:2023-02-08 10:26:06

您的问题是,你提供给data.ga.get方法的指标,您不使用r.metrics指标添加更多的人一用一。在这种情况下GA:访问是您所请求的指标

Your problem is that you supply metrics in the data.ga.get method, you dont use r.metrics metrics add more them one separate them with a ,. In this case ga:visits is the metrics you are requesting.

var r = gas.Data.Ga.Get("ga:MYProfileID", "2012-01-01", "2014-02-20", "ga:visits");

尺寸不是必需的申请,所以你可以和删除 r.Dimensions =GA:visitCount; 看看返回。

请记住,简单地做 Google.Apis.Analytics.v3.Data.GaData D = r.Execute(); 不会返回所有行,如果有更那么你所拥有的最大结果集。你需要检查你的下一个环节,以确保你没有更多的行。

Remember that simply doing Google.Apis.Analytics.v3.Data.GaData d = r.Execute(); isn't going to return all the rows if there are more then what you have max result set to. You need to check your next link to make sure you don't have more rows.

响应更新以低于质疑有关下一个环节。 您目前有 MAX-结果的设置为10000这是可在一大块被返回的行的最大数量。如果有更多的则10000行,那么你将得到一个 NEXTLINK 你需要请求行的下一大块。 TotalResult会给你行,你应该返回的总数。下面我继续使用.execute,直到没有了下一个环节,要求更多的数据。

Update in response to question below about next link. You currently have max-results set to 10000 which is the max number of rows that can be returned in a chunk. If there are more then 10000 rows then you will get a nextlink you need to request the the next chunk of rows. TotalResult will give you the total number of rows that you should be returning. Below I keep requesting more data using .execute until there isn't anymore Next links.

List result = new List();
 do {
     try
       {
       GaData DataList = request.Execute();  // Make the request
       result.AddRange(DataList.Rows);       // store the Data to return later
       // hack to get next link stuff
      totalResults = (!DataList.TotalResults.HasValue) ? 0 : Int32.Parse(DataList.TotalResults.ToString());
       rowcnt = rowcnt + DataList.Rows.Count;
       NextLink = DataList.NextLink;                       
       request.StartIndex = rowcnt + 1 ;
       }
      catch (Exception e)
         {
          Console.WriteLine("An error occurred: " + e.Message);
          totalResults = 0;
         }
 } while (request.StartIndex <= totalResults);