klaviyo 2.0.3 → 2.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/klaviyo.gemspec +2 -2
- data/lib/klaviyo/apis/data_privacy.rb +19 -0
- data/lib/klaviyo/apis/email_templates.rb +97 -0
- data/lib/klaviyo/apis/lists.rb +9 -9
- data/lib/klaviyo/apis/metrics.rb +4 -6
- data/lib/klaviyo/apis/profiles.rb +16 -3
- data/lib/klaviyo/apis/public.rb +3 -3
- data/lib/klaviyo/client.rb +35 -16
- data/lib/klaviyo/klaviyo_module.rb +3 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b02aaf0e72a06b6d2bf999e9279a8b6c20c0f4f7652275fea1981ea0ab0683c5
|
4
|
+
data.tar.gz: 4b2ac021105853944c5bdbf47efa268addda7b1006f2683be04cadefb4db7ecc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 561d598fa79624b0a2e271efb64676d55f39f09ca4ee787097061006f865f08f14d12faa49da7eea2a28523c3c0d96ed727feff2b40924b3965b1b1b234fb727
|
7
|
+
data.tar.gz: 934fe573a195fa8e307fc1c4383f810965d0b2636e81d8a442ece9c5b613ddef4d3eb3a1d5a159a82328edbb12cd0c87ab071a27ce5d7d57f2e8d7060a29433b
|
data/klaviyo.gemspec
CHANGED
@@ -2,8 +2,8 @@ files = ['klaviyo.gemspec', '{lib}/**/**/*'].map {|f| Dir[f]}.flatten
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = 'klaviyo'
|
5
|
-
s.version = '2.0.
|
6
|
-
s.date = '2021-
|
5
|
+
s.version = '2.0.8'
|
6
|
+
s.date = '2021-04-28'
|
7
7
|
s.summary = 'You heard us, a Ruby wrapper for the Klaviyo API'
|
8
8
|
s.description = 'Ruby wrapper for the Klaviyo API'
|
9
9
|
s.authors = ['Klaviyo Team']
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Klaviyo
|
2
|
+
class DataPrivacy < Client
|
3
|
+
DATA_PRIVACY = 'data-privacy'
|
4
|
+
DELETION_REQUEST = 'deletion-request'
|
5
|
+
|
6
|
+
# Submits a data privacy-related deletion request
|
7
|
+
# @param id_type [String] 'email' or 'phone_number' or 'person_id
|
8
|
+
# @param identifier [String] value for the identifier specified
|
9
|
+
# @return a dictionary with a confirmation that deletion task submitted for the customer
|
10
|
+
def self.request_profile_deletion(id_type, identifier)
|
11
|
+
unless ['email', 'phone_number', 'person_id'].include? id_type
|
12
|
+
raise Klaviyo::KlaviyoError.new(INVALID_ID_TYPE_ERROR)
|
13
|
+
end
|
14
|
+
data = Hash[id_type.to_sym, identifier]
|
15
|
+
path = "#{DATA_PRIVACY}/#{DELETION_REQUEST}"
|
16
|
+
v2_request(HTTP_POST, path, **data)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
module Klaviyo
|
2
|
+
class EmailTemplates < Client
|
3
|
+
EMAIL_TEMPLATES = 'email-templates'
|
4
|
+
EMAIL_TEMPLATE = 'email-template'
|
5
|
+
CLONE = 'clone'
|
6
|
+
RENDER = 'render'
|
7
|
+
SEND = 'send'
|
8
|
+
|
9
|
+
# Returns a list of all the email templates you've created.
|
10
|
+
# The templates are returned in sorted order by name.
|
11
|
+
# @return [List] of JSON formatted email template objects
|
12
|
+
def self.get_templates()
|
13
|
+
v1_request(HTTP_GET, EMAIL_TEMPLATES)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Creates a new email template
|
17
|
+
# @param :name [String] The name of the email template
|
18
|
+
# @param :html [String] The HTML content for this template
|
19
|
+
# @return [JSON] a JSON object containing information about the email template
|
20
|
+
def self.create_template(name: nil, html: nil)
|
21
|
+
params = {
|
22
|
+
name: name,
|
23
|
+
html: html
|
24
|
+
}
|
25
|
+
v1_request(HTTP_POST, EMAIL_TEMPLATES, content_type: CONTENT_URL_FORM, params: params)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Updates the name and/or HTML content of a template. Only updates imported
|
29
|
+
# HTML templates; does not currently update drag & drop templates
|
30
|
+
# @param template_id [String] The id of the email template
|
31
|
+
# @param :name [String] The name of the email template
|
32
|
+
# @param :html [String] The HTML content for this template
|
33
|
+
# @return [JSON] a JSON object containing information about the email template
|
34
|
+
def self.update_template(template_id, name:, html:)
|
35
|
+
path = "#{EMAIL_TEMPLATE}/#{template_id}"
|
36
|
+
params = {
|
37
|
+
name: name,
|
38
|
+
html: html
|
39
|
+
}
|
40
|
+
v1_request(HTTP_PUT, path, **params)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Deletes a given template.
|
44
|
+
# @param template_id [String] The id of the email template
|
45
|
+
# @return [JSON] a JSON object containing information about the email template
|
46
|
+
def self.delete_template(template_id)
|
47
|
+
path = "#{EMAIL_TEMPLATE}/#{template_id}"
|
48
|
+
v1_request(HTTP_DELETE, path)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Creates a copy of a given template with a new name
|
52
|
+
# @param template_id [String] The id of the email template to copy
|
53
|
+
# @param :name [String] The name of the newly cloned email template
|
54
|
+
# @return [JSON] a JSON object containing information about the email template
|
55
|
+
def self.clone_template(template_id, name:)
|
56
|
+
path = "#{EMAIL_TEMPLATE}/#{template_id}/#{CLONE}"
|
57
|
+
params = {
|
58
|
+
name: name
|
59
|
+
}
|
60
|
+
v1_request(HTTP_POST, path, content_type: CONTENT_URL_FORM, params: params)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Renders the specified template with the provided data and return HTML
|
64
|
+
# and text versions of the email
|
65
|
+
# @param template_id [String] The id of the email template to copy
|
66
|
+
# @param :context [Hash] The context the email template will be rendered with
|
67
|
+
# @return [JSON] a JSON object containing information about the email template
|
68
|
+
def self.render_template(template_id, context: {})
|
69
|
+
path = "#{EMAIL_TEMPLATE}/#{template_id}/#{RENDER}"
|
70
|
+
params = {
|
71
|
+
context: context
|
72
|
+
}
|
73
|
+
v1_request(HTTP_POST, path, content_type: CONTENT_URL_FORM, params: params)
|
74
|
+
end
|
75
|
+
|
76
|
+
# Renders the specified template with the provided data and then send the
|
77
|
+
# contents in an email via the service specified
|
78
|
+
# @param template_id [String] The id of the email template to copy
|
79
|
+
# @param :from_email [String] The from email address; used in the reply-to header
|
80
|
+
# @param :from_name [String] The name the email is sent from
|
81
|
+
# @param :subject [String] The subject of the email template
|
82
|
+
# @param :to [Mixed] The email this template is being sent to
|
83
|
+
# @param :context [Hash] The context the email template will be rendered with
|
84
|
+
# @return [JSON] a JSON object containing information about the email template
|
85
|
+
def self.send_template(template_id, from_email:, from_name:, subject:, to:, context: {})
|
86
|
+
path = "#{EMAIL_TEMPLATE}/#{template_id}/#{SEND}"
|
87
|
+
params = {
|
88
|
+
from_email: from_email,
|
89
|
+
from_name: from_name,
|
90
|
+
subject: subject,
|
91
|
+
to: to,
|
92
|
+
context: context
|
93
|
+
}
|
94
|
+
v1_request(HTTP_POST, path, content_type: CONTENT_URL_FORM, params: params)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
data/lib/klaviyo/apis/lists.rb
CHANGED
@@ -14,7 +14,7 @@ module Klaviyo
|
|
14
14
|
body = {
|
15
15
|
:list_name => list_name
|
16
16
|
}
|
17
|
-
v2_request(HTTP_POST, LISTS, body)
|
17
|
+
v2_request(HTTP_POST, LISTS, **body)
|
18
18
|
end
|
19
19
|
|
20
20
|
# Retrieves all the lists in the Klaviyo account
|
@@ -40,7 +40,7 @@ module Klaviyo
|
|
40
40
|
body = {
|
41
41
|
:list_name => list_name
|
42
42
|
}
|
43
|
-
v2_request(HTTP_PUT, path, body)
|
43
|
+
v2_request(HTTP_PUT, path, **body)
|
44
44
|
end
|
45
45
|
|
46
46
|
# Deletes a list
|
@@ -65,7 +65,7 @@ module Klaviyo
|
|
65
65
|
:phone_numbers => phone_numbers,
|
66
66
|
:push_tokens => push_tokens
|
67
67
|
}
|
68
|
-
v2_request(HTTP_GET, path, params)
|
68
|
+
v2_request(HTTP_GET, path, **params)
|
69
69
|
end
|
70
70
|
|
71
71
|
# Subscribe profiles to a list.
|
@@ -80,7 +80,7 @@ module Klaviyo
|
|
80
80
|
params = {
|
81
81
|
:profiles => profiles
|
82
82
|
}
|
83
|
-
v2_request(HTTP_POST, path, params)
|
83
|
+
v2_request(HTTP_POST, path, **params)
|
84
84
|
end
|
85
85
|
|
86
86
|
# Unsubscribe and remove profiles from a list
|
@@ -92,7 +92,7 @@ module Klaviyo
|
|
92
92
|
params = {
|
93
93
|
:emails => emails
|
94
94
|
}
|
95
|
-
v2_request(HTTP_DELETE, path, params)
|
95
|
+
v2_request(HTTP_DELETE, path, **params)
|
96
96
|
end
|
97
97
|
|
98
98
|
# Add profiles to a list
|
@@ -106,7 +106,7 @@ module Klaviyo
|
|
106
106
|
params = {
|
107
107
|
:profiles => profiles
|
108
108
|
}
|
109
|
-
v2_request(HTTP_POST, path, params)
|
109
|
+
v2_request(HTTP_POST, path, **params)
|
110
110
|
end
|
111
111
|
|
112
112
|
# Check if profiles are on a list
|
@@ -123,7 +123,7 @@ module Klaviyo
|
|
123
123
|
:phone_numbers => phone_numbers,
|
124
124
|
:push_tokens => push_tokens
|
125
125
|
}
|
126
|
-
v2_request(HTTP_GET, path, params)
|
126
|
+
v2_request(HTTP_GET, path, **params)
|
127
127
|
end
|
128
128
|
|
129
129
|
# Remove profiles from a list
|
@@ -139,7 +139,7 @@ module Klaviyo
|
|
139
139
|
:phone_numbers => phone_numbers,
|
140
140
|
:push_tokens => push_tokens
|
141
141
|
}
|
142
|
-
v2_request(HTTP_DELETE, path, params)
|
142
|
+
v2_request(HTTP_DELETE, path, **params)
|
143
143
|
end
|
144
144
|
|
145
145
|
# Get all emails, phone numbers, along with reasons for list exclusion
|
@@ -151,7 +151,7 @@ module Klaviyo
|
|
151
151
|
params = {
|
152
152
|
:marker => marker
|
153
153
|
}
|
154
|
-
v2_request(HTTP_GET, path, params)
|
154
|
+
v2_request(HTTP_GET, path, **params)
|
155
155
|
end
|
156
156
|
|
157
157
|
# Get all of the emails, phone numbers, and push tokens for profiles in a given list or segment
|
data/lib/klaviyo/apis/metrics.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
module Klaviyo
|
2
2
|
class Metrics < Client
|
3
3
|
EXPORT = 'export'
|
4
|
-
METRIC = 'metric'
|
5
|
-
METRICS = 'metrics'
|
6
4
|
|
7
5
|
# Returns a list of all metrics in Klaviyo
|
8
6
|
# @param page [Integer] which page to return, default 0
|
@@ -13,7 +11,7 @@ module Klaviyo
|
|
13
11
|
:page => page,
|
14
12
|
:count => count
|
15
13
|
}
|
16
|
-
v1_request(HTTP_GET, METRICS, params)
|
14
|
+
v1_request(HTTP_GET, METRICS, **params)
|
17
15
|
end
|
18
16
|
|
19
17
|
# Returns a batched timeline of all events in your Klaviyo account.
|
@@ -28,7 +26,7 @@ module Klaviyo
|
|
28
26
|
:count => count,
|
29
27
|
:sort => sort
|
30
28
|
}
|
31
|
-
v1_request(HTTP_GET, path, params)
|
29
|
+
v1_request(HTTP_GET, path, **params)
|
32
30
|
end
|
33
31
|
|
34
32
|
# Returns a batched timeline for one specific type of metric.
|
@@ -44,7 +42,7 @@ module Klaviyo
|
|
44
42
|
:count => count,
|
45
43
|
:sort => sort
|
46
44
|
}
|
47
|
-
v1_request(HTTP_GET, path, params)
|
45
|
+
v1_request(HTTP_GET, path, **params)
|
48
46
|
end
|
49
47
|
|
50
48
|
# Export event data, optionally filtering and segmented on available event properties
|
@@ -76,7 +74,7 @@ module Klaviyo
|
|
76
74
|
:by => by,
|
77
75
|
:count => count
|
78
76
|
}
|
79
|
-
v1_request(HTTP_GET, path, params)
|
77
|
+
v1_request(HTTP_GET, path, **params)
|
80
78
|
end
|
81
79
|
end
|
82
80
|
end
|
@@ -1,6 +1,19 @@
|
|
1
1
|
module Klaviyo
|
2
2
|
class Profiles < Client
|
3
3
|
PERSON = 'person'
|
4
|
+
PEOPLE = 'people'
|
5
|
+
SEARCH = 'search'
|
6
|
+
|
7
|
+
# Retrieves the id of the profile given email
|
8
|
+
# @param email [String] the email of the profile
|
9
|
+
# @return [JSON] a JSON object containing id of the profile
|
10
|
+
def self.get_profile_id_by_email(email)
|
11
|
+
path = "#{PEOPLE}/#{SEARCH}"
|
12
|
+
params = {
|
13
|
+
:email => email
|
14
|
+
}
|
15
|
+
v2_request(HTTP_GET, path, **params)
|
16
|
+
end
|
4
17
|
|
5
18
|
# Retrieve all the data attributes for a Klaviyo Person ID.
|
6
19
|
# @param person_id [String] the id of the profile
|
@@ -16,7 +29,7 @@ module Klaviyo
|
|
16
29
|
# @return returns the updated person object
|
17
30
|
def self.update_person_attributes(person_id, kwargs = {})
|
18
31
|
path = "#{PERSON}/#{person_id}"
|
19
|
-
v1_request(HTTP_PUT, path, kwargs)
|
32
|
+
v1_request(HTTP_PUT, path, **kwargs)
|
20
33
|
end
|
21
34
|
|
22
35
|
# Listing a person's event timeline
|
@@ -32,7 +45,7 @@ module Klaviyo
|
|
32
45
|
:count => count,
|
33
46
|
:sort => sort
|
34
47
|
}
|
35
|
-
v1_request(HTTP_GET, path, params)
|
48
|
+
v1_request(HTTP_GET, path, **params)
|
36
49
|
end
|
37
50
|
|
38
51
|
# Listing a person's event timeline for a particular metric
|
@@ -49,7 +62,7 @@ module Klaviyo
|
|
49
62
|
:count => count,
|
50
63
|
:sort => sort
|
51
64
|
}
|
52
|
-
v1_request(HTTP_GET, path, params)
|
65
|
+
v1_request(HTTP_GET, path, **params)
|
53
66
|
end
|
54
67
|
end
|
55
68
|
end
|
data/lib/klaviyo/apis/public.rb
CHANGED
@@ -27,7 +27,7 @@ module Klaviyo
|
|
27
27
|
:properties => properties
|
28
28
|
}
|
29
29
|
|
30
|
-
public_request(HTTP_GET, 'identify', params)
|
30
|
+
public_request(HTTP_GET, 'identify', **params)
|
31
31
|
end
|
32
32
|
|
33
33
|
# Used for tracking events and customer behaviors
|
@@ -66,9 +66,9 @@ module Klaviyo
|
|
66
66
|
:properties => kwargs[:properties],
|
67
67
|
:customer_properties => customer_properties
|
68
68
|
}
|
69
|
-
params[:time] = kwargs[:time]
|
69
|
+
params[:time] = kwargs[:time] if kwargs[:time]
|
70
70
|
|
71
|
-
public_request(HTTP_GET, 'track', params)
|
71
|
+
public_request(HTTP_GET, 'track', **params)
|
72
72
|
end
|
73
73
|
|
74
74
|
def self.track_once(event, kwargs = {})
|
data/lib/klaviyo/client.rb
CHANGED
@@ -10,57 +10,76 @@ module Klaviyo
|
|
10
10
|
HTTP_PUT = 'put'
|
11
11
|
|
12
12
|
ALL = 'all'
|
13
|
+
METRIC = 'metric'
|
14
|
+
METRICS = 'metrics'
|
13
15
|
TIMELINE = 'timeline'
|
14
16
|
|
15
17
|
DEFAULT_COUNT = 100
|
16
18
|
DEFAULT_PAGE = 0
|
17
19
|
DEFAULT_SORT_DESC = 'desc'
|
18
20
|
|
21
|
+
CONTENT_JSON = 'application/json'
|
22
|
+
CONTENT_URL_FORM = 'application/x-www-form-urlencoded'
|
23
|
+
|
19
24
|
private
|
20
25
|
|
21
|
-
def self.request(method, path, kwargs
|
26
|
+
def self.request(method, path, content_type, **kwargs)
|
22
27
|
check_private_api_key_exists()
|
23
28
|
url = "#{BASE_API_URL}/#{path}"
|
24
29
|
connection = Faraday.new(
|
25
30
|
url: url,
|
26
31
|
headers: {
|
27
|
-
'Content-Type' =>
|
32
|
+
'Content-Type' => content_type
|
28
33
|
})
|
34
|
+
if content_type == CONTENT_JSON
|
35
|
+
kwargs[:body] = kwargs[:body].to_json
|
36
|
+
end
|
29
37
|
response = connection.send(method) do |req|
|
30
|
-
req.body = kwargs[:body]
|
38
|
+
req.body = kwargs[:body] || nil
|
31
39
|
end
|
32
40
|
end
|
33
41
|
|
34
|
-
def self.public_request(method, path, kwargs
|
42
|
+
def self.public_request(method, path, **kwargs)
|
35
43
|
check_public_api_key_exists()
|
36
44
|
params = build_params(kwargs)
|
37
45
|
url = "#{BASE_API_URL}/#{path}?#{params}"
|
38
46
|
res = Faraday.get(url).body
|
39
47
|
end
|
40
48
|
|
41
|
-
def self.v1_request(method, path,
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
49
|
+
def self.v1_request(method, path, content_type: CONTENT_JSON, **kwargs)
|
50
|
+
if content_type == CONTENT_URL_FORM
|
51
|
+
data = {
|
52
|
+
:body => {
|
53
|
+
:api_key => Klaviyo.private_api_key
|
54
|
+
}
|
55
|
+
}
|
56
|
+
data[:body] = data[:body].merge(kwargs[:params])
|
57
|
+
full_url = "#{V1_API}/#{path}"
|
58
|
+
request(method, full_url, content_type, **data)
|
59
|
+
else
|
60
|
+
defaults = {:page => nil,
|
61
|
+
:count => nil,
|
62
|
+
:since => nil,
|
63
|
+
:sort => nil}
|
64
|
+
params = defaults.merge(kwargs)
|
65
|
+
query_params = encode_params(params)
|
66
|
+
full_url = "#{V1_API}/#{path}?api_key=#{Klaviyo.private_api_key}#{query_params}"
|
67
|
+
request(method, full_url, content_type)
|
68
|
+
end
|
50
69
|
end
|
51
70
|
|
52
|
-
def self.v2_request(method, path, kwargs
|
71
|
+
def self.v2_request(method, path, **kwargs)
|
53
72
|
path = "#{V2_API}/#{path}"
|
54
73
|
key = {
|
55
74
|
"api_key": "#{Klaviyo.private_api_key}"
|
56
75
|
}
|
57
76
|
data = {}
|
58
77
|
data[:body] = key.merge(kwargs)
|
59
|
-
request(method, path, data)
|
78
|
+
request(method, path, CONTENT_JSON, **data)
|
60
79
|
end
|
61
80
|
|
62
81
|
def self.build_params(params)
|
63
|
-
"data=#{Base64.encode64(JSON.generate(params)).gsub(/\n/,'')}"
|
82
|
+
"data=#{CGI.escape(Base64.encode64(JSON.generate(params)).gsub(/\n/, ''))}"
|
64
83
|
end
|
65
84
|
|
66
85
|
def self.check_required_args(kwargs)
|
@@ -9,6 +9,8 @@ require_relative 'apis/lists'
|
|
9
9
|
require_relative 'apis/metrics'
|
10
10
|
require_relative 'apis/profiles'
|
11
11
|
require_relative 'apis/campaigns'
|
12
|
+
require_relative 'apis/email_templates'
|
13
|
+
require_relative 'apis/data_privacy'
|
12
14
|
|
13
15
|
module Klaviyo
|
14
16
|
class << self
|
@@ -21,4 +23,5 @@ module Klaviyo
|
|
21
23
|
NO_PRIVATE_API_KEY_ERROR = 'Please provide your Private API key for this request'
|
22
24
|
NO_PUBLIC_API_KEY_ERROR = 'Please provide your Public API key for this request'
|
23
25
|
REQUIRED_ARG_ERROR = 'You must identify a user by email, ID or phone_number'
|
26
|
+
INVALID_ID_TYPE_ERROR = 'Invalid id_type provided, must be one of: email, phone_number, person_id'
|
24
27
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: klaviyo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Klaviyo Team
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-04-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -75,6 +75,8 @@ files:
|
|
75
75
|
- klaviyo.gemspec
|
76
76
|
- lib/klaviyo.rb
|
77
77
|
- lib/klaviyo/apis/campaigns.rb
|
78
|
+
- lib/klaviyo/apis/data_privacy.rb
|
79
|
+
- lib/klaviyo/apis/email_templates.rb
|
78
80
|
- lib/klaviyo/apis/lists.rb
|
79
81
|
- lib/klaviyo/apis/metrics.rb
|
80
82
|
- lib/klaviyo/apis/profiles.rb
|