Skip to content

Instantly share code, notes, and snippets.

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 abhinaykumar/95a460efd034fd7574c34a02b02475a1 to your computer and use it in GitHub Desktop.
Save abhinaykumar/95a460efd034fd7574c34a02b02475a1 to your computer and use it in GitHub Desktop.
require 'oauth2'
require 'legato'

client = OAuth2::Client.new('client-ID', 'client-secret', {
  :authorize_url => 'https://accounts.google.com/o/oauth2/auth',
  :token_url => 'https://accounts.google.com/o/oauth2/token'
})

client.auth_code.authorize_url({
  :scope => 'https://www.googleapis.com/auth/analytics.readonly',
  # Below line(redirect_uri) should be the same as you have mentioned in the app's credentials settings. 
  # Since I am using omniauth-google_oauth2 I have used the same
  :redirect_uri => 'http://localhost:3000/users/auth/google_oauth2/callback',
  :access_type => 'offline'
})

# Toekn returned from omniauth-google_oauth2 hash, this token will work only for short period of time(1 hour) so
# do update the token everytime a user logs in or ask them to re-login with google may be.
token = "xxxxxxxxxxxxxxxxxxx"

# Get access_token
access_token = OAuth2::AccessToken.from_hash client, {:access_token => token}

#######################################################################
#Create a Model
####
class Exit
  extend Legato::Model

  metrics :exits, :pageviews
  dimensions :browser
end
#####################################
user = Legato::User.new(access_token) # access_token from step #4

profile = user.profiles.first

Exit.results(profile).each {|result| p result}

profile.exit.each {|result| p result}

####################################### From Legato page:

The results method accepts a number of options:

start_date
end_date
limit
offset
sort, for reverse sort, pass a string value with a - as the first character e.g., '-pageviews'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment