- Japanese Document (my blog's article) is following
This gem helps you to obtain Google Analytics
reporting data with ease.
First of all, you create configuration YAML file as Google Analytics API. Then execute the simple Ruby code, and you obtain Google Analytics reporting data.
Yeah, very simple :)
This gem is a wrapper of Legato gem. Legato
is very coooooooool and full-stack gem, so complicated as a beginner (me!).
Therefore I developed this wrapper gem, simple_ga_reporting
.
$ gem install simple_ga_reporting
- There are two ways to set
private_key
andclient_email
- How to get above two Key-Value sets is written later
- Create
./config/key_and_email.yml
- This YAML file must contain two Key-Value sets
private_key
and its valueclient_email
and its value
- Set arguments of
SimpleGaReports.filtered_results
method - For example:
SimpleGaReports.filtered_results(private_key: 'FOO', client_email: 'BAR')
- Convenient when you set by environment variables
- You must create the YAML file which contains Google Analytics reporting configuration such as
start-date
,end-date
,metrics
,demensions
,filters
and etc. - filepath is
./config/ga_reporting_config.yml
- This YAML file format is shown later
- If you wanna use filters function, please create
./config/filters.rb
- the filters configuration on GUI is below
- Write your application used this gem
- The sample code is below
- Use only two class methods
SimpleGaReports.configure
SimpleGaReports.filtered_results
- Only three lines :-)
- Note
- If you don't use filters, use
SimpleGaReports.raw_results
too - when no filter is in configure file,
SimpleGaReports.raw_results
is the same asSimpleGaReports.filtered_results
- If you don't use filters, use
- Use only two class methods
require 'simple_ga_reporting'
SimpleGaReports.configure
SimpleGaReports.filtered_results #=> Legato::Query
- You must set Google Analytics API configuration,
private_key
andclient_email
- There are two ways to set it
- The one is by arguments
- Set arguments of
SimpleGaReports.filtered_results
method
- Set arguments of
- The other is by YAML file
- Default filepath is
./config/key_and_email.yml
- You can change filepath by argument
- Default filepath is
- The one is by arguments
- The sample of YAML file is below
private_key: "-----BEGIN PRIVATE KEY-----\nMIIE..........Eqw==\n-----END PRIVATE KEY-----\n"
client_email: "[email protected]"
- You must create the YAML file which contains Google Analytics reporting configuration
- Default filepath is
./config/ga_reporting_config.yml
- You can change filepath by argument in
SimpleGaReports.configure
method
- You can change filepath by argument in
- The sample of YAML file is below
profile_name: my_profile_name
start_date: 2018-04-01
end_date: 2018-04-05
metrics:
- users
- pageviews
dimensions:
- pagePath
- pageTitle
sort:
- -pageviews
- -users
filters:
- my_awesome_filter
- his_awesome_filter
- her_awesome_filter
limit: 20
- Please refer the official documents as Key-Value sets
- Though above official documents show many Key-Value sets, you can config only several sets as below
- start-date
- end-date
- metrics
- dimensions
- sort
- filters
- max-results
- samplingLevel
- Some Key names in configure file are different from ones in official document
start_date
in configure file (notstart-date
)end_date
in configure file (notend-date
)limit
in configure file (instead ofmax-results
)sampling_level
in configure file (notsamplingLevel
)
- The type of
metrics
,dimensions
,sort
andfilters
isArray
- The order of elements doesn't matter
- REQUIRED
- It was called
view (view name)
before- but in Japanese page, still
ビュー名
- but in Japanese page, still
- REQUIRED
- Original Key name is
start-date
- Value examples
2018-04-01
today
oryesterday
14daysAgo
- use
daysAgo
phrase even if1day
- use
- REQUIRED
- Original Key name is
end-date
- Value examples are the same as
start-date
- REQUIRED
指標
in Japanese- Official documents are below (very useful!)
- Value examples
users
uniquePageviews
- optional
- Official documents are below (very useful!)
- Value examples
pagePath
sessionCount
- optional
- prefix
-
sign means descending- no sign means ascending
- Don't use
+
sign
- Don't use
- Value examples
-pageviews
users
- optional
- Value is method name you named in
filters.rb
- optional
- Original Key name is
max-results
- Value is max amount of returned rows
- Default value is
100
- optional
- Original Key name is
samplingLevel
- Default value is
HIGHER_PRECISION
- You use filters function using
filters.rb
file- the filename
filters.rb
is determined- only filepath is optional
- the module name
Filters
is determined - Default filepath is
./config/filters.rb
- You can change it by argument with
configure
method
- You can change it by argument with
- the filename
- Examples
module Filters
def chrome_or_fx
filter :chrome_or_fx, &lambda { contains(:browser, 'Chrome|Firefox') }
end
def awesome_page
filter :awesome_page, &lambda { contains(:pagePath, '\A.*awesome.*\z') }
end
end
chrome_or_fx
method filters the results- pick up data which contains 'Google Chrome' or 'Firefox' as browser
- the convention of filter method is below
- define method name
- define filter name by symbol which is the same as method name
- define filter behavior by block object
- Usage of multiple filters
- If you want to apply AND-chain, only specify each filter in Google Analytics reporting configuration file
- If you want to apply OR-chain, use regexp in block object as
chrome_or_fx
method
- Adding refer Legato's Documents
YOU MUST SPECIFY ARGUMENTS AS RELATIVE FILEPATH SO YOU MUST ADD './' (dot slash) AT BEGINNING
- Set arguments of
SimpleGaReports.filtered_results
method - For example:
- `SimpleGaReports.filtered_results(private_key: 'FOO', client_email: 'BAR')
- Convenient when you set by environment variables
- If you don't use filters, use
SimpleGaReports.raw_results
method instead ofSimpleGaReports.filtered_results
method- when no filter is in configure file,
SimpleGaReports.raw_results
is the same asSimpleGaReports.filtered_results
- when no filter is in configure file,
- take three arguments
- All arguments are keyword ones
- You must set (
key_and_email_file
) or (private_key
andclient_email
)
-
key_and_email_file
- default is
./config/key_and_email.yml
- default is
-
private_key
- default is
nil
- default is
-
client_email
- default is
nil
- default is
- take three arguments
- All arguments are keyword ones
-
report_config
- first argument
- YAML file
- default is
./config/ga_reporting_config.yml
-
filters
- second argument
- filter file
- default is
./config/filters.rb
-
**options
- third argument
- default is nil
- if you set this argument, you can overwrite values in Google Analytics reporting configuration file
- example
start_date: '90daysAgo', end_date: '60daysAgo', limit: 100
1. Access to Google Cloud Platform Console
- name new project as you like
- Select
Service Account Key
- Deal with them carefully
- Deal with them carefully
- NOT
Google Analytics Reporting API
BUTAnalytics API
- In Google Analytics, add mail address of API user
- Only
Display and Analytics
authority needs
YOU MUST SPECIFY ARGUMENTS AS RELATIVE FILEPATH SO YOU MUST ADD './' (dot slash) AT BEGINNING
./my_sample_app.rb
./foo/my_key_and_email.yml
./bar/my_ga_reporting_config.yml
./foobar/filters.rb
require 'simple_ga_reporting'
SimpleGaReports.configure(report_config: './bar/my_ga_reporting_config.yml', filters: './foobar/filters.rb', start_date: '2daysAgo', limit: 100)
results = SimpleGaReports.filtered_results(key_and_email: './foo/my_key_and_email.yml') # or use arguments of 'private_key:' and 'client_email:'
results.each do |result|
puts '==================================='
puts result['pagePath']
puts result['pageTitle']
puts result['pageviews']
puts result['users']
end
private_key: "-----BEGIN PRIVATE KEY-----\nMIIE..........Eqw==\n-----END PRIVATE KEY-----\n"
client_email: "[email protected]"
- You can set two key and value by not only YAML file but also arguments of
SimpleGaReports.filtered_results
method
profile_name: my_profile_name
start_date: 2018-04-01
end_date: 2018-04-05
metrics:
- users
- pageviews
dimensions:
- pagePath
- pageTitle
sort:
- -pageviews
- -users
filters:
- chrome_or_fx
- happy_page
limit: 20
module Filters
def chrome_or_fx
filter :chrome_or_fx, &lambda { contains(:browser, 'Chrome|Firefox') }
end
def happy_page
filter :happy_page, &lambda { contains(:pagePath, '\A.*happy.*\z') }
end
end
$ ruby ./my_sample_app.rb
===================================
/i_am_happy.html
I am HAPPY!
10000
1000
===================================
/happy/index.html
HAPPY TOP
9000
900
===================================
/path/to/happy.html
Are You Happy?
8000
800
===================================
/happy.php
HAPPY CHECK!
7000
700
===================================
......
When you use this gem on Rails, it's simple :)
- add this gem to
Gemfile
$ bundle install
- after it, the same way above
- Created by Ai Minatogawa
- She is an energetic creator!
- Japanese version only
- English version will...?
- can input not only
profile_name
but alsoprofile id (view id)
- implement following query parametes
- offset
- quota_user
- segment_id
segment
is the same as conbination of filters, so you can use 'filters' as alternative waysegment
is the first filter for initial population and it can't substitute
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/corselia/simple-ga-reporting. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the SimpleGaReporting project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.