module Mixpanel::Event

Constants

EVENT_PROPERTIES
IMPORT_URL
TRACK_URL

Public Instance Methods

alias(name, properties={}, options={}) click to toggle source
# File lib/mixpanel/event.rb, line 26
def alias(name, properties={}, options={})
  track_event '$create_alias', properties.merge(:alias => name), options, TRACK_URL
end
append_alias(aliased_id) click to toggle source
# File lib/mixpanel/event.rb, line 30
def append_alias(aliased_id)
  append 'alias', aliased_id
end
append_track(event, properties={}) click to toggle source
# File lib/mixpanel/event.rb, line 22
def append_track(event, properties={})
  append 'track', event, track_properties(properties, false)
end
import(event, properties={}, options={}) click to toggle source
# File lib/mixpanel/event.rb, line 18
def import(event, properties={}, options={})
  track_event event, properties, options, IMPORT_URL
end
redirect_url(event, redirect, properties={}, options={}) click to toggle source
# File lib/mixpanel/event.rb, line 14
def redirect_url(event, redirect, properties={}, options={})
  build_url event, properties, options.merge(:url => TRACK_URL, :redirect => CGI::escape(redirect))
end
track(event, properties={}, options={}) click to toggle source
# File lib/mixpanel/event.rb, line 6
def track(event, properties={}, options={})
  track_event event, properties, options, TRACK_URL
end
tracking_pixel(event, properties={}, options={}) click to toggle source
# File lib/mixpanel/event.rb, line 10
def tracking_pixel(event, properties={}, options={})
  build_url event, properties, options.merge(:url => TRACK_URL, :img => true)
end

Protected Instance Methods

build_data(event, properties, options) click to toggle source
# File lib/mixpanel/event.rb, line 55
def build_data event, properties, options
  params = {}
  data = build_event event, track_properties(properties)
  params[:data] = encoded_data(data)
  params[:api_key] = options[:api_key] if options.fetch(:api_key, nil)
  params[:img] = 1 if options[:img]
  params[:test] = 1 if options[:test]
  params
end
build_event(event, properties) click to toggle source
# File lib/mixpanel/event.rb, line 51
def build_event(event, properties)
  { :event => event, :properties => properties_hash(properties, EVENT_PROPERTIES) }
end
build_url(event, properties, options) click to toggle source
# File lib/mixpanel/event.rb, line 65
def build_url event, properties, options
  data = build_event event, track_properties(properties)
  url = "#{options[:url]}?data=#{encoded_data(data)}"
  url << "&api_key=#{options[:api_key]}" if options.fetch(:api_key, nil)
  url << "&img=1" if options[:img]
  url << "&test=1" if options[:test]
  url << "&redirect=#{options[:redirect]}" if options[:redirect]
  url
end
track_event(event, properties, options, default_url) click to toggle source
# File lib/mixpanel/event.rb, line 36
def track_event(event, properties, options, default_url)
  default = {:url => default_url, :async => @async, :api_key => @api_key}
  options = default.merge(options)
  data = build_data(event, properties, options)
  parse_response post_request(options[:url], data, options[:async])
end
track_properties(properties, include_token=true) click to toggle source
# File lib/mixpanel/event.rb, line 43
def track_properties(properties, include_token=true)
  default = {:time => Time.now, :ip => ip}
  properties = default.merge(properties)

  properties.merge!(:token => @token) if include_token
  properties_hash(properties, EVENT_PROPERTIES)
end