16

Google API Ruby client이 최선의 선택입니까?Google 애널리틱스 통계를 가져 오는 방법은 무엇인가요?

사용자가있는 example.com 사이트가 있고 example.com에서 Google Analytics 통계를보고 싶습니다. 어떻게해야합니까?

나는 the example을 볼 수 있지만 시작하는 방법을 알 수는 없습니다.

+1

Legato는 포기한 적이 없으므로 GA API에 대한 유지 보수 가능한 쿼리를 작성하는 더 좋은 방법입니다. 나는 그 요지의 저자에게 그의 잘못된 메모를 바로 잡으라고 요청했습니다. Legato는 GA API 버전 3을 항상 지원합니다. https://github.com/tpitale/legato/commit/0def82f9bdb9cf259d4d91d5bd2f17759231bb29 –

답변

29

또한 google-api-ruby-client 보석을 사용하고 제공 한 링크 (https://gist.github.com/joost/5344705)에 설명 된 것과 거의 같은 방식으로 설정하십시오.

그냥 Google 웹 로그 분석 클라이언트 설정하는 링크에 설명 된 단계를 수행 :

# you need to set this according to your situation/needs 
SERVICE_ACCOUNT_EMAIL_ADDRESS = '...' # looks like [email protected] 
PATH_TO_KEY_FILE    = '...' # the path to the downloaded .p12 key file 
PROFILE      = '...' # your GA profile id, looks like 'ga:12345' 


require 'google/api_client' 

# set up a client instance 
client = Google::APIClient.new 

client.authorization = Signet::OAuth2::Client.new(
    :token_credential_uri => 'https://accounts.google.com/o/oauth2/token', 
    :audience    => 'https://accounts.google.com/o/oauth2/token', 
    :scope    => 'https://www.googleapis.com/auth/analytics.readonly', 
    :issuer    => SERVICE_ACCOUNT_EMAIL_ADDRESS, 
    :signing_key   => Google::APIClient::PKCS12.load_key(PATH_TO_KEY_FILE, 'notasecret') 
).tap { |auth| auth.fetch_access_token! } 

api_method = client.discovered_api('analytics','v3').data.ga.get 


# make queries 
result = client.execute(:api_method => api_method, :parameters => { 
    'ids'  => PROFILE, 
    'start-date' => Date.new(1970,1,1).to_s, 
    'end-date' => Date.today.to_s, 
    'dimensions' => 'ga:pagePath', 
    'metrics' => 'ga:pageviews', 
    'filters' => 'ga:pagePath==/url/to/user' 
}) 

puts result.data.rows.inspect 

앱에서 사용자의 페이지에 대한 통계를 표시하려면를, 당신은 측정을 조정해야필터 매개 변수를 사용할 수 있습니다. 위의 쿼리는 예를 들어 example.com/url/to/user 페이지의 모든 페이지 뷰를 포함하는 결과 개체를 반환합니다.


경고 :이 대답은 오래 전에 쓰여진과 구글이 보석의 새로운 호환되지 않는 버전을 발표했다. 문의하십시오 https://github.com/google/google-api-ruby-client/blob/master/MIGRATING.md

+0

내 날 저장 .. Thanks @severin. make query 섹션에서 PROILE은 PROFILE로 표기해야합니다. 하지만 큰 문제는 아닙니다. – Kumar

+0

Thanks @Kumar. 오타를 지적 했어. 지적 해 주셔서 고마워! – severin

+0

@severin "PROFILE = '...'# 내 GA 프로필 ID는 'ga : 12345'와 (과) 일치합니까?" 여기에서 그 이드를 가져 갔습니까? 은 해당 분석 계정 ID입니다.? 그렇다면 'ga :'를 접두어로 사용해야합니다.? – Aparichith

관련 문제