Create a gist now

Instantly share code, notes, and snippets.

Embed
An example of calling the Analytics API using machine creds and the V4 API from Ruby
require 'google/apis/analyticsreporting_v4'
require 'googleauth'
include Google::Apis::AnalyticsreportingV4
include Google::Auth
VIEW_ID = "12345678" #your profile ID from your Analytics Profile
SCOPE = 'https://www.googleapis.com/auth/analytics.readonly'
@client = AnalyticsReportingService.new
#Using the "Server to Server auth mechanism as documented at
#https://developers.google.com/api-client-library/ruby/auth/service-accounts
@creds = ServiceAccountCredentials.make_creds({:json_key_io => File.open('client_secrets.json'),
:scope => SCOPE})
@client.authorization = @creds
grr = GetReportsRequest.new
rr = ReportRequest.new
rr.view_id = VIEW_ID
#put a filter which only returns results for the root page
rr.filters_expression="ga:pagePath==/"
#We want the number of sessions
metric = Metric.new
metric.expression = "ga:sessions"
rr.metrics = [metric]
#We want this for the last 7 days
range = DateRange.new
range.start_date = "7daysAgo"
range.end_date = "today"
rr.date_ranges = [range]
grr.report_requests = [rr]
response = @client.batch_get_reports(grr)
puts response.inspect
puts response.reports.inspect
@gordonjl

This comment has been minimized.

Show comment
Hide comment
@gordonjl

gordonjl Dec 5, 2016

This gist answered my problems, thanks! Biggest nightmare I was having with this was my request's body was never populated. The kicker for me was that I see you have to use the GetReportsRequest class (?!!!).

Thanks again for this very helpful gist.

gordonjl commented Dec 5, 2016

This gist answered my problems, thanks! Biggest nightmare I was having with this was my request's body was never populated. The kicker for me was that I see you have to use the GetReportsRequest class (?!!!).

Thanks again for this very helpful gist.

@suryatresna

This comment has been minimized.

Show comment
Hide comment
@suryatresna

suryatresna Dec 10, 2016

Thats Greats.. Thanks.. its solve my probs..

Thats Greats.. Thanks.. its solve my probs..

@maxfindel

This comment has been minimized.

Show comment
Hide comment
@maxfindel

maxfindel Mar 10, 2017

Great gist, thank you!
Maybe adding a little help to create the service account and giving it access to Analytics might be useful. This guy does exactly that:
https://gist.github.com/peterclark/df7e68e62cb97385beb46685c66a8606

Great gist, thank you!
Maybe adding a little help to create the service account and giving it access to Analytics might be useful. This guy does exactly that:
https://gist.github.com/peterclark/df7e68e62cb97385beb46685c66a8606

@angelagabereau

This comment has been minimized.

Show comment
Hide comment
@angelagabereau

angelagabereau Jun 14, 2017

Thank you kindly

Thank you kindly

@aldrienht

This comment has been minimized.

Show comment
Hide comment
@aldrienht

aldrienht May 16, 2018

@CoryFoy I'm on the last part, but can't move forward, because i'm getting forbidden: User does not have any Google Analytics account.
any idea what configuration/s i missed?

@CoryFoy I'm on the last part, but can't move forward, because i'm getting forbidden: User does not have any Google Analytics account.
any idea what configuration/s i missed?

@andrzej-zuralovic

This comment has been minimized.

Show comment
Hide comment
@andrzej-zuralovic

andrzej-zuralovic May 18, 2018

@aldrienht You need to add your service email that is in client_secrets.json to Google Analytics what you are using.

@aldrienht You need to add your service email that is in client_secrets.json to Google Analytics what you are using.

@npupatk

This comment has been minimized.

Show comment
Hide comment
@npupatk

npupatk Jun 5, 2018

thanks @CoryFoy !
@aldrienht when you create the service account at https://console.developers.google.com/iam-admin/serviceaccounts you should be given the option to download the JSON file & it should be in the file. In the file, the type should be service_account, the project_id should be whatever project name you provided it like abc123, and the client_email should be like xxxx@abc123.iam.gserviceaccount.com
To satisfy the forbidden error, you need to add that service account email to an account or property or view (at https://analytics.google.com/analytics, -> Admin page/gear icon -> User Management -> plus sign + -> Add new users. Needs at least Read & Analyze permissions).

npupatk commented Jun 5, 2018

thanks @CoryFoy !
@aldrienht when you create the service account at https://console.developers.google.com/iam-admin/serviceaccounts you should be given the option to download the JSON file & it should be in the file. In the file, the type should be service_account, the project_id should be whatever project name you provided it like abc123, and the client_email should be like xxxx@abc123.iam.gserviceaccount.com
To satisfy the forbidden error, you need to add that service account email to an account or property or view (at https://analytics.google.com/analytics, -> Admin page/gear icon -> User Management -> plus sign + -> Add new users. Needs at least Read & Analyze permissions).

@wlopz

This comment has been minimized.

Show comment
Hide comment
@wlopz

wlopz Jun 11, 2018

Quick question. Do you add this file into config folder? Or in the helper or controller folder?

Also, is the Profile ID, as mentioned in the comment section is considered the View ID in view settings?

wlopz commented Jun 11, 2018

Quick question. Do you add this file into config folder? Or in the helper or controller folder?

Also, is the Profile ID, as mentioned in the comment section is considered the View ID in view settings?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment