Created
November 3, 2016 18:13
-
-
Save PlenipotentSS/ddd6cf6e491eb89bb0ef4ca5e405f5d7 to your computer and use it in GitHub Desktop.
Service class using ruby/rails and google api client >.9. This uses google analytics service account.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'google/apis/analytics_v3' | |
class GoogleAnalyticsService | |
def initialize | |
@filters = nil | |
@metrics = "ga:uniquePageviews" | |
@dimensions = 'ga:date' | |
end | |
# get string of key file in rails root | |
# storing key in config path, feel free to change as needed. | |
def google_api_key_path | |
Rails.root.join('config', ENV['GA_API_KEY']).to_s | |
end | |
# return file for api key path | |
def google_api_key | |
File.open(google_api_key_path, "r") | |
end | |
# setup authorization given a specific scope | |
def authorization(scope) | |
Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: google_api_key, scope: scope ) | |
end | |
# primary service to gain scope and authorize analytics access | |
def analytics_service | |
scope = ['https://www.googleapis.com/auth/analytics.readonly'] | |
client = Google::Apis::AnalyticsV3::AnalyticsService.new | |
client.authorization = authorization(scope) | |
client | |
end | |
# the google analytics view id in api form | |
# e.g. ga:XXXXXXXXX | |
def ga_profile | |
"ga:#{ENV['GA_VIEW_ID']}" | |
end | |
# A comma-separated list of dimension or metric filters to be applied to | |
# Analytics data. | |
# e.g. 'ga:pagePath=~/featured/,ga:uniquePageviews>=20' | |
# see https://github.com/tpitale/legato | |
# https://developers.google.com/analytics/devguides/reporting/core/v3/reference#filters | |
def change_filters(filters) | |
@filters = filters | |
end | |
# A comma-separated list of Analytics metrics. E.g., 'ga:sessions,ga:pageviews'. | |
# At least one metric must be specified. | |
# e.g. 'ga:uniquePageviews' | |
# https://developers.google.com/analytics/devguides/reporting/core/dimsmets | |
def change_metrics(metrics) | |
if metrics.present? | |
@metrics = metrics | |
end | |
end | |
# A comma-separated list of Analytics dimensions. E.g., 'ga:browser,ga:city'. | |
# e.g. 'ga:date' | |
# https://developers.google.com/analytics/devguides/reporting/core/dimsmets | |
def change_dimensions(dimensions) | |
@dimensions = dimensions | |
end | |
# Get the parameters to search the analytics api | |
def analytics_analytics_parameters | |
{ | |
'ids' => ga_profile, | |
'start-date' => (Date.today - 30).strftime("%Y-%m-%d"), | |
'end-date' => Time.now.strftime("%Y-%m-%d"), | |
'metrics' => @metrics, | |
'dimensions' => @dimensions, | |
'filters' => @filters | |
} | |
end | |
# request google analytics rows using ga_profile view | |
# 30 days and any given metrics, dimensions and filters: | |
# see https://ga-dev-tools.appspot.com/query-explorer/ | |
# | |
# uses api .get_ga_data as primary resource | |
# see https://github.com/google/google-api-ruby-client/blob/master/generated/google/apis/analytics_v3/service.rb | |
# @return analytics response | |
# .rows is the data | |
def get | |
analytics_service.get_ga_data( | |
analytics_parameters['ids'], | |
analytics_parameters['start-date'], | |
analytics_parameters['end-date'], | |
analytics_parameters['metrics'], | |
dimensions: analytics_parameters['dimensions'], | |
filters: analytics_parameters['filters'] | |
) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment