Japanese Document
- Japanese Document (my blog's article) is following
What's this?
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 :)
Wrapper gem of Legato
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.
Get Started
1. Installation
$ gem install simple_ga_reporting2. Set private_key and client_email
- There are two ways to set
private_keyandclient_email - How to get above two Key-Value sets is written later
2-1. The first way: Create API configuration file
- Create
./config/key_and_email.yml - This YAML file must contain two Key-Value sets
-
private_keyand its value -
client_emailand its value
-
2-2. The second way: Set values as arguments
- Set arguments of
SimpleGaReports.filtered_resultsmethod - For example:
SimpleGaReports.filtered_results(private_key: 'FOO', client_email: 'BAR')
- Convenient when you set by environment variables
3. Create Google Analytics reporting configuration file
- You must create the YAML file which contains Google Analytics reporting configuration such as
start-date,end-date,metrics,demensions,filtersand etc. - filepath is
./config/ga_reporting_config.yml - This YAML file format is shown later
4. Create filters file (Optional)
- If you wanna use filters function, please create
./config/filters.rb - the filters configuration on GUI is below
5. Write your app
- Write your application used this gem
- The sample code is below
- Use only two class methods
SimpleGaReports.configureSimpleGaReports.filtered_results
- Only three lines :-)
- Note
- If you don't use filters, use
SimpleGaReports.raw_resultstoo - when no filter is in configure file,
SimpleGaReports.raw_resultsis 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::QueryDetail of configuration
1. Configuration of API account
- You must set Google Analytics API configuration,
private_keyandclient_email - There are two ways to set it
- The one is by arguments
- Set arguments of
SimpleGaReports.filtered_resultsmethod
- 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: "your_account_name@foobar.iam.gserviceaccount.com"2. Google Analytics reporting configuration
- 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.configuremethod
- 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_datein configure file (notstart-date) -
end_datein configure file (notend-date) -
limitin configure file (instead ofmax-results) -
sampling_levelin configure file (notsamplingLevel)
-
- The type of
metrics,dimensions,sortandfiltersisArray- The order of elements doesn't matter
2-1. Key-Value detail
profile_name
- REQUIRED
- It was called
view (view name)before- but in Japanese page, still
ビュー名
- but in Japanese page, still
start_date
- REQUIRED
- Original Key name is
start-date - Value examples
2018-04-01-
todayoryesterday -
14daysAgo- use
daysAgophrase even if1day
- use
end_date
- REQUIRED
- Original Key name is
end-date - Value examples are the same as
start-date
metrics
- REQUIRED
-
指標in Japanese - Official documents are below (very useful!)
- Value examples
usersuniquePageviews
dimensions
- optional
- Official documents are below (very useful!)
- Value examples
pagePathsessionCount
sort
- optional
- prefix
-
-sign means descending - no sign means ascending
- Don't use
+sign
- Don't use
-
- Value examples
-pageviewsusers
filters
- optional
- Value is method name you named in
filters.rb
limit
- optional
- Original Key name is
max-results - Value is max amount of returned rows
- Default value is
100
sampling_level
- optional
- Original Key name is
samplingLevel - Default value is
HIGHER_PRECISION
3. filters file
- You use filters function using
filters.rbfile- the filename
filters.rbis determined- only filepath is optional
- the module name
Filtersis determined - Default filepath is
./config/filters.rb- You can change it by argument with
configuremethod
- 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_fxmethod 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_fxmethod
- Adding refer Legato's Documents
Arguments of method
Note
YOU MUST SPECIFY ARGUMENTS AS RELATIVE FILEPATH SO YOU MUST ADD './' (dot slash) AT BEGINNING
- Set arguments of
SimpleGaReports.filtered_resultsmethod - For example:
- `SimpleGaReports.filtered_results(private_key: 'FOO', client_email: 'BAR')
- Convenient when you set by environment variables
SimpleGaReports.filtered_results method
- If you don't use filters, use
SimpleGaReports.raw_resultsmethod instead ofSimpleGaReports.filtered_resultsmethod- when no filter is in configure file,
SimpleGaReports.raw_resultsis 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_keyandclient_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
SimpleGaReports.configure method
- 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
How to get private_key and client_email
1. Access to Google Cloud Platform Console
2. Create new project
- name new project as you like
3. Create service account for Google Analytics API and select API and Service from menu
4. Create authentication information
- Select
Service Account Key
5. Select Service Account and download JSON Key
6. Open JSON Key file by editor and pick up "private_key" and "client_email"
7. They are the very Key-Value sets you want
- Deal with them carefully
8. They are the very Key-Value sets you want
- Deal with them carefully
9. Activate Analytics API
- NOT
Google Analytics Reporting APIBUTAnalytics API
10. Add API user to Google Analytics
- In Google Analytics, add mail address of API user
- Only
Display and Analyticsauthority needs
Sample App
Note
YOU MUST SPECIFY ARGUMENTS AS RELATIVE FILEPATH SO YOU MUST ADD './' (dot slash) AT BEGINNING
Prepare files
./my_sample_app.rb./foo/my_key_and_email.yml./bar/my_ga_reporting_config.yml./foobar/filters.rb
1. ./my_sample_app.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']
end2. ./foo/my_key_and_email.yml
private_key: "-----BEGIN PRIVATE KEY-----\nMIIE..........Eqw==\n-----END PRIVATE KEY-----\n"
client_email: "your_account_name@foobar.iam.gserviceaccount.com"- You can set two key and value by not only YAML file but also arguments of
SimpleGaReports.filtered_resultsmethod
3. ./bar/my_ga_reporting_config.yml
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: 204. ./foobar/filters.rb
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
endexecute script
$ 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
===================================
......Rails
When you use this gem on Rails, it's simple :)
- add this gem to
Gemfile $ bundle install- after it, the same way above
Very Awesome Reference Book (Recommended)
- Created by Ai Minatogawa
- She is an energetic creator!
- Japanese version only
- English version will...?
Official documents
Core Reporting API - Reference Guide
Dimensions & Metrics Explorer
Query Explorer
API Rate Limits
TODO
- can input not only
profile_namebut alsoprofile id (view id) - implement following query parametes
- offset
- quota_user
- segment_id
segmentis the same as conbination of filters, so you can use 'filters' as alternative way-
segmentis the first filter for initial population and it can't substitute
Development
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.
Contributing
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.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the SimpleGaReporting project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.


.png)








