Class: Klaviyo::Public

Inherits:
Client
  • Object
show all
Defined in:
lib/klaviyo/apis/public.rb

Constant Summary

Constants inherited from Client

Client::ALL, Client::BASE_API_URL, Client::CONTENT_JSON, Client::CONTENT_URL_FORM, Client::DEFAULT_COUNT, Client::DEFAULT_PAGE, Client::DEFAULT_SORT_DESC, Client::HTTP_DELETE, Client::HTTP_GET, Client::HTTP_POST, Client::HTTP_PUT, Client::KL_USER_AGENT, Client::KL_VERSION, Client::METRIC, Client::METRICS, Client::TIMELINE, Client::V1_API, Client::V2_API

Class Method Summary collapse

Class Method Details

.identify(kwargs = {}) ⇒ Object

Used for identifying customers and managing profile properties



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
# File 'lib/klaviyo/apis/public.rb', line 11

def self.identify(kwargs = {})
  defaults = {:id => nil,
              :email => nil,
              :phone_number => nil,
              :properties => {},
              :method => HTTP_GET,
              :token => nil
             }
  kwargs = defaults.merge(kwargs)

  unless check_required_args(kwargs)
    return
  end

  properties = kwargs[:properties]
  properties[:email] = kwargs[:email] unless kwargs[:email].to_s.empty?
  properties[:$phone_number] = kwargs[:phone_number] unless kwargs[:phone_number].to_s.empty?
  properties[:id] = kwargs[:id] unless kwargs[:id].to_s.empty?

  token = kwargs[:token] || Klaviyo.public_api_key || nil

  params = {
    :token => token,
    :properties => properties
  }

  public_request(kwargs[:method], 'identify', **params)
end

.track(event, kwargs = {}) ⇒ Object

Used for tracking events and customer behaviors

Parameters:

  • event (String)

    the event to track



51
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
83
84
85
# File 'lib/klaviyo/apis/public.rb', line 51

def self.track(event, kwargs = {})
  defaults = {
    :id => nil,
    :email => nil,
    :phone_number => nil,
    :properties => {},
    :customer_properties => {},
    :time => nil,
    :method => HTTP_GET,
    :token => nil
  }

  kwargs = defaults.merge(kwargs)

  unless check_required_args(kwargs)
    return
  end

  customer_properties = kwargs[:customer_properties]
  customer_properties[:email] = kwargs[:email] unless kwargs[:email].to_s.empty?
  customer_properties[:$phone_number] = kwargs[:phone_number] unless kwargs[:phone_number].to_s.empty?
  customer_properties[:id] = kwargs[:id] unless kwargs[:id].to_s.empty?

  token = kwargs[:token] || Klaviyo.public_api_key || nil

  params = {
    :token => token,
    :event => event,
    :properties => kwargs[:properties],
    :customer_properties => customer_properties
  }
  params[:time] = kwargs[:time] if kwargs[:time]

  public_request(kwargs[:method], 'track', **params)
end

.track_once(event, kwargs = {}) ⇒ Object



87
88
89
90
# File 'lib/klaviyo/apis/public.rb', line 87

def self.track_once(event, kwargs = {})
  kwargs.update('__track_once__' => true)
  track(event, kwargs)
end