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
- Get profile.
user = Hisui::User.new(access_token)
profile = user.profiles.first # Select profile that you want to get Google Analytics data.
- Create class extended
Hisui::Model
. And Setmetrics
anddimensions
.
class DailySummary
extend Hisui::Model
metrics :pageviews, :sessions, :users, :new_users, :bounce_rate, :pageviews_per_session, :avg_session_duration
dimensions :date
end
- Get Google Analytics API response with
results
methods. Setstart_date
andend_date
if you need.(defult period is past one month) Setcomparing_start_date
andcomparing_end_date
if you need.
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)
- Use data.
# Deprecate
response.raw_attributes
# [DEPRECATION] `raw_attributes` is deprecated. Please use `primary` instead.
# => [#<struct date="20171122", pageviews="137", sessions="73", users="51", newUsers="43", bounceRate="69.56521739130434", pageviewsPerSession="2.608695652173913", avgSessionDuration="87.69565217391305">,
# ...
# #<struct date="20171129", pageviews="95", sessions="67", users="44", newUsers="32", bounceRate="80.0", pageviewsPerSession="2.25", avgSessionDuration="42.0">]
response.primary
# => [#<struct date="20171122", pageviews="137", sessions="73", users="51", newUsers="43", bounceRate="69.56521739130434", pageviewsPerSession="2.608695652173913", avgSessionDuration="87.69565217391305">,
# ...
# #<struct date="20171129", pageviews="95", sessions="67", users="44", newUsers="32", bounceRate="80.0", pageviewsPerSession="2.25", avgSessionDuration="42.0">]
response.comparing
# => [#<struct date="20171022", pageviews="130", sessions="69", users="45", newUsers="40", bounceRate="70.09567898751234", pageviewsPerSession="1.884057975234981", avgSessionDuration="85.02349863284283">,
# ...
# #<struct date="20171029", pageviews="95", sessions="67", users="44", newUsers="32", bounceRate="80.0", pageviewsPerSession="2.25", avgSessionDuration="42.0">]
# Deprecate
response.total_values
# [DEPRECATION] `total_values` is deprecated. Please use `primary_total` instead.
#=> #<struct pageviews="646", sessions="308", users="223", newUsers="144", bounceRate="77.77777777777779", pageviewsPerSession="2.3518518518518519", avgSessionDuration="62.148148148148145">
response.primary_total
# => #<struct pageviews="646", sessions="308", users="223", newUsers="144", bounceRate="77.77777777777779", pageviewsPerSession="2.3518518518518519", avgSessionDuration="62.148148148148145">
response.comparing_total
# => #<struct pageviews="640", sessions="299", users="213", newUsers="138", bounceRate="75.97253924292489", pageviewsPerSession="2.1404682398523578", avgSessionDuration="60.814845134904329">
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.
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.