Class: Bento::Events

Inherits:
Object
  • Object
show all
Extended by:
Validators::Base, Validators::EventValidators
Defined in:
lib/bento/resources/events.rb

Class Method Summary collapse

Methods included from Validators::EventValidators

validate_cart, validate_cart_items, validate_details, validate_event, validate_unique, validate_value

Methods included from Validators::Base

validate_email, validate_fields, validate_type

Class Method Details

.import(events) ⇒ Object

Batch track multiple events Usage: Bento::Events.import([‘test@bentonow.com’, type: ‘Login’, ‘test@bentonow.com’, type: ‘Purchase’, fields: { first_name: ‘Jesse’, last_name: ‘Hanley’ }])

Raises:


57
58
59
60
61
62
63
64
# File 'lib/bento/resources/events.rb', line 57

def import(events)
  raise ArgumentError, 'Events must be an array' unless events.is_a?(Array)
  events.each { |event| validate_event(event) }

  payload = { events: events }.to_json
  response = client.post("api/v1/batch/events?#{URI.encode_www_form(default_params)}", payload)
  Bento::Response.new(response)
end

.track(email:, type:, fields: {}, details: {}) ⇒ Object

Track an event Usage examples:

Basic event: Bento::Events.track(email: ‘test@test.com’, type: ‘$completed_onboarding’)

Event with fields: Bento::Events.track(

email: 'test@test.com',
type: '$completed_onboarding',
fields: { first_name: 'Jesse', last_name: 'Pinkman' }

)

Complex event with fields and details: Bento::Events.track(

email: 'test@test.com',
type: '$purchase',
fields: { first_name: 'Jesse' },
details: {
  unique: { key: 'test123' },
  value: { currency: 'USD', amount: 8000 },
  cart: {
    items: [
      {
        product_sku: 'SKU123',
        product_name: 'Test',
        quantity: 100
      }
    ],
    abandoned_checkout_url: 'https://test.com'
  }
}

)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bento/resources/events.rb', line 39

def track(email:, type:, fields: {}, details: {})
  validate_email(email)
  validate_type(type)
  validate_fields(fields)
  validate_details(details)

  event = {
    email: email,
    type: type
  }
  event[:fields] = fields unless fields.empty?
  event[:details] = details unless details.empty?

  import([event])
end