Skip to content

Instantly share code, notes, and snippets.

@devdave
Created July 23, 2012 22:21
Show Gist options
  • Save devdave/3166610 to your computer and use it in GitHub Desktop.
Save devdave/3166610 to your computer and use it in GitHub Desktop.
Working SA auth for Google analytics
require 'pry'
require 'rubygems'
require 'google/api_client'
passPhrase = "notasecret"
keyFile = "YOUR KEY HERE-privatekey.p12"
cID = "YOUR ID HERE.apps.googleusercontent.com"
scope = 'https://www.googleapis.com/auth/analytics.readonly'
profileID = "YOUR PROFILE ID"
email = '[email protected]'
key = Google::APIClient::PKCS12.load_key(keyFile, passPhrase)
asserter = Google::APIClient::JWTAsserter.new(
email,
scope,
key)
#Do stuff
client = Google::APIClient.new()
client.authorization = asserter.authorize()
analytics = client.discovered_api("analytics",'v3')
#Grab the first profile
#result = client.execute(analytics.management.accounts.list)
#print result.data.items[0]
startDate = "2012-06-01"
endDate = "2012-07-12"
common = { :ids=>"ga:" + profileID, "start-date" => startDate, "end-date" => endDate }
visitCount = client.execute(analytics.to_h["analytics.data.ga.get"], { :dimensions => "ga:day,ga:month", :metrics => "ga:visits", :sort => "ga:month,ga:day"}.update(common) )
#print visitCount.data.column_headers.map { |c|
# c.name
#}.join("\t")
#visitCount.data.rows.each do |r|
# print r.join("\t"), "\n"
#end
binding.pry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment