Hisui Build Status

Google Analytics Reporting API v4 Client on Rails application

Hisui referred to Legato which is Google Analytics Reporting API Client for Ruby.

Legato uses Core Reporting API V3. Hisui uses Reporting API v4.

But, usage is similar to Legato.

Installation

Add this line to your application's Gemfile:

gem 'hisui'

And then execute:

$ bundle install

Usage

Google Analytics Management API Version 3.0

Usage is the same as Legato.

Google Analytics Reporting API v4

  1. Get profile.

    user = Hisui::User.new(access_token)
    profile = user.profiles.first # Select profile that you want to get Google Analytics data.
    
  2. Create class extended Hisui::Model. And Set metrics and dimensions.

    class DailySummary
    extend Hisui::Model
    

metrics :pageviews, :sessions, :users, :new_users, :bounce_rate, :pageviews_per_session, :avg_session_duration dimensions :date end


3. Get Google Analytics API response with `results` methods.
Set `start_date` and `end_date` if you need.(defult period is past one month)
Set `comparing_start_date` and `comparing_end_date` if you need.
```ruby
response = DailySummary.results(profile: profile, start_date: Date.current - 7.days, end_date: Date.current, comparing_start_date: Date.current - 7.days - 1.month, comparing_end_date: Date.current - 1.month)
  1. Use data. ```ruby # Deprecate response.raw_attributes

[DEPRECATION] raw_attributes is deprecated. Please use primary instead.

=> [#,

...

#]

response.primary

=> [#,

...

#]

response.comparing

=> [#,

...

#]

Deprecate

response.total_values

[DEPRECATION] total_values is deprecated. Please use primary_total instead.

=> #

response.primary_total

=> #

response.comparing_total

=> #

response.data?

=> true


#### Filtering
When you set `filters_expression` method with `field_name`, `operator` and `value` params, you can filter Google Analytics Data.

For example, this is filtered by organic.
```ruby
class OrganicMedium
  extend Hisui::Model

  metrics :pageviews, :sessions, :users, :new_users, :bounce_rate, :pageviews_per_session, :avg_session_duration
  dimensions :medium
  filters_expression({ field_name: 'medium', operator: '==', value: 'organic' })
end

other operator s are here

Contributing

Fork it, fix me, and send me your pull requests.

License

The gem is available as open source under the terms of the MIT License.