2016-11-26 50 views
0

我有以下代碼,它的工作正常。使用PHP獲取特定的頁面瀏覽量谷歌分析

<?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] . "\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"); 
     } 
     } 
    } 
    } 
} 

現在我得到的所有頁面數量和獨特的頁面count.Now我的問題是我需要得到特別例如登錄頁面註冊頁面,頁面瀏覽次數etc.Can任何一個可以幫助我如何獲得特定頁面的計數。感謝

+0

既然這樣,你的問題無關與著名Laravelcodeignitercakephp框架...... – ndm

回答

1

爲了獲取每個頁面路徑的數據,您需要添加ga:pagePath作爲維度。即

$pagePath = new Google_Service_AnalyticsReporting_Dimension(); 
$pagePath->setName("ga:browser"); 

// Later on 

$request->setDimensions(array($pagePath)); 

您還可以按維度過濾數據,因此只能看到符合條件的結果。例如,只有通過增加過濾器如下顯示您的註冊頁面數據:

// 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)); 
+0

@ Curtis.Thanks了很多。我我會從一週內尋找這個。感謝很多 – iCoders

+0

@ Curits.Works like charm.Great answer.Thanks很多 – iCoders

相關問題