代码之家  ›  专栏  ›  技术社区  ›  Ben

如何使用php从google analytics api获取接下来的10000个数据?

  •  1
  • Ben  · 技术社区  · 8 年前

    很好的一天,

    有没有办法从谷歌分析API获取下一个10000个数据? 我想在获得第一个10000个数据后获得下一组数据。有没有办法做到这一点?我正在使用谷歌分析api php客户端库。

    这是我的代码:

     <?php
     $analytics = initializeAnalytics();     
     $response = getReport($analytics);  
     printResults($response);
    
    function initializeAnalytics()
    {
        $KEY_FILE_LOCATION = __DIR__ . 'MyFileDirectory';
        // Create and configure a new client object.
        $client = new Google_Client();
        $client->setApplicationName("Hello Analytics Reporting");
        $client->setAuthConfig($KEY_FILE_LOCATION);
        $client-
    >setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
        $analytics = new Google_Service_AnalyticsReporting($client);
        return $analytics;
     }
    
    function getReport($analytics) {
        // Replace with your view ID, for example XXXX.
        $VIEW_ID = "MyViewId";
    
        // Create the DateRange object.
        $dateRange = new Google_Service_AnalyticsReporting_DateRange();
        $dateRange->setStartDate("2017-04-01");
        $dateRange->setEndDate("2017-06-19");
    
        // Create the Metrics object.
        $totalEvents = new Google_Service_AnalyticsReporting_Metric();
        $totalEvents->setExpression("ga:totalEvents");
        $totalEvents->setAlias("totalEvents");
    
        //Create the Dimensions object.
        $clientId = new Google_Service_AnalyticsReporting_Dimension();
        $clientId->setName("ga:dimension4");
        $sessionId = new Google_Service_AnalyticsReporting_Dimension();
        $sessionId->setName("ga:dimension5");
        $eventLabel = new Google_Service_AnalyticsReporting_Dimension();
        $eventLabel->setName("ga:eventLabel");
        $timestamp = new Google_Service_AnalyticsReporting_Dimension();
        $timestamp->setName("ga:dimension3");
    
        // Create the ReportRequest object.
        $request = new Google_Service_AnalyticsReporting_ReportRequest();
        $request->setViewId($VIEW_ID);
        //set number of rows
        $request->setPageSize(10000);
        $request->setDateRanges($dateRange);
        $request->setMetrics(array($totalEvents));
        $request->setDimensions(array($clientId,$sessionId,$eventLabel, 
        $timestamp));   
        $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
        $body->setReportRequests( array( $request) );
        return $analytics->reports->batchGet( $body );
    }
    }
    ?>
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   Linda Lawton - DaImTo    8 年前

    这将得到前10页的数据。我把它限制在10个,以防止它吃掉我的配额。如果你删除它,它将继续抓走所有的行,吃掉你的配额。

    注意:只有在您有一份报告的情况下,这才有效。如果你有不止一个报告,那么你必须做一些奇特的事情,将正确的pageToken应用到每个报告,并删除完整的报告。此时,除了返回的报告在报告数组中的位置外,没有报告Id或匹配发送到报告的内容的方法。

    $service = new Google_Service_AnalyticsReporting($client); 
    
    // Create the DateRange object.
    $dateRange = new Google_Service_AnalyticsReporting_DateRange();
    $dateRange->setStartDate("2016-01-01");
    $dateRange->setEndDate("2017-06-30");
    
    // Create the Metrics object.
    $sessions = new Google_Service_AnalyticsReporting_Metric();
    $sessions->setExpression("ga:sessions");
    $sessions->setAlias("sessions");
    
    //Create the Dimensions object.
    $date = new Google_Service_AnalyticsReporting_Dimension();
    $date->setName("ga:date");
    $pagePath = new Google_Service_AnalyticsReporting_Dimension();
    $pagePath->setName("ga:pagePath");
    
    // Create the ReportRequest object.
    $request = new Google_Service_AnalyticsReporting_ReportRequest();
    $request->setViewId("81692014");
    $request->setPageSize("10000");  
    $request->setDateRanges($dateRange);
    $request->setDimensions(array($date,$pagePath));
    $request->setMetrics(array($sessions));
    $request->setMetrics(array($sessions));
    
    $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
    $body->setReportRequests( array( $request) );
    $data =  $service->reports->batchGet( $body );
    
    
    // Remove count if you really want everything.
    $cnt = 0; 
    while ($data->reports[0]->nextPageToken > 0 && $cnt < 10) {
        // There are more rows for this report.
        $body->reportRequests[0]->setPageToken($data->reports[0]->nextPageToken);
        $data =  $service->reports->batchGet( $body );
        $cnt++;
        }
    

    Google Analytics V4 pagination 我使用的完整代码可以在 GitHub 注意,这段代码是从我的示例项目中删除的。