Analytics Reporting API V4 Client Library for .NET

前端 未结 3 1356
难免孤独
难免孤独 2020-12-23 02:46

I\'m trying to get some data from our google analytics instance and I\'d like to use the Analytics Reporting API V4 Client Library for .NET (https://developers.google.com/ap

3条回答
  •  醉梦人生
    2020-12-23 03:17

    Here are the steps updated for Sep 2019.

    First, understand that there are two choices under OAuth: User credentials and Service Account credentials. User credentials are meant to be used when you do not know which Google Analytics account you will be connected to, hence the user grants your application permission. Service Account credentials are meant to be used, e.g. if you build your own dashboard for your company to display Google Analytics data.

    Most of the time, if you need programmatic access to Analytics data, it is the second case.

    The steps below should get you started for a simple C# example. Note that the Google web console part may vary slightly, but should be easy to find nevertheless.

    1. Go to the Google API Console. Create a Project if prompted.
    2. Go to Service accounts.
    3. Create a new Service Account. You should have an account with a random-generated email address (mine is ending with xxx@xxx.iam.gserviceaccount.com)
    4. Find the Create Key button. Choose JSON and download the file. This is your private key and your only copy. Do not lose it.
    5. Go to your Google Analytics admin panel. Grant access to the Service Account using its email address, the same way you would grant access to other users.

    The Google configuration is done. Now jump into Visual Studio.

    1. Create a new C# Console Project.
    2. Get the Nuget package Google.Apis.AnalyticsReporting.v4. It should also automatically download the core packages as well.
    3. Grab the JSON file downloaded earlier, put it in the project, set its Property to Content and Copy Always.
    using Google.Apis.AnalyticsReporting.v4.Data;
    using System;
    
    namespace ConsoleApplication {
        class Program {
            static void Main(string[] args) {
                var credential = Google.Apis.Auth.OAuth2.GoogleCredential.FromFile("serviceAccount.json")
                    .CreateScoped(new[] { Google.Apis.AnalyticsReporting.v4.AnalyticsReportingService.Scope.AnalyticsReadonly });
    
                using (var analytics = new Google.Apis.AnalyticsReporting.v4.AnalyticsReportingService(new Google.Apis.Services.BaseClientService.Initializer {
                    HttpClientInitializer = credential
                })) {
                    var request = analytics.Reports.BatchGet(new GetReportsRequest {
                        ReportRequests = new[] {
                            new ReportRequest{
                                DateRanges = new[] { new DateRange{ StartDate = "2019-01-01", EndDate = "2019-01-31" }},
                                Dimensions = new[] { new Dimension{ Name = "ga:date" }},
                                Metrics = new[] { new Metric{ Expression = "ga:sessions", Alias = "Sessions"}},
                                ViewId = "99999999"
                            }
                        }
                    });
                    var response = request.Execute();
                    foreach (var row in response.Reports[0].Data.Rows) {
                        Console.Write(string.Join(",", row.Dimensions) + ": ");
                        foreach (var metric in row.Metrics) Console.WriteLine(string.Join(",", metric.Values));
                    }
                }
    
                Console.WriteLine("Done");
                Console.ReadKey(true);
            }
        }
    }
    

提交回复
热议问题