Skip to content

Instantly share code, notes, and snippets.

@jayanthdeejay
Forked from mjgiarlo/analytics_test.rb
Created January 19, 2020 04:01
Show Gist options
  • Save jayanthdeejay/8e911cde8d7f3725d0555e02bcf5f36d to your computer and use it in GitHub Desktop.
Save jayanthdeejay/8e911cde8d7f3725d0555e02bcf5f36d to your computer and use it in GitHub Desktop.
legato filtering out 0-result dates?
# Inspired by https://gist.github.com/3166610
require 'google/api_client'
require 'date'
API_VERSION = 'v3'
CACHED_API_FILE = "analytics-#{API_VERSION}.cache"
# Update these to match your own apps credentials
service_account_email = 'foobar@developer.gserviceaccount.com' # Email of service account
key_file = '/tmp/foobar-privatekey.p12' # File containing your private key
key_secret = 'foobar' # Password to unlock private key
profileID = '123456789' # Analytics profile ID.
client = Google::APIClient.new(
:application_name => 'foobar',
:application_version => '1.0.0')
# Load our credentials for the service account
key = Google::APIClient::KeyUtils.load_from_pkcs12(key_file, key_secret)
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,
:signing_key => key)
# Request a token for our service account
client.authorization.fetch_access_token!
analytics = client.discovered_api('analytics', API_VERSION)
startDate = DateTime.now.prev_month.strftime("%Y-%m-%d")
endDate = DateTime.now.strftime("%Y-%m-%d")
puts "Grabbing stats from #{startDate} to #{endDate}"
visitCount = client.execute(:api_method => analytics.data.ga.get, :parameters => {
'ids' => "ga:" + profileID,
'start-date' => startDate,
'end-date' => endDate,
'dimensions' => "ga:month,ga:day",
'metrics' => "ga:pageviews",
'filters' => 'ga:pagePath=~/files/9880vr69g',
'sort' => "ga:month,ga:day"
})
print visitCount.data.column_headers.map { |c|
c.name
}.join("\t\t")
puts
visitCount.data.rows.each do |r|
print r.join("\t\t"), "\n"
end
# > ruby analytics_test.rb
Grabbing stats from 2014-03-03 to 2014-04-03
ga:month ga:day ga:pageviews
03 03 3
03 04 1
03 05 3
03 06 2
03 07 6
03 08 1
03 09 0
03 10 1
03 11 0
03 12 1
03 13 1
03 14 0
03 15 0
03 16 2
03 17 1
03 18 5
03 19 3
03 20 1
03 21 0
03 22 2
03 23 2
03 24 2
03 25 0
03 26 1
03 27 1
03 28 2
03 29 0
03 30 0
03 31 1
04 01 1
04 02 0
04 03 2
# Needed for generating token
require 'google/api_client'
require 'oauth2'
# Needed for model
require 'legato'
def service_account_user(scope="https://www.googleapis.com/auth/analytics.readonly")
client = Google::APIClient.new(
:application_name => "foobar",
:application_version => "1.0.0"
)
key = Google::APIClient::PKCS12.load_key('/tmp/foobar-privatekey.p12','foobar')
service_account = Google::APIClient::JWTAsserter.new('foobar@developer.gserviceaccount.com', scope, key)
client.authorization = service_account.authorize
oauth_client = OAuth2::Client.new("", "", {
:authorize_url => 'https://accounts.google.com/o/oauth2/auth',
:token_url => 'https://accounts.google.com/o/oauth2/token'
})
token = OAuth2::AccessToken.new(oauth_client, client.authorization.access_token)
Legato::User.new(token)
end
class Pageview
extend Legato::Model
metrics :pageviews
dimensions :pagePath, :date
filter :for_path, &lambda { |path| contains(:pagePath, path) }
end
start = DateTime.now.prev_month
today = DateTime.now
path = '/files/9880vr69g'
user = service_account_user
profile = user.profiles.detect { |p| p.web_property_id == 'UA-foo-bar' }
puts profile.pageview(start_date: start, end_date: today, sort: 'date').for_path(path).collection
# > ruby legato_test.rb
#<OpenStruct pagePath="/files/9880vr69g", date="20140303", pageviews="3">
#<OpenStruct pagePath="/files/9880vr69g", date="20140304", pageviews="1">
#<OpenStruct pagePath="/files/9880vr69g", date="20140305", pageviews="3">
#<OpenStruct pagePath="/files/9880vr69g", date="20140306", pageviews="2">
#<OpenStruct pagePath="/files/9880vr69g", date="20140307", pageviews="6">
#<OpenStruct pagePath="/files/9880vr69g", date="20140308", pageviews="1">
#<OpenStruct pagePath="/files/9880vr69g", date="20140310", pageviews="1">
#<OpenStruct pagePath="/files/9880vr69g", date="20140312", pageviews="1">
#<OpenStruct pagePath="/files/9880vr69g", date="20140313", pageviews="1">
#<OpenStruct pagePath="/files/9880vr69g", date="20140316", pageviews="2">
#<OpenStruct pagePath="/files/9880vr69g", date="20140317", pageviews="1">
#<OpenStruct pagePath="/files/9880vr69g", date="20140318", pageviews="5">
#<OpenStruct pagePath="/files/9880vr69g", date="20140319", pageviews="3">
#<OpenStruct pagePath="/files/9880vr69g", date="20140320", pageviews="1">
#<OpenStruct pagePath="/files/9880vr69g", date="20140322", pageviews="2">
#<OpenStruct pagePath="/files/9880vr69g", date="20140323", pageviews="2">
#<OpenStruct pagePath="/files/9880vr69g", date="20140324", pageviews="2">
#<OpenStruct pagePath="/files/9880vr69g", date="20140326", pageviews="1">
#<OpenStruct pagePath="/files/9880vr69g", date="20140327", pageviews="1">
#<OpenStruct pagePath="/files/9880vr69g", date="20140328", pageviews="2">
#<OpenStruct pagePath="/files/9880vr69g", date="20140331", pageviews="1">
#<OpenStruct pagePath="/files/9880vr69g", date="20140401", pageviews="1">
#<OpenStruct pagePath="/files/9880vr69g", date="20140403", pageviews="1">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment