class LogStash::Inputs::GoogleAnalytics

Generate a repeating message.

This plugin is intented only as an example.

Public Instance Methods

register() click to toggle source
# File lib/logstash/inputs/googleanalytics.rb, line 98
def register
  require 'google/api_client'
end
run(queue) click to toggle source
# File lib/logstash/inputs/googleanalytics.rb, line 102
def run(queue)
  # we can abort the loop if stop? becomes true
  while !stop?
    start = Time.now
    client, analytics = get_service
    results_index = @start_index
    while !stop?
      results = client.execute(
        :api_method => analytics.data.ga.get,
        :parameters => client_options(results_index))

      if results.data.rows.first
        query = results.data.query.to_hash
        profile_info = results.data.profile_info.to_hash
        column_headers = results.data.column_headers.map { |c|
          c.name
        }

        results.data.rows.each do |r|
          event = LogStash::Event.new()
          decorate(event)
          event['containsSampledData'] = results.data.containsSampledData
          event['query'] = query if @store_query
          event['profileInfo'] = profile_info if @store_profile
          column_headers.zip(r).each do |head,data|
            if is_num(data)
              float_data = Float(data)
              # Sometimes GA returns infinity. if so, the number it invalid
              # so set it to zero.
              if float_data == Float::INFINITY
                event[head.gsub(':','_')] = 0.0
              else
                event[head.gsub(':','_')] = float_data
              end
            else
              event[head.gsub(':','_')] = data
            end
          end
          # Try to add a date unless it was already added
          if @start_date == @end_date
            if !event.include?('ga_date')
              if @start_date == 'today'
                event['ga_date'] = Date.parse(Time.now().strftime("%F"))
              elsif @start_date == 'yesterday'
                event['ga_date'] = Date.parse(Time.at(Time.now.to_i - 86400).strftime("%F"))
              elsif @start_date.include?('daysAgo')
                days_ago = @start_date.sub('daysAgo','').to_i
                event['ga_date'] = Date.parse(Time.at(Time.now.to_i - (days_ago*86400)).strftime("%F"))
              else
                event['ga_date'] = Date.parse(@start_date)
              end
            else
              event['ga_date'] = Date.parse(event['ga_date'].to_i.to_s)
            end
          else
            # Convert YYYYMMdd to YYYY-MM-dd
            event['ga_date'] = Date.parse(event['ga_date'].to_i.to_s)
          end
          event['ga_date'] = event['ga_date'].to_s
          queue << event
        end
      end
      nextLink = results.data.nextLink rescue nil
      if nextLink
        start_index+=@max_results
      else
        break
      end
    end
    if @interval.nil?
      break
    else
      duration = Time.now - start
      # Sleep for the remainder of the interval, or 0 if the duration ran
      # longer than the interval.
      sleeptime = [0, @interval - duration].max
      if sleeptime == 0
        @logger.warn("Execution ran longer than the interval. Skipping sleep.",
                     :duration => duration,
                     :interval => @interval)
      else
        Stud.stoppable_sleep(sleeptime) { stop? }
      end
    end
  end # loop
end
stop() click to toggle source
# File lib/logstash/inputs/googleanalytics.rb, line 189
def stop
end

Private Instance Methods

client_options(results_index) click to toggle source
# File lib/logstash/inputs/googleanalytics.rb, line 193
def client_options(results_index)
  options = {
    'ids' => @ids,
    'start-date' => @start_date,
    'end-date' => @end_date,
    'metrics' => @metrics,
    'max-results' => @max_results,
    'output' => 'json',
    'start-index' => results_index
  }
  options.merge!({ 'dimensions' => @dimensions }) if @dimensions
  options.merge!({ 'filters' => @filters }) if @filters
  options.merge!({ 'sort' => @sort }) if @sort
  options.merge!({ 'segment' => @segment }) if @segment
  options.merge!({ 'samplingLevel' => @sampling_level }) if @sampling_level
  options.merge!({ 'include-empty-rows' => @include_empty_rows }) if !@include_empty_rows.nil?
  return options
end
get_service() click to toggle source
# File lib/logstash/inputs/googleanalytics.rb, line 212
def get_service
  client = Google::APIClient.new(
    :application_name => 'Google Analytics Logstash Input',
    :application_version => '1.0.0')

  puts @key_file_path
  puts @key_secret
  puts @service_account_email
  # Load our credentials for the service account
  key = Google::APIClient::KeyUtils.load_from_pkcs12(@key_file_path, @key_secret)
  client.authorization = Signet::OAuth2::Client.new(
    :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
    :audience => 'https://accounts.google.com/o/oauth2/token',
    :scope => 'https://www.googleapis.com/auth/analytics.readonly',
    :issuer => @service_account_email,
    :signing_key => key)

  # Request a token for our service account
  client.authorization.fetch_access_token!
  analytics = client.discovered_api(@service_name, @api_version)
  return client, analytics
end
is_num(a) click to toggle source
# File lib/logstash/inputs/googleanalytics.rb, line 236
def is_num(a)
  return (Float(a) and true) rescue false
end