Class: Fluent::GamobileOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_gamobile.rb

Instance Method Summary collapse

Constructor Details

#initializeGamobileOutput

Returns a new instance of GamobileOutput.



18
19
20
21
22
23
24
# File 'lib/fluent/plugin/out_gamobile.rb', line 18

def initialize
  super
  require 'net/http'
  require 'active_support'
  require 'active_support/core_ext'
  Net::HTTP.version_1_2
end

Instance Method Details

#build_queryObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fluent/plugin/out_gamobile.rb', line 75

def build_query
  utm_gif_location = 'http://www.google-analytics.com/r/collect'
  queries = Array.new
  queries << "v=1"
  queries << "t=pageview"
  queries << "dh=#{ERB::Util.u(get_record(@map_dh))}"
  queries << "dp=#{ERB::Util.u(get_record(@map_dp))}"
  queries << "dr=#{ERB::Util.u(get_record(@map_referer))}"
  queries << "tid=#{@ga_account}"
  queries << "uip=#{get_remote_address}"
  queries << "ua=#{get_utmv}"
  queries << "cid=#{get_visitor_id}"
  queries << "cd1=#{ERB::Util.u(get_record(@map_cd1))}"
  queries << "cd2=#{ERB::Util.u(get_record(@map_cd2))}"
  $log.info "gamobile building query: #{queries}" if @development
  return URI.parse(utm_gif_location + '?' + queries.join('&'))
end

#configure(conf) ⇒ Object



26
27
28
29
30
31
# File 'lib/fluent/plugin/out_gamobile.rb', line 26

def configure(conf)
  super
  @development = Fluent::Config.bool_value(@development) || false
  @unique_ident_key = @unique_ident_key.split(',')
  $log.info "gamobile treats unique identifer key with #{@unique_ident_key}"
end

#emit(tag, es, chain) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/fluent/plugin/out_gamobile.rb', line 33

def emit(tag, es, chain)
  es.each do |time,record|
    report(record)
  end

  chain.next
end

#get_record(key) ⇒ Object



45
46
47
# File 'lib/fluent/plugin/out_gamobile.rb', line 45

def get_record(key)
  return @record[key] unless @record[key].blank?
end

#get_remote_addressObject



49
50
51
52
53
54
55
# File 'lib/fluent/plugin/out_gamobile.rb', line 49

def get_remote_address
  if get_record(@map_remoteaddr) =~ /^([^.]+\.[^.]+\.[^.]+\.).*/
    return "#{$1}0"
  else
    return ''
  end
end

#get_utmvObject



70
71
72
73
# File 'lib/fluent/plugin/out_gamobile.rb', line 70

def get_utmv
   user_var = get_record(@set_var).gsub(';','%3B')
   return ERB::Util.u("+__utmv=999#{user_var};") unless user_var.blank?
end

#get_visitor_idObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fluent/plugin/out_gamobile.rb', line 57

def get_visitor_id
  if !@unique_ident_key.blank?
    message = "#{@ga_account}"
    @unique_ident_key.map {|key| message.concat("#{get_record(key)}")}
  elsif get_record(@map_guid).blank?
    message = "#{get_record(@map_useragent)}#{Digest::SHA1.hexdigest(rand.to_s)}#{Time.now.to_i}"
  else
    message = "#{get_record(@map_guid)}#{@ga_account}"
  end
  md5string = Digest::MD5.hexdigest(message)
  return "0x#{md5string[0,16]}"
end

#report(record) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/fluent/plugin/out_gamobile.rb', line 93

def report(record)
  set_record(record)
  begin
    uri = build_query
    $log.info "gamobile sending report: #{uri.to_s}" if @development
    Net::HTTP.start(uri.host, uri.port) do |http|
      http.get(uri.request_uri, {
        "User-Agent" => get_record(@map_useragent).to_s,
        "Accepts-Language" => get_record(@map_acceptlang).to_s
      })
    end
  rescue => e
    $log.error("gamobile Error: #{e.message}")
  end
end

#set_record(record) ⇒ Object



41
42
43
# File 'lib/fluent/plugin/out_gamobile.rb', line 41

def set_record(record)
  @record = record
end