Skip to content

Instantly share code, notes, and snippets.

@SeanJA
Last active October 21, 2016 07:40
Show Gist options
  • Save SeanJA/4992004 to your computer and use it in GitHub Desktop.
Save SeanJA/4992004 to your computer and use it in GitHub Desktop.
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
@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