Google Analytics API - Pagination

I download some data from Google Analytics using the Google Api Client for Ruby (my Gemfile.lock is its google-api client (0.6.4)). I get data from google, but it is so much that it comes (or at least should) on several pages (more than 1000 lines).

I tried using an example from google (part of my code below)

    request = {
      :api_method => analytics.data.ga.get, 
      :parameters => {
        'ids' => "ga:" + ids, 
        'start-date' => start_date, 
        'end-date' => end_date, 
        'dimensions' => dimensions, 
        'metrics' => metrics,
        'max-results' => 10 #only for testing
      }
    }

    loop do
      result = api.execute(request)
      results << result

      break unless result.next_page_token
      request = result.next_page
    end

Well ... that doesn't work.

result.next_page_token #returns always nil

I am using the Google Analytics API (v3)

+4
source share
1 answer

I also experienced the same thing so that it works with the following code

     loop do
        result = api.execute(request)
        results << result
        next_page_uri = result.data.next_link
        break unless next_page_uri
        next_page = result.next_page
        next_page.uri = next_page_uri
        request = next_page
      end

Hope this helps those facing the same problem.

+2

Source: https://habr.com/ru/post/1524665/


All Articles