【问题标题】:Change content of table displaying Google analytics data更改显示 Google 分析数据的表格内容
【发布时间】:2016-04-17 13:36:39
【问题描述】:

所以我正在使用 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>

【问题讨论】:

  • 哦,顺便说一句,我也知道我需要的所有查询字段是什么。只需要知道如何将它们组合在一起。 JavaScript 和 ajax 可能吗?
  • 我也知道我可以做到这一点的一种方法是创建多个仪表板页面,这些页面除了表查询之外都是相同的。然后链接可以链接到这些页面。但是当我对仪表板进行更改时,我将不得不对所有这些页面进行更改。必须有更好的方法。

标签: php ajax google-analytics html-table google-api-js-client


【解决方案1】:

好吧,我找到了一种方法来使用在 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>

【讨论】:

    猜你喜欢
    • 2015-01-05
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多