Class: Customerio::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/customerio/client.rb

Defined Under Namespace

Classes: MissingIdAttributeError, ParamError

Constant Summary collapse

DELIVERY_OPENED =
"opened"
DELIVERY_CLICKED =
"clicked"
DELIVERY_CONVERTED =
"converted"
DELIVERY_DELIVERED =
"delivered"
DELIVERY_BOUNCED =
"bounced"
DELIVERY_DEFERRED =
"deferred"
DELIVERY_DROPPED =
"dropped"
DELIVERY_SPAMMED =
"spammed"
VALID_DELIVERY_METRICS =
[
  DELIVERY_OPENED, DELIVERY_CLICKED, DELIVERY_CONVERTED,
  DELIVERY_DELIVERED, DELIVERY_BOUNCED, DELIVERY_DEFERRED,
  DELIVERY_DROPPED, DELIVERY_SPAMMED
].freeze
VALID_PUSH_EVENTS =
[DELIVERY_OPENED, DELIVERY_CONVERTED, DELIVERY_DELIVERED].freeze
PUSH_OPENED =
Deprecated.

Use DELIVERY_OPENED, DELIVERY_CONVERTED, DELIVERY_DELIVERED instead.

DELIVERY_OPENED
PUSH_CONVERTED =
DELIVERY_CONVERTED
PUSH_DELIVERED =
DELIVERY_DELIVERED

Instance Method Summary collapse

Constructor Details

#initialize(site_id, api_key, options = {}) ⇒ Client

Returns a new instance of Client.



38
39
40
41
42
43
44
45
46
47
# File 'lib/customerio/client.rb', line 38

def initialize(site_id, api_key, options = {})
  options = options.dup
  options[:region] = Regions::US if options[:region].nil?
  unless options[:region].is_a?(Regions::Region)
    raise ArgumentError, "region must be an instance of Customerio::Regions::Region"
  end

  options[:url] = options[:region].track_url if options[:url].nil? || options[:url].empty?
  @client = BaseClient.new({ site_id: site_id, api_key: api_key }, options)
end

Instance Method Details

#add_device(customer_id, device_id, platform, data = {}) ⇒ Object

Raises:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/customerio/client.rb', line 91

def add_device(customer_id, device_id, platform, data = {})
  raise ParamError, "customer_id must be a non-empty string" if empty?(customer_id)
  raise ParamError, "device_id must be a non-empty string" if empty?(device_id)
  raise ParamError, "platform must be a non-empty string" if empty?(platform)

  data = {} if data.nil?

  raise ParamError, "data parameter must be a hash" unless data.is_a?(Hash)

  @client.request_and_verify_response(
    :put,
    device_path(customer_id),
    device: data.merge(id: device_id, platform: platform)
  )
end

#batch(operations) ⇒ Object

Raises:



163
164
165
166
167
# File 'lib/customerio/client.rb', line 163

def batch(operations)
  raise ParamError, "operations must be a non-empty array" unless operations.is_a?(Array) && !operations.empty?

  @client.request_and_verify_response(:post, batch_path, batch: operations)
end

#delete(customer_id) ⇒ Object

Raises:



53
54
55
56
57
# File 'lib/customerio/client.rb', line 53

def delete(customer_id)
  raise ParamError, "customer_id must be a non-empty string" if empty?(customer_id)

  @client.request_and_verify_response(:delete, customer_path(customer_id))
end

#delete_device(customer_id, device_id) ⇒ Object

Raises:



107
108
109
110
111
112
# File 'lib/customerio/client.rb', line 107

def delete_device(customer_id, device_id)
  raise ParamError, "customer_id must be a non-empty string" if empty?(customer_id)
  raise ParamError, "device_id must be a non-empty string" if empty?(device_id)

  @client.request_and_verify_response(:delete, device_id_path(customer_id, device_id))
end

#identify(attributes) ⇒ Object



49
50
51
# File 'lib/customerio/client.rb', line 49

def identify(attributes)
  create_or_update(attributes)
end

#merge_customers(primary_id_type, primary_id, secondary_id_type, secondary_id) ⇒ Object

Raises:



152
153
154
155
156
157
158
159
160
161
# File 'lib/customerio/client.rb', line 152

def merge_customers(primary_id_type, primary_id, secondary_id_type, secondary_id)
  raise ParamError, "invalid primary_id_type" unless valid_id_type?(primary_id_type)
  raise ParamError, "primary_id must be a non-empty string" if empty?(primary_id)
  raise ParamError, "invalid secondary_id_type" unless valid_id_type?(secondary_id_type)
  raise ParamError, "secondary_id must be a non-empty string" if empty?(secondary_id)

  body = { primary: { primary_id_type => primary_id }, secondary: { secondary_id_type => secondary_id } }

  @client.request_and_verify_response(:post, merge_customers_path, body)
end

#pageview(customer_id, page, attributes = {}) ⇒ Object

Raises:



78
79
80
81
82
83
# File 'lib/customerio/client.rb', line 78

def pageview(customer_id, page, attributes = {})
  raise ParamError, "customer_id must be a non-empty string" if empty?(customer_id)
  raise ParamError, "page must be a non-empty string" if empty?(page)

  create_pageview_event(customer_id, page, attributes)
end

#suppress(customer_id) ⇒ Object

Raises:



59
60
61
62
63
# File 'lib/customerio/client.rb', line 59

def suppress(customer_id)
  raise ParamError, "customer_id must be a non-empty string" if empty?(customer_id)

  @client.request_and_verify_response(:post, suppress_path(customer_id))
end

#track(customer_id, event_name, attributes = {}, id: nil, timestamp: nil) ⇒ Object

Raises:



71
72
73
74
75
76
# File 'lib/customerio/client.rb', line 71

def track(customer_id, event_name, attributes = {}, id: nil, timestamp: nil)
  raise ParamError, "customer_id must be a non-empty string" if empty?(customer_id)
  raise ParamError, "event_name must be a non-empty string" if empty?(event_name)

  create_customer_event(customer_id, event_name, attributes, id: id, timestamp: timestamp)
end

#track_anonymous(anonymous_id, event_name, attributes = {}, id: nil, timestamp: nil) ⇒ Object

Raises:



85
86
87
88
89
# File 'lib/customerio/client.rb', line 85

def track_anonymous(anonymous_id, event_name, attributes = {}, id: nil, timestamp: nil)
  raise ParamError, "event_name must be a non-empty string" if empty?(event_name)

  create_anonymous_event(anonymous_id, event_name, attributes, id: id, timestamp: timestamp)
end

#track_delivery_metric(metric_name, attributes = {}) ⇒ Object

Raises:



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/customerio/client.rb', line 133

def track_delivery_metric(metric_name, attributes = {})
  keys = %i[delivery_id timestamp recipient reason href]
  attributes = symbolize_keys(attributes).slice(*keys)

  unless VALID_DELIVERY_METRICS.include?(metric_name)
    raise ParamError, "metric_name must be one of: #{VALID_DELIVERY_METRICS.join(', ')}"
  end

  raise ParamError, "delivery_id must be a non-empty string" if empty?(attributes[:delivery_id])

  body = { delivery_id: attributes[:delivery_id], metric: metric_name }
  body[:timestamp] = attributes[:timestamp] if valid_timestamp?(attributes[:timestamp])
  body[:recipient] = attributes[:recipient] unless empty?(attributes[:recipient])
  body[:reason] = attributes[:reason] unless empty?(attributes[:reason])
  body[:href] = attributes[:href] unless empty?(attributes[:href])

  @client.request_and_verify_response(:post, delivery_metrics_path, body)
end

#track_push_notification_event(event_name, attributes = {}) ⇒ Object

Raises:



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/customerio/client.rb', line 114

def track_push_notification_event(event_name, attributes = {})
  keys = %i[delivery_id device_id timestamp]
  attributes = symbolize_keys(attributes).slice(*keys)

  unless VALID_PUSH_EVENTS.include?(event_name)
    raise ParamError, "event_name must be one of opened, converted, or delivered"
  end

  raise ParamError, "delivery_id must be a non-empty string" if empty?(attributes[:delivery_id])
  raise ParamError, "device_id must be a non-empty string" if empty?(attributes[:device_id])
  raise ParamError, "timestamp must be a valid timestamp" unless valid_timestamp?(attributes[:timestamp])

  @client.request_and_verify_response(
    :post,
    track_push_notification_event_path,
    attributes.merge(event: event_name)
  )
end

#unsuppress(customer_id) ⇒ Object

Raises:



65
66
67
68
69
# File 'lib/customerio/client.rb', line 65

def unsuppress(customer_id)
  raise ParamError, "customer_id must be a non-empty string" if empty?(customer_id)

  @client.request_and_verify_response(:post, unsuppress_path(customer_id))
end