Class: Hubspot::Performable::Event

Inherits:
Struct
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
app/models/hubspot/performable/event.rb

Overview

The Event class can be used to record events using the Performable API. Example: Hubspot::Event.new(‘event_32145’).record! The response will always be a 1X1 transparent GIF, this is because the HTTP API can be used via an HTML image tag as well :)

Constant Summary collapse

PARAMETER_MAPPING =

_a - Your Performable API key. (Your API Key can be found in Account Settings within your Performable account.) _n - The EVENT_ID of the event you want to record. You can get an event id in two ways: Log into your Performable account, click Setup, and then click on the event that you wish to record. Finally, click the configuration link to bring up a dialog which shows you the event id. Use your own unique label as the id, and Performable will automatically generate the corresponding event in your account. You can rename it from within the app later if you’d like. There are also optional parameters:

_t - The timestamp of the event (in seconds as a UTC Unix epoch). By default this is ignored - it is used only to help make requests unique and the time that our servers receive the event is used for the timestamp of the event. _d - If equal to the string true, implies that the timestamp sent with this event should be used as the time of completion for this event. The timestamp will be ignored if it represents a future date (within some reason), or is more than a few years old. Servers generating timestamps should use some mechanism (like NTP) for syncing clocks to a reasonable standard. The API expects timestamps are in seconds, but may detect timestamps in milliseconds and attempt to resolve them to seconds. _l (that’s a lowercase L) - The URL that triggered this event. _ip - The IP address of the user who completed this event. This is useful if you want Performable to assign location data to a user based on their IP address.

If you are recording an e-commerce transaction you may use any of the following parameters:

value - A monetary value associated with this event, such as the purchase amount or expected conversion value. This value should be a number with no formatting other than a period representing the decimal point (if necessary.) order_id - A unique identifier associated with the order or transaction. This is used to make sure this transaction is not counted more than once.

{ :event_id => '_n', :performed_at => '_t', :completed_at => '_d', :url => '_l', :ip => '_ip', :value => 'value', :order_id => 'order_id'}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#completed_atObject

Returns the value of attribute completed_at

Returns:

  • (Object)

    the current value of completed_at



10
11
12
# File 'app/models/hubspot/performable/event.rb', line 10

def completed_at
  @completed_at
end

#custom_parametersObject

Returns the value of attribute custom_parameters

Returns:

  • (Object)

    the current value of custom_parameters



10
11
12
# File 'app/models/hubspot/performable/event.rb', line 10

def custom_parameters
  @custom_parameters
end

#event_idObject

Returns the value of attribute event_id

Returns:

  • (Object)

    the current value of event_id



10
11
12
# File 'app/models/hubspot/performable/event.rb', line 10

def event_id
  @event_id
end

#ipObject

Returns the value of attribute ip

Returns:

  • (Object)

    the current value of ip



10
11
12
# File 'app/models/hubspot/performable/event.rb', line 10

def ip
  @ip
end

#order_idObject

Returns the value of attribute order_id

Returns:

  • (Object)

    the current value of order_id



10
11
12
# File 'app/models/hubspot/performable/event.rb', line 10

def order_id
  @order_id
end

#performed_atObject

Returns the value of attribute performed_at

Returns:

  • (Object)

    the current value of performed_at



10
11
12
# File 'app/models/hubspot/performable/event.rb', line 10

def performed_at
  @performed_at
end

#urlObject

Returns the value of attribute url

Returns:

  • (Object)

    the current value of url



10
11
12
# File 'app/models/hubspot/performable/event.rb', line 10

def url
  @url
end

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



10
11
12
# File 'app/models/hubspot/performable/event.rb', line 10

def value
  @value
end

Instance Method Details

#recordObject

Records, but doesn’t raise an error when the record fails and returns flase instead.



43
44
45
# File 'app/models/hubspot/performable/event.rb', line 43

def record
  record! rescue false
end

#record!Object



32
33
34
35
36
37
38
39
40
# File 'app/models/hubspot/performable/event.rb', line 32

def record!
  return false unless valid?
     
  uri.query = to_param      
  http = Net::HTTP.new(uri.host, uri.port)
  http.set_debug_output(Hubspot.config.debug_http_output) if Hubspot.config.debug_http_output
      
  return http.request_get(uri.request_uri).code.to_i
end

#to_paramObject

Converts the event to encoded form data.



53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/hubspot/performable/event.rb', line 53

def to_param
  parameters = PARAMETER_MAPPING.keys.inject({}) do |hash, parameter| 
    parameter_value = send(parameter)
    hash[PARAMETER_MAPPING[parameter]] = parameter_value if parameter_value.present?
    hash
  end

  parameters.merge!(custom_parameters) if custom_parameters
  parameters.merge!('_a' => Hubspot.config.hubspot_key)        
  parameters.to_param      
end

#uriObject

Sets the Performable API URI



48
49
50
# File 'app/models/hubspot/performable/event.rb', line 48

def uri
  @uri ||= URI('http://analytics.performable.com/v1/event')            
end