Skip to content

Instantly share code, notes, and snippets.

@brennovich
Created February 15, 2016 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brennovich/e1a698edfa15b16709dc to your computer and use it in GitHub Desktop.
Save brennovich/e1a698edfa15b16709dc to your computer and use it in GitHub Desktop.
Almost external dependencies free GA API consuming
require 'date'
require 'json'
require 'net/http'
# non-stdlib dependencies
require 'signet/oauth_2/client'
# First we need to fetch our access token
pkc12_key = File.read('./legato-development-8b36edf27344.p12')
client_authorization = Signet::OAuth2::Client.new(
token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
audience: 'https://accounts.google.com/o/oauth2/token',
issuer: 'lm-reports@legato-development.iam.gserviceaccount.com',
scope: 'https://www.googleapis.com/auth/analytics.readonly',
signing_key: OpenSSL::PKCS12.new(pkc12_key, 'notasecret').key
)
access_token = client_authorization.fetch_access_token!['access_token']
# Then we need to grab our profile_id
uri = URI('https://www.googleapis.com/analytics/v3/management/accountSummaries')
uri.query = URI.encode_www_form(access_token: access_token)
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
request = Net::HTTP::Get.new(uri)
http.request(request)
end
properties = JSON.parse(response.body)['items'][0]['webProperties']
profiles = properties.find { |i| i['id'] == 'UA-42245406-1' }['profiles']
profile_id = profiles.find { |i| i['name'] == 'Todos os dados do website' }['id']
# And finally fetch GA data
uri = URI('https://www.googleapis.com/analytics/v3/data/ga')
uri.query = URI.encode_www_form(
ids: "ga:#{profile_id}",
access_token: access_token,
'start-date' => (Date.today - 30).to_s,
'end-date' => :yesterday,
metrics: 'ga:pageviews'
)
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
request = Net::HTTP::Get.new(uri)
http.request(request)
end
JSON.parse(response.body)
# {
# "kind":"analytics#gaData",
# "id":"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:74228006&metrics=ga:pageviews&start-date=2016-01-16&end-date=yesterday",
# "query":{
# "start-date":"2016-01-16",
# "end-date":"yesterday",
# "ids":"ga:74228006",
# "metrics":[
# "ga:pageviews"
# ],
# "start-index":1,
# "max-results":1000
# },
# "itemsPerPage":1000,
# "totalResults":1,
# "selfLink":"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:74228006&metrics=ga:pageviews&start-date=2016-01-16&end-date=yesterday",
# "profileInfo":{
# "profileId":"74228006",
# "accountId":"42245406",
# "webPropertyId":"UA-42245406-1",
# "internalWebPropertyId":"71927960",
# "profileName":"Todos os dados do website",
# "tableId":"ga:74228006"
# },
# "containsSampledData":false,
# "columnHeaders":[
# {
# "name":"ga:pageviews",
# "columnType":"METRIC",
# "dataType":"INTEGER"
# }
# ],
# "totalsForAllResults":{
# "ga:pageviews":"3404548"
# },
# "rows":[
# [
# "3404548"
# ]
# ]
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment