![](/img/trans.png)
[英]Invalid Response Error when Retrieving Google Analytics Data with a Service Account in C# .NET
[英]Retrieving data from Google Analytics API using .NET/C#
我正在嘗試使用本地控制台應用從谷歌分析中檢索數據。 我能夠提取一些數據,而無需登錄谷歌帳戶,只使用API。
問題是我沒有得到正確的值,我不知道如何格式化代碼以提取正確的值。 我不想在特定時間范圍內檢索所有訪客,在本例中為“2012-01-01” - “2014-02-20”。 查看Google Analytics信息中心時,實際訪問者數量會增加10倍。 在調試代碼時,我得到了15000。 我在控制台中顯示d.TotalResults可能是錯誤的,變量“d”包含許多不同的屬性。
這是我正在運行的代碼:
public static void Main(string[] args)
{
var serviceAccountEmail = "MY@developer.gserviceaccount.com";
var certificate = new X509Certificate2(@"C:\Users\User\Desktop\key.p12", "notasecret", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { AnalyticsService.Scope.Analytics }
}.FromCertificate(certificate));
// Create the service.
//Twistandtango
var gas = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "TestGoogleAnalytics",
});
var r = gas.Data.Ga.Get("ga:MYProfileID", "2012-01-01", "2014-02-20", "ga:visitors");
//Specify some addition query parameters
r.Dimensions = "ga:pagePath";
r.Sort = "-ga:visitors";
r.MaxResults = 5;
//Execute and fetch the results of our query
Google.Apis.Analytics.v3.Data.GaData d = r.Execute();
Console.WriteLine(d.TotalResults);
Console.ReadLine();
}
我正在嘗試使用此工具https://ga-dev-tools.appspot.com/explorer/查詢我想要的結果。 當我在我的代碼中實現它時,它看起來像這樣:
public static void Main(string[] args)
{
var serviceAccountEmail = "MY@developer.gserviceaccount.com";
var certificate = new X509Certificate2(@"C:\Users\User\Desktop\key.p12", "notasecret", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { AnalyticsService.Scope.Analytics }
}.FromCertificate(certificate));
// Create the service.
//Twistandtango
var gas = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "TestGoogleAnalytics",
});
var r = gas.Data.Ga.Get("ga:MYProfileID", "2012-01-01", "2014-02-20", "ga:visits");
//Specify some addition query parameters
r.Dimensions = "ga:visitCount";
r.Metrics = "ga:visits";
r.Segment = "gaid::-1";
//r.Sort = "-ga:visitors";
r.MaxResults = 10000;
//Execute and fetch the results of our query
Google.Apis.Analytics.v3.Data.GaData d = r.Execute();
Console.WriteLine(d.TotalResults);
Console.ReadLine();
}
}
但是,在將指標( r.Metrics = "ga:visits";
)添加到項目后,我收到此錯誤:
錯誤7無法將屬性或索引器“Google.Apis.Analytics.v3.DataResource.GaResource.GetRequest.Metrics”分配給 - 它是只讀的
此處還有分析v3中的類索引:
有誰知道這是如何工作的? 如何從我指定的時間范圍中檢索訪客總數?
謝謝
您的問題是您在data.ga.get方法中提供指標,您不使用r.metrics指標添加更多它們一個單獨的,用, 。 在這種情況下,ga:visits是您要求的指標。
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();
如果有更多的結果設置為最大結果,則不會返回所有行。 您需要檢查下一個鏈接以確保沒有更多行。
關於下一個鏈接更新以回答下面的問題。 您當前將max-results設置為10000,這是可以在塊中返回的最大行數。 如果有超過10000行,那么您將獲得需要請求下一行行的下一個鏈接。 TotalResult將為您提供應返回的總行數。 下面我繼續使用.execute請求更多數據,直到沒有下一個鏈接。
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.