Skip to content

Instantly share code, notes, and snippets.

@simllll
Forked from willjohnson/README.md
Last active February 24, 2017 17:47
Show Gist options
  • Save simllll/1e541da2ebb8b70ba4a2fe267dad11ec to your computer and use it in GitHub Desktop.
Save simllll/1e541da2ebb8b70ba4a2fe267dad11ec to your computer and use it in GitHub Desktop.
Google Analytics Website Visitor Count Widget for Dashing with OAuth2 Authentication (multiple profiles)

Description

This is a fork with support of the latest google api of the Google Analytics Website Visitor Count Widget for Dashing with OAuth2 Authentication to allow for multiple profiles to be shown in the same widget.

A Dashing widget for displaying the number of visitors to your website over a specified timeframe, as reported by Google Analytics

Dependencies

google-api-ruby-client

Add it to dashing's gemfile:

gem 'google-api-client'

and run bundle install.

Usage

To use this widget, you'll first need to set up a Google API project and attach it to the Google Analytics profile you wish to monitor.

1. Create and download a new private key for Google API access.

  1. Go to https://code.google.com/apis/console
  2. Click 'Create Project'
  3. Enable 'Analytics API' service and accept both TOS's
  4. Click 'API Access' in the left-hand nav menu
  5. Click 'Create an OAuth 2.0 Client ID'
  6. Enter a product name (e.g. Dashing Widget) - logo and url are optional
  7. Click 'Next'
  8. Under Application Type, select 'Service Account'
  9. Click 'Create Client ID'
  10. Click 'Download private key' (JSON!) NOTE: This will be your only opportunity to download this key.
  11. Close the download key dialog
  12. Find the details for the service account you just created and copy it's email address which will look something like this: [email protected] - you'll need it in your ruby code later

2. Attach your Google API service account to your Google Analytics profile

Note: you will need to be an administrator of the Google Analytics profile

  1. Log in to your Google Analytics account: http://www.google.com/analytics/
  2. Click 'Admin' in the upper-right corner
  3. Select the account containing the profile you wish to use
  4. Select the property containing the profile you wish to use
  5. Select the profile you wish to use
  6. Click the 'Users' tab
  7. Click '+ New User'
  8. Enter the email address you copied from step 13 above
  9. Click 'Add User'

3. Locate the ID for your Google Analytics profile

  1. On your Google Analytics profile page, click the 'Profile Settings' tab
  2. Under 'General Information' copy your Profile ID (e.g. 654321) - you'll need it in your ruby code later

4. Start coding (finally)

  1. Copy the visitor_count.rb file in to your dashing jobs\ folder.

  2. Update the profile_id and set ENVIRONEMTN variabel GOOGLE_APPLICATION_CREDENTIALS to your google-service-account.json file

  3. Add the widget HTML to your dashboard

    <li data-row="1" data-col="1" data-sizex="1" data-sizey="1"> 
        <div data-id="visitor_count" data-view="List" data-unordered="true" data-title="Visitors This Month"></div> 
    </li>

Notes

If you want to modify this plugin to pull other data from Google Analytics, be sure to check out the Google Analytics Query Explorer.

<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
<div data-id="visitor_count" data-view="List" data-unordered="true" data-title="Visitors This Month"></div>
</li>
require 'date'
require 'google/apis/analytics_v3'
max_amount = 100
# Update these to match your own apps credentials
# set ENV GOOGLE_APPLICATION_CREDENTIALS = "/home/ubuntu/google-service-account.json"
profile_id = '...' # Analytics profile ID.
visitors = []
(1..10).each do |i|
visitors << { x: Time.now.to_i-(10-i), y: 0 }
end
# Get the Google API client
scopes = ['https://www.googleapis.com/auth/analytics.readonly']
authorization = Google::Auth.get_application_default(scopes)
Analytics = Google::Apis::AnalyticsV3
analytics = Analytics::AnalyticsService.new
# Start the scheduler
SCHEDULER.every '5s', :first_in => 0 do
# Request a token for our service account
analytics.authorization = authorization
# Get the analytics API
response = analytics.get_realtime_data("ga:" + profile_id, "ga:activeVisitors")
visitors << { x: Time.now.to_i, y: response.rows[0][0].to_i }
if visitors.size > max_amount
visitors.shift
end
# Update the dashboard
send_event('visitor_count_real_time', points: visitors)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment