2014-02-21 34 views
4

我試圖從本地控制檯應用程序檢索谷歌分析數據。我能夠提取一些數據,而無需登錄到Google帳戶,只使用API​​。使用.NET/C從Google AnalyticsAPI檢索數據#

問題是我沒有得到正確的值,我不知道如何格式化代碼以提取正確的值。我不想在特定的時間範圍內檢索所有訪問者,在這種情況下「2012-01-01」 - 「2014-02-20」。在Google Analytics(分析)信息中心中查看時,真正的訪問者數量是10倍。調試代碼時,我得到了15000的數字。我在控制檯顯示d.TotalResults可能是錯誤的,變量「d」包含許多不同的屬性。

這是我運行代碼:

public static void Main(string[] args) 
{ 
    var serviceAccountEmail = "[email protected]"; 

    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 = "[email protected]"; 

    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";)到項目後,我得到這個錯誤:

Error 7 Property or indexer 'Google.Apis.Analytics.v3.DataResource.GaResource.GetRequest.Metrics' cannot be assigned to -- it is read only

而且這裏有一個班在分析一個指數V3:

https://developers.google.com/resources/api-libraries/documentation/analytics/v3/csharp/latest/annotated.html

沒有人有是如何工作的知識呢?我如何檢索我指定的時間範圍內的遊客總量?

THX

+0

你也選擇了PagePath,你將不得不把它們總和,以獲得全部總數。你確定你不是在尋找訪問者還是訪客? – DaImTo

+0

嗯,我只是在我指定的日期之間尋找所有訪問。所有訪問總數,新,舊等。我怎麼能總結他們呢?猜猜我還沒有弄清楚這是如何工作,嘗試了不同的維度和指標,但我認爲我沒有正確地格式化代碼,因爲我沒有得到我期望的結果。我認爲你可以結合過濾器或尺寸,但我不知道 – koffe14

回答

1

你的問題是,你在data.ga.get方法提供指標,您不使用r.metrics指標增加更多的他們一用一它們分開,。在這種情況下,ga:訪問是您請求的指標。

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行,那麼您將得到一個nextlink您需要請求下一行的行。 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); 
+0

好吧認爲iv'e得到它,謝謝。不知道你的意思是通過檢查下一個鏈接。是的,看起來我的應用程序並不是針對所有訪問進行查詢,只是其中的一部分。你如何去收集總訪問量作爲r.Execute的替代選擇? – koffe14

+0

已更新,在nextlink上添加更多信息 – DaImTo

+0

當我嘗試將.Rows添加到結果集合中時出現錯誤。什麼樣的結果是? – Ras