Skip to content

Instantly share code, notes, and snippets.

@netconstructor
Forked from SeanJA/README.md
Created June 21, 2013 11:35
Show Gist options
  • Save netconstructor/5830641 to your computer and use it in GitHub Desktop.
Save netconstructor/5830641 to your computer and use it in GitHub Desktop.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment