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

时间:2022-03-26 14:41:08

So I am using the GAPI api which is a PHP api for Google Analytics. I have the analytics data pulling correctly, and can change the table manually to reflect whatever data I want to see. I wish to have a list of different analytics on the left, and when clicked it will populate the table using new criteria. I am assuming I can use AJAX to do this, but would like more info on how to get started. I am familiar with ajax but do not even know how to begin here.

所以我使用的是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 个解决方案

#1


0  

Well, I found one way to do this using variables that are passed on the URL. It works, however I am not sure if this is the best way.

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

#1


0  

Well, I found one way to do this using variables that are passed on the URL. It works, however I am not sure if this is the best way.

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