Module: IfdTools::ApiController::Tracking

Defined in:
lib/ifd_tools/api_controller/tracking.rb

Instance Method Summary collapse

Instance Method Details

#batch_trackObject

Used to record events from the application, more than one at a time, by submitting a request via JSON or XML

Example request stucture:

events
  event
    type product_event
    id 14
    timestamp 1233452 (optional)
    platform mac (optional)


52
53
54
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
# File 'lib/ifd_tools/api_controller/tracking.rb', line 52

def batch_track
  
  begin
  
    @events = params[:events] || []
    @events.each do |event|
    
      case event[:type]
      when "application_event"
        @event = IfdTools::Tracking::ApplicationEvent.new

      else
        @event = "ifd_tools/tracking/#{event[:type]}".camelize.constantize.new
        @event.assign_trackable_item_by_id(event[:id])
        
      end
      
      @event.ip_address = request.remote_ip
      @event.customer = current_customer
      @event.platform = event[:platform] || IfdTools::Tracking::Event.platform_for_request(request)
      @event.timestamp = Time.at(event[:timestamp]) || Time.now
      @event.save!
      
    end
    
  rescue Exception => e
    @error = e.message
    render status: :unprocessable_entity
  end
              
end

#trackObject

Used to record events from the application

 - trackable_type: ("application_event", "product_event") should be the last part of a model name, ie "IfdTools::Tracking::ProductEvent" => "product_event"
 - trackable_id: integer pk (null if trackable_type == "application")
#


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ifd_tools/api_controller/tracking.rb', line 10

def track
  
  begin

    unless params[:type].present? && params[:id].present?
      @error = "Missing 'type' or 'id' parameter"
      render status: :unprocessable_entity and return
    end

    case params[:type]
      when "application_event"
        @event = IfdTools::Tracking::ApplicationEvent.new

      else
        @event = "ifd_tools/tracking/#{params[:type]}".camelize.constantize.new
        @event.assign_trackable_item_by_id(params[:id])

    end

    unless @event.nil?
      @event.ip_address = request.remote_ip
      @event.customer = current_customer
      @event.platform = IfdTools::Tracking::Event.platform_for_request(request)
      @event.save!
    end

  rescue Exception => e
    @error = e.message
    render status: :unprocessable_entity
  end
end