2017-09-04 244 views
0

获得网页浏览,我能够从谷歌分析API运行德“你好”的应用程序:谷歌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' 
    ); 
} 

返回的(全)账户的页面访问量。 我想获得的页面浏览量(整个范围)特定网址

我试图找出如何做到这一点,但最similar page,我发现,似乎是用不同的语法编写。所以我不知道如何在当前应用中输入这些参数。

有什么想法?

- 编辑 -

欲了解更多信息,以前的代码,打印:

但这码(与链接的 “语法” 我之前提供)

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 Analytics API的两个不同版本(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]应该是部分匹配一个过滤器(或者查阅文档),当然你需要把“/ my/url”改成你的实际URL。

另请注意我对不同API版本的评论。

+0

是的!这似乎工作!如何获得整个现有的日期范围,面团? –

+1

没有“全选”命令,您需要将“7daysAgo”替换为开始GA帐户的日期。 –

相关问题