サーチ…


備考

GoogleアナリティクスレポートAPI V4は、 Googleアナリティクスのレポートデータにアクセスするための最も高度なプログラム的な方法です。 Google Analytics Reporting APIを使用すると、次のことができます。

  • Googleアナリティクスのデータを表示するカスタムダッシュボードを作成します。
  • 複雑なレポート作成タスクを自動化することで、時間を節約できます。
  • Googleアナリティクスのデータを他のビジネスアプリケーションと統合します。

特徴

Googleアナリティクスは、強力なデータレポートインフラストラクチャに基づいて構築されています。 GoogleアナリティクスレポートAPI V4を使用すると、 Googleアナリティクスプラットフォームの機能にアクセスできます。 APIには次の重要な機能があります。

  • メトリック式:APIを使用すると、組み込みメトリックだけでなく、数学的操作で表されるメトリックの組み合わせも要求できます。たとえば、式ga:goal1completions / ga:sessionsを使用して、セッション数ごとに目標達成をリクエストできます。
  • 複数の日付範囲:APIを使用すると、1つのリクエストで2つの日付範囲のデータを取得できます。
  • コホートと生涯価値:APIにはコホートとライフタイムのバリューレポートを要求する豊富な語彙があります。
  • 複数のセグメント:APIを使用すると、1回のリクエストで複数のセグメントを取得できます。

Oauth2 C#を使用した単一レポートの例

この例では、公式のGoogle .netクライアントライブラリを使用しています。

PM> インストールパッケージGoogle.Apis.AnalyticsReporting.v4

認可次のいずれかのOAuthスコープが必要です。

Oauth2

// These are the scopes of permissions you need.
string[] scopes = new string[] { AnalyticsReportingService.Scope.AnalyticsReadonly };   // View your Google Analytics Data

UserCredential credential;
using (var stream = new FileStream(clientSecretJson, FileMode.Open, FileAccess.Read))
{
    string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    credPath = Path.Combine(credPath, ".credentials/", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);

    // Requesting Authentication or loading previously stored authentication for userName
   credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
       scopes,
      userName,
      CancellationToken.None,
      new FileDataStore(credPath, true)).Result;
}

// Create Reporting API service.
var service = new AnalyticsReportingService(new BaseClientService.Initializer()
            {
             HttpClientInitializer = credential,
             ApplicationName = string.Format("{0} Authentication", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name),
            });

報告要求

// Create the DateRange object.
DateRange June2015 = new DateRange() { StartDate = "2015-01-01", EndDate = "2015-06-30" };
DateRange June2016 = new DateRange() { StartDate = "2016-01-01", EndDate = "2016-06-30" };
List<DateRange> dateRanges = new List<DateRange>() { June2016, June2015 };           
                       

// Create the ReportRequest object.
// This should have a large number of rows
ReportRequest reportRequest = new ReportRequest
{
    ViewId = ConfigurationManager.AppSettings["GoogleAnaltyicsViewId"],
    DateRanges = dateRanges,
    Dimensions = new List<Dimension>() { new Dimension() { Name = "ga:date" }, new Dimension() { Name = "ga:usertype" } },
    Metrics = new List<Metric>() { new Metric() { Expression= "ga:users" }, new Metric() { Expression = "ga:sessions" } },
    PageSize = 1000,
};
            
List<ReportRequest> requests = new List<ReportRequest>();
requests.Add(reportRequest);


var getReport = new GetReportsRequest() { ReportRequests = requests };
var response = service.Reports.BatchGet(getReport).Execute();

単一レポートの例

APIリクエストはHTTP POSTであり、APIエンドポイントの最後にアクセストークンが添付されています。

認可次のいずれかのOAuthスコープが必要です。

データの転記時には、 ContentType = "application/Json";使用してくださいContentType = "application/Json";

https://analyticsreporting.googleapis.com/v4/reports:batchGet?Access_token={from auth}
{
  "reportRequests":[
  {
    "viewId":"XXXX",
    "dateRanges":[
      {
        "startDate":"2015-06-15",
        "endDate":"2015-06-30"
      }],
    "metrics":[
      {
        "expression":"ga:sessions"
      }],
    "dimensions": [
      {
        "name":"ga:browser"
      }]
    }]
}


Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow