Last active
August 31, 2023 17:41
-
-
Save jaredcwhite/4ec4d4b9c85f5616133cf0e3bdc99b16 to your computer and use it in GitHub Desktop.
Working with the ConvertKit API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ConvertkitFeatures | |
TAG_ID = 0000000 # redacted | |
FORM_ID = 0000000 # redacted | |
def api_secret = ENV.fetch("CONVERTKIT_API") | |
def conn | |
@conn ||= Faraday.new("https://api.convertkit.com/v3") do |f| | |
f.request :json | |
f.response :json | |
f.params = { api_secret: } | |
end | |
end | |
def find_subscriber(email_address) | |
conn.get("subscribers", { email_address: }) | |
end | |
def tag_subscriber(email) | |
conn.post("tags/#{TAG_ID}/subscribe", { email: }) | |
end | |
def new_subscriber(email, first_name) | |
# NOTE: this will trigger an opt-in confirmation email! | |
conn.post("forms/#{FORM_ID}/subscribe", { | |
email:, | |
first_name:, | |
tags: [TAG_ID] | |
}) | |
end | |
def tag_or_subscribe_user(user) | |
user.attributes.with_indifferent_access => { email:, first_name: } | |
if find_subscriber(email).body["total_subscribers"] > 0 | |
# already subscribed! go ahead and tag: | |
tag_subscriber email | |
else | |
# it's a new subscriber: | |
new_subscriber email, first_name | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Documentation: https://developers.convertkit.com