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.

Revisions

  1. SeanJA revised this gist Jun 18, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ Set your google username

    `username = ''`

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

    `password = ''`

  2. SeanJA revised this gist Feb 20, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,8 @@ You will need Sija's version of the garb gem, so add this line to your `Gemfile`

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

    Then run the gem bundler: `bundle install`

    Set your google username

    `username = ''`
  3. SeanJA revised this gist Feb 20, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ Put in your google password (you might want to create a secondary account for th

    `password = ''`

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

    `api_key = ''`

  4. SeanJA revised this gist Feb 20, 2013. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -2,16 +2,23 @@ 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'`

    Set your google username

    `username = ''`

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

    `password = ''`

    Get an api key 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.
  5. SeanJA created this gist Feb 20, 2013.
    19 changes: 19 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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'`

    Set your google username
    `username = ''`
    Put in your google password (you might want to create a secondary account for this with a really long randomly generated password)
    `password = ''`
    Get an api key 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.
    7 changes: 7 additions & 0 deletions google_pageviews.erb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    <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>
    34 changes: 34 additions & 0 deletions google_pageviews.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    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