Class: GoogleAnalytics
- Inherits:
-
Object
- Object
- GoogleAnalytics
- Defined in:
- app/services/google_analytics.rb
Instance Method Summary collapse
Constructor Details
#initialize ⇒ GoogleAnalytics
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'app/services/google_analytics.rb', line 8 def initialize @client = Google::APIClient.new(:application_name => ENV['GA_APP_NAME'], :application_version => '1.0') key_file = File.join('config', ENV['GA_KEY_FILE_NAME']) key = Google::APIClient::PKCS12.load_key(key_file, 'notasecret') service_account = Google::APIClient::JWTAsserter.new( ENV['GA_SERVICE_ACCOUNT_EMAIL'], ['https://www.googleapis.com/auth/analytics.readonly', 'https://www.googleapis.com/auth/prediction'], key) @client. = service_account. @analytics = @client.discovered_api('analytics', 'v3') end |
Instance Method Details
#visitors(startDate, endDate) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/services/google_analytics.rb', line 22 def visitors(startDate, endDate) results = @client.execute(:api_method => @analytics.data.ga.get, :parameters => { 'ids' => "ga:" + ENV['GA_VIEW_ID'], 'start-date' => startDate, 'end-date' => endDate, 'metrics' => "ga:visitors", 'dimensions' => "ga:year,ga:month,ga:day", 'sort' => "ga:year,ga:month,ga:day" }) if results.error? puts results. return {} else hash = {} results.data.rows.each do |r| hash["#{r[0]}-#{r[1]}-#{r[2]}"] = r[3].to_i end return hash end end |