Hi,
I'm currently trying out the new AnalyticsreportingV4 api in ruby, but I can't get it to work. There is in fact no documentation, except the code itself (https://github.com/google/google-api-ruby-client/tree/master/generated/google/apis/analyticsreporting_v4) and this guide, where I tried to port the Java, Python and PHP examples to Ruby https://developers.google.com/analytics/devguides/reporting/core/v4/
Here is my code so far, I managed to get the auth stuff to work with a Service Account (server to server communication). But the report itself returns nil every time.
require 'google/apis/analyticsreporting_v4'
json_path = '/google_analytics_key.json'
authorization = Google::Auth::ServiceAccountCredentials.make_creds(
json_key_io: File.open(json_path),
scope: [Google::Apis::AnalyticsreportingV4::AUTH_ANALYTICS_READONLY]
)
client = Google::Apis::AnalyticsreportingV4::AnalyticsReportingService.new
client.authorization = authorization
date_range = Google::Apis::AnalyticsreportingV4::DateRange.new(start_date: '7DaysAgo', end_date: 'today')
metric = Google::Apis::AnalyticsreportingV4::Metric.new(expression: 'ga:sessions', alias: 'sessions')
dimension = Google::Apis::AnalyticsreportingV4::Dimension.new(name: 'ga:browser')
request = Google::Apis::AnalyticsreportingV4::ReportRequest.new(view_id: 'UA-XXXXXXXX-1', metrics: [metric], dimensions: [dimension], date_ranges: [date_range])
response = client.batch_get_reports(request)
response.reports # => nil
Will there be a better documentation for this soon? Or am I doing something wrong?
Thanks in advance!
hey, I was answering your question on stackoverflow when you deleted it 馃榿
My answer show a way to query GA without this gem (I know that this is not the best answer to post here, but can help you if you need to make this work fast). The idea is to do a normal request to the GA endpoiny, here I'm using Faraday, but you can use any for http requests:
module GABatchGet
extend self
def auth
google_api_scopes = ['https://www.googleapis.com/auth/analytics']
Google::Auth.get_application_default(google_api_scopes)
end
def request(args = {})
connection = Faraday::Connection.new('https://analyticsreporting.googleapis.com') do |faraday|
faraday.response :json, content_type: /\bjson$/
end
connection.post('v4/reports:batchGet') do |request|
request.headers = auth.apply(request.headers.merge('Content-Type' => 'application/json'))
request.body = {
reportRequests: [{
dateRanges: [{
startDate: Date.parse('2016/10/01'),
endDate: Date.parse('2016/10/26')
}],
viewId: 'XXXXXX',
metrics: [{ expression: "ga:sessions" }],
dimensions: [{ name: "ga:Campaign" }],
dimensionFilterClauses: [{
operator: 'AND',
filters: [
{ dimensionName: 'ga:Campaign',
not: true, operator: 'IN_LIST',
expressions: ['(not set)', '(not provided)'] }
]
}]
}]
}.to_json
end
end
end
Thanks for your answer! (I thought it suits better here in the bug tracker than on Stackoverflow, therefore I deleted it on Stackoverflow)
I like the approach, it seems more straightforward than wrapping every attribute in a class. But can you show an example, where you use a service account key (that json file, without OAuth2 and user interaction, I only fetch data directly from my server).
thx!
My code do it via json file in a server too, I use this gem to authenticate with Google:
https://github.com/google/google-auth-library-ruby
them this line works fine: Google::Auth.get_application_default(google_api_scopes)
thanks, that worked! I'm not sure if I should close the issue, because the AnalyticsReporting V4 class still misses documentation
I had the same issue and that gist helped
There's a v3 sample at https://github.com/google/google-api-ruby-client/blob/master/samples/cli/lib/samples/analytics.rb, but nothing for v4 yet.
Generally it's up to the individual API teams (in this case, analytics) to create samples for their APIs. Community contributions are also welcome.
Thanks for your support! I also created an issue here https://github.com/google/google-auth-library-ruby/issues/91 about how to use a token store with service account credentials. Unfortunately, nobody has answered so far, and I didn't find a solution elsewhere.
So, maybe someone of you already have used it, and can help me. thx!
@23tux the problem in your original code is you're using ReportRequest instead of GetReportsRequest
request = Google::Apis::AnalyticsreportingV4::GetReportsRequest.new(
report_requests: [Google::Apis::AnalyticsreportingV4::ReportRequest.new(
view_id: '12345'
#metrics: [metric],
#dimensions: [dimension],
#date_ranges: [date_range]
)]
)
Also you had view_id wrong, those are usually just 5 or 6 digit numbers for that exact view, not your U-xxx account ID
@9mm thanks for your correction! I've been hunting around for ages and finally came across this. Hoping to contribute a bit more to the documentation dearth on this when I have a moment.
Most helpful comment
@23tux the problem in your original code is you're using
ReportRequestinstead ofGetReportsRequesthttps://github.com/google/google-api-ruby-client/blob/da6c828d6fe37b4ded79e56259874a0ad930deb8/generated/google/apis/analyticsreporting_v4/service.rb#L50
Also you had
view_idwrong, those are usually just 5 or 6 digit numbers for that exact view, not your U-xxx account ID