dpbv85276 2016-11-26 06:40
浏览 29
已采纳

使用PHP获取Google Analytics中的特定页面查看次数

I have following code and its working fine.

<?php

require_once __DIR__ . '/vendor/autoload.php';

$analytics = initializeAnalytics();
$response = getReport($analytics);

printResults($response);

function initializeAnalytics()
{

  $KEY_FILE_LOCATION = __DIR__ . '/Student-a2aeba87a13b.json';


  $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) {


  $VIEW_ID = "107034574";


  $dateRange = new Google_Service_AnalyticsReporting_DateRange();
  $dateRange->setStartDate("30daysAgo");
  $dateRange->setEndDate("today");


  $sessions = new Google_Service_AnalyticsReporting_Metric();

 $sessions->setExpression("ga:pageviews");
  $sessions->setAlias("hits");
  echo "<pre>";

  $browser = new Google_Service_AnalyticsReporting_Dimension();
$browser->setName("ga:pagePathLevel1");
  $request = new Google_Service_AnalyticsReporting_ReportRequest();


  $request->setViewId($VIEW_ID);
  $request->setDateRanges($dateRange);

 $request->setMetrics(array($sessions));

  //Create the Dimensions object.

  /* $sessions->setExpression("ga:users"); --get total users
  /$browser->setName("ga:userType");
  $sessions->setExpression("ga:percentNewSessions"); ---new user session percentage
  *  $sessions->setExpression("ga:newUsers");
  $sessions->setExpression("ga:sessions");
  $sessions->setExpression("ga:bounceRate");--bounce tare
  $sessions->setExpression("ga:pageviews"); //total page view
  $sessions->setExpression("ga:visits");
  *
  */


  $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
  $body->setReportRequests( array( $request) );
   echo "<pre>";
//  print_r($body);
  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();
echo "<pre>";
 // print_r($rows);
    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] . "
");
      }

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

Now i am getting all page count and unique page count.Now my problem is i need to get particular page view count for example login page signup page etc.Can any one help me how to get particular page count.Thanks

  • 写回答

1条回答 默认 最新

  • duandange7480 2016-11-27 17:15
    关注

    In order to get data for each page path you will need to add ga:pagePath as a dimension. i.e.

    $pagePath = new Google_Service_AnalyticsReporting_Dimension();
    $pagePath->setName("ga:browser");
    
    // Later on
    
    $request->setDimensions(array($pagePath));
    

    You can also filter your data by dimension so you only see results that fit a criteria. For example, only show data for your signup page by adding the filter as follows:

    // Create the DimensionFilter.
    $dimensionFilter = new Google_Service_AnalyticsReporting_DimensionFilter();
    $dimensionFilter->setDimensionName('ga:pagePath');
    $dimensionFilter->setOperator('EXACT');
    $dimensionFilter->setExpressions(array('/signup'));
    
    // Create the DimensionFilterClauses
    $dimensionFilterClause = new Google_Service_AnalyticsReporting_DimensionFilterClause();
    $dimensionFilterClause->setFilters(array($dimensionFilter));
    
    //Later on
    
    $request->setDimensionFilterClauses(array($dimensionFilterClause));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥15 第一个已完成,求第二个做法
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?