Class: Fluent::MixpanelOutput

Inherits:
BufferedOutput
  • Object
show all
Includes:
HandleTagNameMixin
Defined in:
lib/fluent/plugin/out_mixpanel.rb

Defined Under Namespace

Classes: MixpanelError

Instance Method Summary collapse

Constructor Details

#initializeMixpanelOutput

Returns a new instance of MixpanelOutput.



20
21
22
23
# File 'lib/fluent/plugin/out_mixpanel.rb', line 20

def initialize
  super
  require 'mixpanel-ruby'
end

Instance Method Details

#configure(conf) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fluent/plugin/out_mixpanel.rb', line 25

def configure(conf)
  super
  @project_tokey = conf['project_token']
  @distinct_id_key = conf['distinct_id_key']
  @event_key = conf['event_key']
  @ip_key = conf['ip_key']
  @event_map_tag = conf['event_map_tag']
  @api_key = conf['api_key']
  @use_import = conf['use_import']
  @use_legacy_prefix_behavior = conf['use_legacy_prefix_behavior']

  if @event_key.nil? and !@event_map_tag
    raise Fluent::ConfigError, "'event_key' must be specifed when event_map_tag == false."
  end
end

#format(tag, time, record) ⇒ Object



50
51
52
53
# File 'lib/fluent/plugin/out_mixpanel.rb', line 50

def format(tag, time, record)
  time = record['time'] if record['time'] && @use_import
  [tag, time, record].to_msgpack
end

#send_to_mixpanel(records) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/fluent/plugin/out_mixpanel.rb', line 100

def send_to_mixpanel(records)
  records.each do |record|
   success = 	if @use_import
      					@tracker.import(@api_key, record['distinct_id'], record['event'], record['properties'])
    					else
      					@tracker.track(record['distinct_id'], record['event'], record['properties'])
    					end
    raise MixpanelError.new("Failed to track event to mixpanel") unless success
  end
end

#shutdownObject



46
47
48
# File 'lib/fluent/plugin/out_mixpanel.rb', line 46

def shutdown
  super
end

#startObject



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

def start
  super
  @tracker = Mixpanel::Tracker.new(@project_token)
end

#write(chunk) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fluent/plugin/out_mixpanel.rb', line 55

def write(chunk)
  records = []
  chunk.msgpack_each do |tag, time, record|
    data = {}
    prop = data['properties'] = record.dup

    # Ignore token in record
    prop.delete('token')

    if @event_map_tag
      tag.gsub!(/^\./, '') if @use_legacy_prefix_behavior
      data['event'] = tag
    elsif record[@event_key]
      data['event'] = record[@event_key]
      prop.delete(@event_key)
    else
      log.warn('no event')
      return
    end

    # Ignore browswer only special event
    return if data['event'].start_with?('mp_')

    if record[@distinct_id_key]
      data['distinct_id'] = record[@distinct_id_key]
      prop.delete(@distinct_id_key)
    else
      log.warn('no distinct_id')
      return
    end

    if !@ip_key.nil? and record[@ip_key]
      prop['ip'] = record[@ip_key]
      prop.delete(@ip_key)
    end

    prop.select! {|key, _| !key.start_with?('mp_') }
    prop.merge!('time' => time.to_i)

    records << data
  end

  send_to_mixpanel(records)
end