使用Google AnalyticsAPI获取每小时统计信息

时间:2014-07-13 15:32:40

标签: php html google-analytics

我想构建一个简单的页面,显示过去23小时gapi.class.php的每小时统计信息。所以我在下面写了代码,但似乎有些东西。它显示多个小时的相同统计信息。

define('ga_email','$email');
define('ga_password','$password');
define('ga_profile_id',$analyticsid);

require('Including/php/gapi-1.3/gapi.class.php');

$number = 1;
$temp = 0;

while($number < 24) {
    $start = date("Y-m-d",strtotime("-$number hours"));
    $date = date("Y-m-d",strtotime("-$temp hours"));

    $ga = new gapi(ga_email,ga_password);
    //$dimensions, $metrics, $sort_metric=null, $filter=null, $start_date=null, $end_date=null, $start_index=1, $max_results=30)
    $ga->requestReportData(
        ga_profile_id,
        array('browser'),
        array('pageviews','visits','visitors','avgTimeOnSite','percentNewVisits','visitBounceRate','UniquePageviews',),
        array('-visits'), 
        null, 
        $start,
        $date,
        1,
        30
    );

    ${'visits'.$number} = $ga->getVisits();
    ${'pageviews'.$number} = $ga->getPageviews();
    ${'bounces'.$number} = $ga->getVisitBounceRate();
    ${'visitors'.$number} = $ga->getVisitors();
    ${'date'.$number} = $date;

    $temp++;
    $number++;

}

然后在一个简单的表中显示它 - 我使用了一个while循环,它构建了这个表:

<table>
    <tr>
        <th>Hours:</th>
        <th>-1</th>
        <th>-2</th>
        <th>-3</th>
        <th>-4</th>
        <th>-5</th>
        <th>-6</th>
        <th>-7</th>
        <th>-8</th>
        <th>-9</th>
        <th>-10</th>
        <th>-11</th>
        <th>-12</th>
        <th>-13</th>
        <th>-14</th>
        <th>-15</th>
        <th>-16</th>
        <th>-17</th>
        <th>-18</th>
        <th>-19</th>
        <th>-20</th>
        <th>-21</th>
        <th>-22</th>
        <th>-23</th>                                                
    </tr>
    <tr>
        <th>Visits:</th>
        <td><?php echo $visits1?></td>
        <td><?php echo $visits2?></td>
        <td><?php echo $visits3?></td>
        <td><?php echo $visits4?></td>
        <td><?php echo $visits5?></td>
        <td><?php echo $visits6?></td>
        <td><?php echo $visits7?></td>
        <td><?php echo $visits8?></td>
        <td><?php echo $visits9?></td>
        <td><?php echo $visits10?></td>
        <td><?php echo $visits11?></td>
        <td><?php echo $visits12?></td>
        <td><?php echo $visits13?></td>
        <td><?php echo $visits14?></td>
        <td><?php echo $visits15?></td>
        <td><?php echo $visits16?></td>
        <td><?php echo $visits17?></td>
        <td><?php echo $visits18?></td>
        <td><?php echo $visits19?></td>
        <td><?php echo $visits20?></td>
        <td><?php echo $visits21?></td>
        <td><?php echo $visits22?></td>
        <td><?php echo $visits23?></td>
    </tr>
    <tr>
        <th>Pageviews:</th>
        <td><?php echo $pageviews1?></td>
        <td><?php echo $pageviews2?></td>
        <td><?php echo $pageviews3?></td>
        <td><?php echo $pageviews4?></td>
        <td><?php echo $pageviews5?></td>
        <td><?php echo $pageviews6?></td>
        <td><?php echo $pageviews7?></td>
        <td><?php echo $pageviews8?></td>
        <td><?php echo $pageviews9?></td>
        <td><?php echo $pageviews10?></td>
        <td><?php echo $pageviews11?></td>
        <td><?php echo $pageviews12?></td>
        <td><?php echo $pageviews13?></td>
        <td><?php echo $pageviews14?></td>
        <td><?php echo $pageviews15?></td>
        <td><?php echo $pageviews16?></td>
        <td><?php echo $pageviews17?></td>
        <td><?php echo $pageviews18?></td>
        <td><?php echo $pageviews19?></td>
        <td><?php echo $pageviews20?></td>
        <td><?php echo $pageviews21?></td>
        <td><?php echo $pageviews22?></td>
        <td><?php echo $pageviews23?></td>
    </tr>
</table>

修改 所以我尝试了谷歌分析查询资源管理器2,我得到了一些不错的数据和查询网址。我如何在PHP代码中实现它?

0 个答案:

没有答案
相关问题