会话的Google Analytics分析自定义维度

时间:2016-04-26 18:33:29

标签: php session google-analytics dimension

我正在尝试获取更详细的会话信息,但我不确定如何做到这一点。我想看看会议的来源。确定有多少来自refferal,direct,social和email。这就是我从api查询和打印结果的方法

// Query the API
function getResults(&$analytics, $profileId) {
    // Calls the Core Reporting API and queries the specified data 
    return $analytics->data_ga->get(
            'ga:' . $profileId,
            '30daysAgo',
            'today',
            'ga:Sessions,ga:avgSessionDuration,ga:bounceRate,ga:pageviews,ga:percentNewSessions,ga:pageviewsPerSession');
}




function printResults(&$results) {
  // Parses the response from the Core Reporting API and prints
  // the profile name and total sessions.
  if (count($results->getRows()) > 0) {

    // Get the profile name.
    $profileName = $results->getProfileInfo()->getProfileName();

    // Get the entry for the first entry in the first row.
    $rows = $results->getRows();
    $visits = $rows[0][0];
    $avgVisitDuration = $rows[0][1];
    $bounceRate = $rows[0][2];
    $pageView = $rows[0][3]; 
    $percentNewSession = $rows[0][4];
    $pagesPerVisit = $rows[0][5]; 
    // Print the results.
echo '
<style>
table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}
th, td {
    padding: 5px;
    text-align: left;
}
</style>
</head>
<body>

<table style="width:100%">
  <tr>
    <td>Total Visits</td>
    <td>',$visits,'</td>
  </tr>
  <tr>
    <td>Average Visit Duration</td>
    <td>', floor($avgVisitDuration/ 60),' minutes ',$avgVisitDuration % 60,' seconds </td>
  </tr>
  <tr>
    <td>Bounce Rate</td>
    <td>',$bounceRate,' %</td>
  </tr>
  <tr>
    <td>Page Views</td>
    <td>',$pageView,'</td>
  </tr>
  <tr>
    <td>Percentage New Visits</td>
    <td>',$percentNewSession,' %</td>
  </tr>
  <tr>
    <td>Pages / Visit</td>
    <td>',$pagesPerVisit,'</td>
  </tr>
</table>

</body>';


  } else {
    print "No results found.\n";
  }
}


$analytics = getService();
$profile = getFirstProfileId($analytics);
$results = getResults($analytics, $profile);
printResults($results);

更新:使用此维度我得到错误语法错误,意外''); '(T_CONSTANT_ENCAPSED_STRING),在'ga'一行的getResults上期待')'。简档 这就是我试图查询api的方式

$optParams = array(
          'dimensions' => 'ga:medium'
          ');  
function getResults(&$analytics, $profileId) {
    // Calls the Core Reporting API and queries the specified data

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

0 个答案:

没有答案