Hi guys!

Check out new #gistflow feature – page views counts for each post page. For example recent post about strong parameters by @homakov:

post

Commit for this feature

It uses garb #gem and each hour downloads page views count from #google_analytics.

#garb is pretty good gem for fetching data from google analytics, here is the code example which does all the work:

class GA
  extend Garb::Model

  metrics :pageviews
  dimensions :page_path

  attr_reader :profile

  def initialize
    Garb::Session.login(
      Configuration.google_analytics.login,
      Configuration.google_analytics.password
    )

    @profile = Garb::Management::Profile.all.detect do |profile| 
      profile.web_property_id == Configuration.google_analytics.web_property_id
    end
  end

  def page_views post
    GA.results(
      profile,
      :filters => { :page_path.eql => post.path },
      :start_date => 5.years.ago.to_date,
      :end_date => Date.today
    ).to_a.first.try(:pageviews)
  end
end

Note that settingslogic gem is used so Configuration is basically:

class Configuration < Settingslogic
  source File.expand_path('../application.yml', __FILE__)
  namespace Rails.env
end

Take a look at #gistflow application.yml for example.