2017-09-04 5 views
0

でページビューを取得:グーグルアナリティクスAPI - 私はGoogle AnalyticsのAPIからのデ「ハロー」のアプリを実行することができたURL

function getFirstProfileId($analytics) { 
    // Get the user's first view (profile) ID. 

    // Get the list of accounts for the authorized user. 
    $accounts = $analytics->management_accounts->listManagementAccounts(); 

    if (count($accounts->getItems()) > 0) { 
     $items = $accounts->getItems(); 
     $firstAccountId = $items[0]->getId(); 

     // Get the list of properties for the authorized user. 
     $properties = $analytics->management_webproperties 
       ->listManagementWebproperties($firstAccountId); 

     if (count($properties->getItems()) > 0) { 
      $items = $properties->getItems(); 
      $firstPropertyId = $items[0]->getId(); 

      // Get the list of views (profiles) for the authorized user. 
      $profiles = $analytics->management_profiles->listManagementProfiles($firstAccountId, $firstPropertyId); 

      if (count($profiles->getItems()) > 0) { 
       $items = $profiles->getItems(); 

       // Return the first view (profile) ID. 
       return $items[0]->getId(); 

      } else { 
       throw new Exception('No views (profiles) found for this user.'); 
      } 
     } else { 
      throw new Exception('No properties found for this user.'); 
     } 
    } else { 
     throw new Exception('No accounts found for this user.'); 
    } 
} 

function getResults($analytics, $profileId) { 
    // Calls the Core Reporting API and queries for the number of sessions 
    // for the last seven days. 
    return $analytics->data_ga->get(
     'ga:' . $profileId, 
     '7daysAgo', 
     'today', 
     'ga:sessions' 
    ); 
} 

(全体)アカウントのページビューを返します。 しかし、私は、特定のURLのために、私はそうする方法を見つけようとしている

を(全範囲のための)ページビューを取得したいと思いますが、私が見つけた最もsimilar pageは、のようです異なる構文で書かれています。だから私は現在のアプリでそれらのパラメータを入力する方法を知らない。

どのような考えですか?詳細については

--EDIT--

、前のコード、版画:

しかし、このコード(私は前に提供されたリンクの "構文" を持つ)

function getReport($analytics) { 

    // Replace with your view ID, for example XXXX. 
    $VIEW_ID = "40xxxyyy9"; // I got it from The Google Account Explorer 

    // Create the DateRange object. 
    $dateRange = new Google_Service_AnalyticsReporting_DateRange(); 
    $dateRange->setStartDate("7daysAgo"); 
    $dateRange->setEndDate("today"); 

    // Create the Metrics object. 
    $sessions = new Google_Service_AnalyticsReporting_Metric(); 
    $sessions->setExpression("ga:sessions"); 
    $sessions->setAlias("sessions"); 

    // Create the ReportRequest object. 
    $request = new Google_Service_AnalyticsReporting_ReportRequest(); 
    $request->setViewId($VIEW_ID); 
    $request->setDateRanges($dateRange); 
    $request->setMetrics(array($sessions)); 

    $body = new Google_Service_AnalyticsReporting_GetReportsRequest(); 
    $body->setReportRequests(array($request)); 
    return $analytics->reports->batchGet($body); 
} 

function printResults($reports) { 
    for ($reportIndex = 0; $reportIndex < count($reports); $reportIndex++) { 
    $report = $reports[ $reportIndex ]; 
    $header = $report->getColumnHeader(); 
    $dimensionHeaders = $header->getDimensions(); 
    $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries(); 
    $rows = $report->getData()->getRows(); 

    for ($rowIndex = 0; $rowIndex < count($rows); $rowIndex++) { 
     $row = $rows[ $rowIndex ]; 
     $dimensions = $row->getDimensions(); 
     $metrics = $row->getMetrics(); 
     for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) { 
     print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n"); 
     } 

     for ($j = 0; $j < count($metricHeaders) && $j < count($metrics); $j++) { 
     $entry = $metricHeaders[$j]; 
     $values = $metrics[$j]; 
     print("Metric type: " . $entry->getType() . "\n"); 
     for ($valueIndex = 0; $valueIndex < count($values->getValues()); $valueIndex++) { 
      $value = $values->getValues()[ $valueIndex ]; 
      print($entry->getName() . ": " . $value . "\n"); 
     } 
     } 
    } 
    } 
} 

プリントアウト:

Uncaught exception 'Google_Service_Exception' with message '{ "error": { "code": 403, "message": "Google Analytics Reporting API has not been used in project api-project-395xxxxxxx2105 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/analyticsreporting.googleapis.com/overview?project=api-project-395xxxxxxx2105 then retry. 

そして、自分のプロジェクトへのコンソールリンクをたどった場合。それは私が既に有効になっていることを教えてくれます(そうでなければ、質問の最初のコードはうまくいかないはずです)。

+0

これは実際にはGoogleアナリティクスAPIの2つの異なるバージョン(v3とv4)の異なる構文です。これらは、異なる構文を持つ異なるAPIクライアントを必要とします。 v3は少しわかりやすいですが、v4はもう少しヒントを提供しています(ヒストグラム、ピボットなど)。将来的にはv4の使用を検討することをお勧めします。 –

答えて

3

あなたの例は実際にはページビューではなくセッション数を返します。したがって、URLのページビュー数を取得するには、a)ga:sessionsをga:pageViewsに変更し、b)データを期待Urlに制限するフィルタを適用する必要があります。

フィルタあなたの機能ビット(this basically taken from the documentation)を変更する必要があると思いますのでによって、オプションのパラメータの配列の一部としてそれらを渡すことによって適用することができます

function getResults($analytics, $profileId) { 

    $optParams = array(
      'filters' => 'ga:[email protected]/my/url' 
    ); 

    return $analytics->data_ga->get(
     'ga:' . $profileId, 
     '7daysAgo', 
     'today', 
     'ga:pageViews', 
     $optParams 
    ); 
} 

IIRC [email protected]は、部分一致でなければなりませんフィルタ(ドキュメントを参照してください)ともちろん、実際のURLに "/ my/url"を変更する必要があります。

また、さまざまなバージョンのAPIについてのコメントもあります。

+0

はい!それはうまくいくようです!どのように日付の既存の範囲全体を得るために、生地? –

+1

「すべて選択」コマンドがないので、「7daysAgo」をGAアカウントを開始した日付に置き換える必要があります。 –

関連する問題