GA Dashboard
This gem helps you to import Google Analytics Dashboard into your Rails application.
You can pull out the following stats from Google Analytics
- Most Popular Pages
- Geo Chart (Map) or Table which represents the following data for a given time period
- Users count from various countries across the world
- Users count from various countries in a continent
- Users count from various cities of a country
- Overview of users and sessions for a given time period as line chart
Installation
Add this line to your application's Gemfile:
gem 'ga_dashboard'
And then execute:
$ bundle
Or install it yourself as:
$ gem install ga_dashboard
Usage
###Prerequisites
You need to set up the following things before using this gem.
- Create Google Analytics account for your project, add the script with tracking code in all the view pages that you want to track.
- Create a project at Google Developers Console
- In Google Developers console, Select your Project -> API & auth -> Search for Analytics -> Enable API
- Under API & auth -> Credentials -> Add Credentials -> Service Account -> Select P12 and create
- Download the Key file and save it in your_project_folder/lib/keys
- Note down the service email address that is created. It should be something like ‘something-long@developer.gserviceaccount.com’
- Login into your Google Analytics account -> Admin -> User Management -> Add permission to your service email address.
- Open Query Explorer, select your GA account for the project. Note down the number shown after ga. (e.g., ga:xxxxxxxxxx) which is the GA Profile ID
###Configure your Rails application
In your Rails application, create a file 'ga_dashboard.rb' inside config/initializers with the following lines
GA_SERVICE_EMAIL_ADDRESS="<Your Service Email Address>"
GA_PROFILE_ID="<Ga Profile ID>"
PATH_TO_KEY_FILE="#{Rails.root}/keys/<Your key file name>"
###Add code to your controller (e.g., reports_controller#index)
@ga_dashboard_api=GaDashboard::AnalyticsApi.new("your_project_name",version_no)
###Add the dashboard in your views
In your view file (e.g., reports/index.html.erb) add this
<script src='https://www.google.com/jsapi'></script>
- Create the division where you want the dashboard to be appear.
- Use any of the helper methods listed below to generate the view.
####1. Popular Pages
display_most_popular_pages(ga_dashboard_api,pages_starting_with,start_date,end_date,id_of_the_division)
#####Example:
<div id='popular_pages'></div>
<%= display_most_popular_pages(@ga_dashboard_api,"home",DateTime.now-2.months,DateTime.now,'popular_pages') %>
####2. Users count from various countries across the world
display_users_from_countries_across_world(ga_dashboard_api,start_date,end_date,id_of_the_division,format="table",options={})
- format can be either "table" or "map"
- options - google charts options. Refer Google Charts Documentation
#####Example: Using Map
<div id='world'></div>
<%= display_users_from_countries_across_world(@ga_dashboard_api,DateTime.now-2.months,DateTime.now,'world','map',{:title=>'Users Across world'}) %>
#####Example: Using Table
<div id='world1'></div>
<%= display_users_from_countries_across_world(@ga_dashboard_api,DateTime.now-2.months,DateTime.now,'world1','table',{:title=>'Users Across world'}) %>
####3. Users count from various countries in a continent
display_users_from_countries_of_continent(ga_dashboard_api,continent,start_date,end_date,division_id,format="table",options={})
Acceptable continent names are Africa,Europe,Americas,Asia and Oceania
#####Example: Using Map
<div id='continent'></div>
<%= display_users_from_countries_of_continent(@ga_dashboard_api,"Asia",DateTime.now-2.months,DateTime.now,'continent','map',{:title=>"Users from Asia"}) %>
####4. Users count from various cities of a country
display_users_from_cities_of_country(ga_dashboard_api,country,start_date,end_date,id_of_the_division,format="table",options={})
Acceptable country names are listed here
#####Example: Using Map
<div id='country'></div>
<%= display_users_from_cities_of_country(@ga_dashboard_api,"India",DateTime.now-2.months,DateTime.now,'country','map',{:title=>"Users from India"}) %>
####5. Users Overview
display_users_overview(ga_dashboard_api,start_date,end_date,region_type,region,div_id,options={})
- Region Type - Acceptable values are country or continent
- Region - continent names or countries names basis region type
#####Example: Using Map
<div id='users_overview1'></div>
<%= display_users_overview(@ga_dashboard_api,DateTime.now-2.months,DateTime.now,'country','India','users_overview1',{:title=>"Users Overview",:height=>'500'}) %>
<div id='users_overview2'></div>
<%= display_users_overview(@ga_dashboard_api,DateTime.now-1.months,DateTime.now,'country','India','users_overview2',{:title=>"Users Overview",:height=>'500'}) %>
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/renugasaraswathy/ga_dashboard. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
License
The gem is available as open source under the terms of the MIT License.