Google analytics API pagination current page numbe

2019-09-12 09:58发布

问题:

I am developing a analytics plugin for wordpress, I have got my required data but It is too long, so I want to use pagination here.

if I will try to get into next link( using getNextLink() ) on the below code it will show me an error that, this is unauthenticated.

How I can authenticate it and show the next resultset on the next page . I am displaying the result on plugin setting page.

And another important thing is "Is there any function or way to get the current page number ?"

I am adding the screenshot, If any other details required to guide me on this , please let me know.

You can see there is "1 to 30", I have written that one, but for the next page, it need to be changed, I have no idea how I can get the current page number .

The function that will return this pagination is :-

public  function getPaginationInfo(&$results) {

print '<div class="tablenav-pages"><span class="displaying-num">' . $results->getTotalResults() . ' items</span>
<span class="pagination-links"><span class="tablenav-pages-navspan" aria-hidden="true">«</span>
<a class="prev-page" href="' . $results->getPreviousLink() . '"><span class="screen-reader-text">Previous page</span><span aria-hidden="true">‹</span></a>
<span class="screen-reader-text">Current Page</span><span id="table-paging" class="paging-input">' . 1 . ' of <span class="total-pages">' . ceil($results->getTotalResults()/10) . '</span></span>
<a class="next-page" href="' . $results->getNextLink() . '"><span class="screen-reader-text">Next page</span><span aria-hidden="true">›</span></a>
<span class="tablenav-pages-navspan" aria-hidden="true">»</span></span></div>';

    }

回答1:

Just add "start-index" to get the starting page

(if your max-result is 30 then page 1 start-index is 1, page 2 start-index is 31)

and "max-results" number of total row to display

reference: https://developers.google.com/analytics/devguides/reporting/core/v3/reference#startIndex



回答2:

I added accessToken to the next and previous link and used AJAX for pagination.

$results->getNextLink() . '&access_token='. $accessToken this is the key to achieve this.

$accessToken = json_decode($_SESSION['access_token'])->access_token;
print '<div class="tablenav-pages"><span class="displaying-num">' . $results->getTotalResults() . ' items</span>
<span class="pagination-links"><span class="tablenav-pages-navspan" aria-hidden="true">«</span>
<a class="prev-page" href="' . $results->getPreviousLink() . '&access_token='. $accessToken . '"><span class="screen-reader-text">Previous page</span><span class="tablenav-pages-navspan" aria-hidden="true">‹</span></a>
<span class="screen-reader-text">Current Page</span><span id="table-paging" class="paging-input">' . 1 . ' of <span class="total-pages">' . ceil($results->getTotalResults()/10) . '</span></span>
<a class="next-page" href="' . $results->getNextLink() . '&access_token='. $accessToken . '"><span class="screen-reader-text">Next page</span><span class="tablenav-pages-navspan" aria-hidden="true">›</span></a>
<span class="tablenav-pages-navspan" aria-hidden="true">»</span></span></div>';

and ajax part roughly

jQuery('document').ready(function(){
    jQuery('.next-page').click(
        function( event ){
            event.preventDefault();

            url = jQuery(this).attr('href');
            jQuery.ajax({
                url: url,
                context: document.body
            }).done(function( data ){
                console.log( data );
            });

        });
});