Class: TrakioClient::Track

Inherits:
EndPoint show all
Defined in:
lib/trakio_client/track.rb

Instance Attribute Summary

Attributes inherited from EndPoint

#trakio

Instance Method Summary collapse

Methods inherited from EndPoint

#initialize

Constructor Details

This class inherits a constructor from TrakioClient::EndPoint

Instance Method Details

#check_page_view_parameters(url) ⇒ Object



65
66
67
68
69
# File 'lib/trakio_client/track.rb', line 65

def check_page_view_parameters url
  unless url
    raise Exceptions::InvalidParameter.new("The `url` parameter must be provided.")
  end
end

#check_parameters(event, distinct_id, company_id, properties) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/trakio_client/track.rb', line 53

def check_parameters event, distinct_id, company_id, properties
  unless event
    raise Exceptions::MissingParameter.new("The `event` parameter must be provided.")
  end
  unless distinct_id || company_id
    raise Exceptions::MissingParameter.new('Either a `company_id` or `distinct_id` parameter must be provided.')
  end
  unless properties.is_a?(Hash)
    raise Exceptions::InvalidParameter.new("The `properties` parameter must be a hash.")
  end
end

#page_view(p) ⇒ Object



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

def page_view p
  args = { event: 'Page view' }
  distinct_id = p[:distinct_id] || self.distinct_id
  url = p[:url]
  title = p[:title]
  check_page_view_parameters url

  properties = {
    url: url,
  }
  properties[:title] = title if title
  args[:properties] = properties
  args[:distinct_id] = distinct_id if distinct_id

  run args  # right now page_view is an alias of track
end

#process_time(time) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/trakio_client/track.rb', line 41

def process_time time
  if time
    if !time.is_a? String
      time.iso8601
    else
      time
    end
  else
    DateTime.now.iso8601
  end
end

#run(p = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/trakio_client/track.rb', line 4

def run p = {}
  event = p[:event]
  distinct_id = p[:distinct_id] || self.distinct_id
  company_id = p[:company_id] || self.company_id
  channel = p[:channel] || self.channel
  properties = p[:properties] || {}
  check_parameters event, distinct_id, company_id, properties

  params = {
    event: event
  }
  params[:time] = process_time p[:time]
  params[:channel] = channel if channel
  params[:properties] = properties if properties && !properties.empty?
  params[:company_id] = company_id if company_id
  params[:distinct_id] = distinct_id if distinct_id

  send_request('track', params)
end