Skip to content

Instantly share code, notes, and snippets.

@SeanJA
Last active October 21, 2016 07:40
Google Pageviews

Set Up

You will need Sija's version of the garb gem, so add this line to your Gemfile (you will need git installed for this):

gem 'garb', :git => 'git://github.com/Sija/garb.git'

Then run the gem bundler: bundle install

Set your google username

username = ''

Put in your google password (create a secondary account for this with a really long randomly generated password)

password = ''

Get an api key for google analytics from here: https://code.google.com/apis/console and set the api_key:

api_key = ''

pick your property from the google analytics interface ( http://google.com/analytics ) and set it:

ua = ''

Then add the contents of google_pageviews.erb to your layout file.

If you change the dimensions from :month make sure you update the data-moreinfo line so that it matches the interval.

<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
<div data-id="page_views"
data-view="Number"
data-title="Views"
data-moreinfo="This Month" style="background-color:#869;"></div>
<i class="icon-link icon-background"></i>
</li>
require 'garb'
# google username
username = ''
# google password
password = ''
# google api key
api_key = ''
# google analytics ua (make sure the UA- is there)
ua = 'UA-'
Garb::Session.api_key = api_key
Garb::Session.login(username, password)
class PageViews
extend Garb::Model
metrics :pageviews
# this could be :day, :month, :year
dimensions :month
end
profile = Garb::Management::Profile.all.detect {|p| p.web_property_id == ua}
SCHEDULER.every '1m', :first_in => 0 do
views = PageViews.results(
profile,
:limit => 1,
:start_date => Date.new(Time.now.year,Time.now.month,1).to_time
)
send_event('page_views', { current: views.first.pageviews })
end
@davefp
Copy link

davefp commented Feb 20, 2013

Putting passwords in code like this is super bad news. I'd recommend the OAuth workflow described here: https://github.com/vigetlabs/garb/wiki/OAuth-Session

@gregology
Copy link

I agree @davefp but the OAuth sessions with Google are overly complicated

@crcastle
Copy link

Another option: replace hard-coded password with an environment variable. e.g.

# google password
password = ENV['GA_PASSWORD']

Still not great because a user's password has to be stored somewhere, but not as bad if you create a "service account" not tied to a human.

@SeanJA
Copy link
Author

SeanJA commented Jun 18, 2013

Yep, I agree with @davefp... putting passwords in your code is bad. This is why the instructions are to create a secondary account for this specific purpose that has access to view your stats.

@ethier
Copy link

ethier commented Jul 18, 2013

If anyone is interested in doing this with OAuth, check out the Google Analytics Website Visitor Count Widget by @mtowers. In the client.execute call in the scheduler, you just have to replace 'metrics' => 'ga:visitors', with 'metrics' => 'ga:pageviews',.

@thisistony
Copy link

Another option is using Google Analytics' Superproxy. https://developers.google.com/analytics/solutions/google-analytics-super-proxy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment