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.
- In your Gemfile, add
bundle installfrom a command prompt
- From the API Manager, select Enable API
- Select Analytics API
- In the top bar, click Project (or Name of project)
- From the dropdown, select Create project
- Enter a name and click Create
- From the IAM & Admin menu, select Service accounts
- Click Create service account
- Enter a name for the service account
- For Role, select Project followed by Viewer (for the purposes of this example, since we're not interested in making changes)
- Copy the service account ID to Notepad
- Check the Furnish a new private key checkbox
- Click Create
- Add the downloaded private key to your project folder
Allow service account to access your Google Analytics
- In Google Analytics, select Admin (has cog icon)
- Select the account you want to be using
- Click User Management
- Take the service account ID from Notepad and add it to the Add permission for input field
- Click Add
Get Mozilla's CA Bundle certificate
- Navigate to
C:\Ruby23-x64\lib\ruby\2.3.0\rubygems\ssl_certs(adjust to fit your Ruby install path)
- Download Mozilla's CA Bundle Certificate as ca-bundle.crt into the folder
opensslis in your
PATH, from the command line, in the same folder, run:
openssl x509 -in ca-bundle.crt -out MozBundleCA.pem -outform PEM
openssl is not in your
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:
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.