更改显示Google分析数据的表格内容

时间:2016-01-12 19:23:40

标签: php ajax google-analytics html-table gapi

所以我使用的是GAPI api,它是Google Analytics的PHP api。我正确地提取了分析数据,并且可以手动更改表以反映我想要查看的任何数据。我希望在左侧有一个不同分析的列表,当点击它时,它将使用新标准填充表格。我假设我可以使用AJAX来做到这一点,但想了解更多关于如何入门的信息。我熟悉ajax,但甚至不知道如何从这里开始。

<div class="content">
        <div class="container-fluid"> 
            <p>This is the secure backend for administrators. You are currently logged in. </p>
            <div class="row">
                <div class="col-md-4">
                    <div class="card ">
                        <table class='table table-hover'>
                            <tr>
                                <th>Demographics</th>
                            </tr>
                            <tr>
                                <td><a href="">Language</a></td>
                            </tr>
                            <tr>
                                <td><a href="">Country</a></td>
                            </tr>
                            <tr>
                                <td><a href="">City</a></td>
                            </tr>
                            <tr>
                                <th>System</th>
                            </tr>
                            <tr>
                                <td><a href="">Browser</a></td>
                            </tr>
                            <tr>
                                <td><a href="">Operating System</a></td>
                            </tr>
                            <tr>
                                <td><a href="">Service Provider</a></td>
                            </tr>
                            <tr>
                                <th>Mobile</th>
                            </tr>
                            <tr>
                                <td><a href="">Operating System</a></td>
                            </tr>
                            <tr>
                                <td><a href="">Service Provider</a></td>
                            </tr>
                            <tr>
                                <td><a href="">Screen Resolution</a></td>
                            </tr>
                        </table>
                    </div>
                </div>
                <div class="col-md-8">
                    <div class="card ">
                        <table class='table table-hover'>
                            <tr>
                                <th>Browser</th>
                                <th>Sessions</th>
                                <th>% New Sessions</th>
                            </tr>
                            <?php
                            $ga1 = new gapi("service_account@example.com", "key.p12");
                            $ga1->requestReportData('id',array('browser'),array('sessions','percentNewSessions'),'-sessions',$filter, '30daysAgo', NULL, 1, 10);

                            foreach($ga1->getResults() as $result):
                            ?>
                            <tr>
                                <td><?php echo $result ?></td>
                                <td><?php echo $result->getSessions() ?></td>
                                <td><?php echo $result->getPercentNewSessions() ?></td>
                            </tr>
                            <?php
                            endforeach
                            ?>
                            </table>

                            <table class='table table-hover'>
                            <tr>
                                <th>Total Results</th>
                                <td><?php echo $ga1->getTotalResults() ?></td>
                            </tr>
                            <tr>
                                <th>Total Sessions</th>
                                <td><?php echo $ga1->getSessions() ?>
                            </tr>
                            <tr>
                                <th>Total % New Sessions</th>
                                <td><?php echo $ga1->getPercentNewSessions() ?>
                            </tr>
                            <tr>
                                <th>Result Date Range</th>
                                <td><?php echo $ga1->getStartDate() ?> to <?php echo $ga1->getEndDate() ?></td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

1 个答案:

答案 0 :(得分:0)

好吧,我找到了一种方法,可以使用在URL上传递的变量来完成此操作。它有效,但我不确定这是否是最佳方式。

<div class="content">
        <div class="container-fluid"> 
            <p>This is the secure backend for administrators. You are currently logged in. </p>
            <div class="row">
                <div class="col-md-4">
                    <div class="card ">
                        <table class='table table-hover'>
                            <tr>
                                <th>Demographics</th>
                            </tr>
                            <tr>
                                <td><a href="dashboard.php?var=language">Language</a></td>
                            </tr>
                            <tr>
                                <td><a href="dashboard.php?var=country">Country</a></td>
                            </tr>
                            <tr>
                                <td><a href="dashboard.php?var=city">City</a></td>
                            </tr>
                            <tr>
                                <th>System</th>
                            </tr>
                            <tr>
                                <td><a href="dashboard.php?var=browser">Browser</a></td>
                            </tr>
                            <tr>
                                <td><a href="dashboard.php?var=operatingSystem">Operating System</a></td>
                            </tr>
                            <tr>
                                <td><a href="dashboard.php?var=networkLocation">Service Provider</a></td>
                            </tr>
                            <tr>
                                <th>Mobile</th>
                            </tr>
                            <tr>
                                <td><a href="dashboard.php?var=mobileDeviceInfo">Operating System</a></td>
                            </tr>
                            <tr>
                                <td><a href="">Service Provider</a></td>
                            </tr>
                            <tr>
                                <td><a href="">Screen Resolution</a></td>
                            </tr>
                        </table>
                    </div>
                </div>
                <div class="col-md-8">
                    <div class="card ">
                        <table class='table table-hover'>
                            <tr>
                                <th>Browser</th>
                                <th>Sessions</th>
                                <th>% New Sessions</th>
                            </tr>
                            <?php
                            $ga1 = new gapi("service_account@example.com", "key.p12");
                            //$filter = 'country == United States && browser == Firefox || browser == Chrome';
                            $ga1->requestReportData('id',array($passed_var),array('sessions','percentNewSessions'),'-sessions',$filter, '30daysAgo', NULL, 1, 10);

                            foreach($ga1->getResults() as $result):
                            ?>
                            <tr>
                                <td><?php echo $result ?></td>
                                <td><?php echo $result->getSessions() ?></td>
                                <td><?php echo $result->getPercentNewSessions() ?></td>
                            </tr>
                            <?php
                            endforeach
                            ?>
                            </table>

                            <table class='table table-hover'>
                            <tr>
                                <th>Total Results</th>
                                <td><?php echo $ga1->getTotalResults() ?></td>
                            </tr>
                            <tr>
                                <th>Total Sessions</th>
                                <td><?php echo $ga1->getSessions() ?>
                            </tr>
                            <tr>
                                <th>Total % New Sessions</th>
                                <td><?php echo $ga1->getPercentNewSessions() ?>
                            </tr>
                            <tr>
                                <th>Result Date Range</th>
                                <td><?php echo $ga1->getStartDate() ?> to <?php echo $ga1->getEndDate() ?></td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>