Skip to content

Instantly share code, notes, and snippets.

@jaredcwhite
Last active August 31, 2023 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredcwhite/4ec4d4b9c85f5616133cf0e3bdc99b16 to your computer and use it in GitHub Desktop.
Save jaredcwhite/4ec4d4b9c85f5616133cf0e3bdc99b16 to your computer and use it in GitHub Desktop.
Working with the ConvertKit API
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
@jaredcwhite
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment