I am trying to fetch most used site internal search keyword from GA using Core Reporting API V4.
Here's my code.
我正在尝试使用Core Reporting API V4从GA中获取最常用的网站内部搜索关键字。这是我的代码。
require "google/apis/analyticsreporting_v4"
class GoogleAPI
def auth
Google::Auth::ServiceAccountCredentials.make_creds(
json_key_io: File.open(ENV['GOOGLE_AUTH_KEY_FILE']),
scope: 'https://www.googleapis.com/auth/analytics.readonly'
)
end
def popular_keywords
service = Google::Apis::AnalyticsreportingV4::AnalyticsReportingService.new
service.authorization = auth
query = {
view_id: ENV['POPULAR_KEYWORDS_GA_VIEW'],
dimensions: [{name: 'ga:searchKeyword'}],
metrics: [{expression: 'ga:searchUniques'}],
page_size: 1000,
orderBys: [{fieldName: 'ga:searchUniques', orderType: 'VALUE', sortOrder: 'DESCENDING'}],
date_ranges: [{start_date: '30daysAgo', end_date: 'today'}]
}
request = Google::Apis::AnalyticsreportingV4::GetReportsRequest.new
request.report_requests = [query]
reports = service.batch_get_reports request
p reports.to_h
[]
rescue => e
[]
end
end
All works as expected, it fetches keywords as dimensions and used numbers as metrics.
But orderBys not applied. I am expecting to order by ga:searchUniques
at descending order.
It seems it's being ordered by ga:searchKeyword
(dimension) at ascending order.
Anyone please help me.
Thanks.
所有工作都按预期工作,它将关键字作为维度提取,并将数字用作指标。但是orderBys没有应用。我期待按ga:searchUniques按降序排序。它似乎是由ga:searchKeyword(维度)按升序排序的。有人请帮助我。谢谢。
1 个解决方案
#1
0
OK, I got an answer.
The line orderBys: [{fieldName: 'ga:searchUniques', orderType: 'VALUE', sortOrder: 'DESCENDING'}]
should be replaced with order_bys: [{field_name: 'ga:searchUniques', sort_order: 'DESCENDING'}]
好的,我得到了答案。行orderBys:[{fieldName:'ga:searchUniques',orderType:'VALUE',sortOrder:'DESCENDING'}]应替换为order_bys:[{field_name:'ga:searchUniques',sort_order:'DESCENDING'}]
#1
0
OK, I got an answer.
The line orderBys: [{fieldName: 'ga:searchUniques', orderType: 'VALUE', sortOrder: 'DESCENDING'}]
should be replaced with order_bys: [{field_name: 'ga:searchUniques', sort_order: 'DESCENDING'}]
好的,我得到了答案。行orderBys:[{fieldName:'ga:searchUniques',orderType:'VALUE',sortOrder:'DESCENDING'}]应替换为order_bys:[{field_name:'ga:searchUniques',sort_order:'DESCENDING'}]