stack twitter tryhackme rss linkedin cross

Wilco van Esch

Skip to main content

Search results

    Google Analytics API requests using Ruby on Windows

    The hardest part of making Google API requests via Ruby on Windows is authentication. For this simple example, we'll use an API key rather than an OAuth 2.0 process.

    Retrieve the API key

    These steps assume you have a project folder including a Gemfile, and that you have a browser open and are logged in with the Google account for the Google Analytics you're interested in.

    1. In your Gemfile, add gem 'google-api-client'
    2. Run bundle install from a command prompt
    3. From the API Manager, select Enable API
    4. Select Analytics API
    5. In the top bar, click Project (or Name of project)
    6. From the dropdown, select Create project
    7. Enter a name and click Create
    8. From the IAM & Admin menu, select Service accounts
    9. Click Create service account
    10. Enter a name for the service account
    11. For Role, select Project followed by Viewer (for the purposes of this example, since we're not interested in making changes)
    12. Copy the service account ID to Notepad
    13. Check the Furnish a new private key checkbox
    14. Click Create
    15. Add the downloaded private key to your project folder

    Allow service account to access your Google Analytics

    1. In Google Analytics, select Admin (has cog icon)
    2. Select the account you want to be using
    3. Click User Management
    4. Take the service account ID from Notepad and add it to the Add permission for input field
    5. Click Add

    Get Mozilla's CA Bundle certificate

    1. Navigate to C:\Ruby23-x64\lib\ruby\2.3.0\rubygems\ssl_certs (adjust to fit your Ruby install path)
    2. Download Mozilla's CA Bundle Certificate as ca-bundle.crt into the folder
    3. Assuming openssl is in your PATH, from the command line, in the same folder, run: openssl x509 -in ca-bundle.crt -out MozBundleCA.pem -outform PEM

    Note: If openssl is not in your PATH, ensure openssl is installed and add the installation directory (e.g. C:\openssl\bin) to your PATH environment variable. Make sure you restart your command prompt afterwards, so that the environment variables are reloaded.

    Create a simple example script

    Create a new .rb file and add the following dependency:

    require 'google/apis/analytics_v3'

    In Google Analytics, find the view you want to retrieve data for by navigating through Accounts > Properties & Apps > Views. It will have an 8 digit number. Replace the hash signs below with this number and add it to your script.

    account = 'ga:########'

    Then add the following variables:

    start_date = ((Date.today - 90).strftime('%Y-%m-%d')).to_s
    end_date = (Date.today).to_s
    metric_list = %w(ga:sessions)
    dimension_list = %w(ga:screenResolution)
    filter = 'ga:deviceCategory=~mobile'
    sort = '-ga:sessions'
    results = 10

    This will give us the number of sessions per screen resolution in the last 90 days for mobile devices only, from most used to least used, limited to 10 results once we do the following:

    resolutions = service.get_ga_data(account, start_date,
    end_date, metric_list, dimensions: dimension_list,
    results: 10, filter: filter, sort: sort)

    Now you can do anything you want with the resolutions variable, and build something new using the documentation for the Google API Ruby Client and the Google Analytics API. Note that my example uses v3 and the API has since moved on to v4. However, v3 is still supported.