Project

hisui

0.0
No commit activity in last 3 years
No release in over 3 years
Access the Google Analytics Reporting API v4 On Rails
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

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.
  1. 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
  1. 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.
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.
# 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.