-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(CE): Added Mailchimp Destination Connector (#437)
* feat(CE): Added Mailchimp Destination Connector
- Loading branch information
Showing
11 changed files
with
526 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
141 changes: 141 additions & 0 deletions
141
integrations/lib/multiwoven/integrations/destination/mailchimp/client.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
# frozen_string_literal: true | ||
|
||
module Multiwoven | ||
module Integrations | ||
module Destination | ||
module Mailchimp | ||
include Multiwoven::Integrations::Core | ||
|
||
API_VERSION = "3.0" | ||
|
||
class Client < DestinationConnector | ||
prepend Multiwoven::Integrations::Core::RateLimiter | ||
|
||
def check_connection(connection_config) | ||
connection_config = connection_config.with_indifferent_access | ||
initialize_client(connection_config) | ||
authenticate_client | ||
success_status | ||
rescue StandardError => e | ||
failure_status(e) | ||
end | ||
|
||
def discover(_connection_config = nil) | ||
catalog = build_catalog(load_catalog) | ||
catalog.to_multiwoven_message | ||
rescue StandardError => e | ||
handle_exception(e, { | ||
context: "MAILCHIMP:DISCOVER:EXCEPTION", | ||
type: "error" | ||
}) | ||
end | ||
|
||
def write(sync_config, records, _action = "create") | ||
@sync_config = sync_config | ||
initialize_client(sync_config.destination.connection_specification) | ||
process_records(records, sync_config.stream) | ||
rescue StandardError => e | ||
handle_exception(e, { | ||
context: "MAILCHIMP:WRITE:EXCEPTION", | ||
type: "error", | ||
sync_id: @sync_config.sync_id, | ||
sync_run_id: @sync_config.sync_run_id | ||
}) | ||
end | ||
|
||
private | ||
|
||
def initialize_client(config) | ||
config = config.with_indifferent_access | ||
@client = MailchimpMarketing::Client.new | ||
@client.set_config({ | ||
api_key: config[:api_key], | ||
server: config[:api_key].split("-").last | ||
}) | ||
@list_id = config[:list_id] | ||
@email_template_id = config[:email_template_id] || "" | ||
end | ||
|
||
def process_records(records, stream) | ||
log_message_array = [] | ||
write_success = 0 | ||
write_failure = 0 | ||
properties = stream.json_schema[:properties] | ||
|
||
records.each do |record_object| | ||
record = extract_data(record_object, properties) | ||
args = [stream.name, "Id", record] | ||
begin | ||
response = send_to_mailchimp(record, stream.name) | ||
write_success += 1 | ||
log_message_array << log_request_response("info", args, response) | ||
rescue StandardError => e | ||
handle_exception(e, { | ||
context: "MAILCHIMP:WRITE:EXCEPTION", | ||
type: "error", | ||
sync_id: @sync_config.sync_id, | ||
sync_run_id: @sync_config.sync_run_id | ||
}) | ||
write_failure += 1 | ||
log_message_array << log_request_response("error", args, e.message) | ||
end | ||
end | ||
tracking_message(write_success, write_failure, log_message_array) | ||
end | ||
|
||
def send_to_mailchimp(record, stream_name) | ||
case stream_name | ||
when "Audience" | ||
@client.lists.set_list_member(@list_id, Digest::MD5.hexdigest(record[:email].downcase), { | ||
email_address: record[:email], | ||
status_if_new: "subscribed", | ||
merge_fields: { | ||
FNAME: record[:first_name], | ||
LNAME: record[:last_name] | ||
} | ||
}) | ||
when "Tags" | ||
@client.lists.update_list_member_tags(@list_id, Digest::MD5.hexdigest(record[:email].downcase), { | ||
tags: record[:tags].map { |tag| { name: tag, status: "active" } } | ||
}) | ||
when "Campaigns" | ||
campaign = @client.campaigns.create({ | ||
type: "regular", | ||
recipients: { list_id: @list_id }, | ||
settings: { | ||
subject_line: record[:subject], | ||
from_name: record[:from_name], | ||
reply_to: record[:reply_to] | ||
} | ||
}) | ||
if @email_template_id | ||
@client.campaigns.set_content(campaign["id"], { | ||
template: { id: @email_template_id } | ||
}) | ||
else | ||
@client.campaigns.set_content(campaign["id"], { | ||
plain_text: record[:content] | ||
}) | ||
end | ||
@client.campaigns.send(campaign["id"]) | ||
else | ||
raise "Unsupported stream type: #{stream_name}" | ||
end | ||
end | ||
|
||
def authenticate_client | ||
@client.lists.get_all_lists | ||
end | ||
|
||
def load_catalog | ||
read_json(CATALOG_SPEC_PATH) | ||
end | ||
|
||
def log_debug(message) | ||
Multiwoven::Integrations::Service.logger.debug(message) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
141 changes: 141 additions & 0 deletions
141
integrations/lib/multiwoven/integrations/destination/mailchimp/config/catalog.json
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
{ | ||
"request_rate_limit": 100000, | ||
"request_rate_limit_unit": "day", | ||
"request_rate_concurrency": 10, | ||
"streams": [ | ||
{ | ||
"name": "Audience", | ||
"action": "create", | ||
"json_schema": { | ||
"type": "object", | ||
"additionalProperties": true, | ||
"required": ["email"], | ||
"properties": { | ||
"email": { | ||
"type": "string", | ||
"format": "email" | ||
}, | ||
"first_name": { | ||
"type": "string" | ||
}, | ||
"last_name": { | ||
"type": "string" | ||
}, | ||
"status": { | ||
"type": "string", | ||
"enum": ["subscribed", "unsubscribed", "cleaned", "pending"] | ||
}, | ||
"tags": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
} | ||
}, | ||
"merge_fields": { | ||
"type": "object", | ||
"additionalProperties": true, | ||
"properties": { | ||
"FNAME": { | ||
"type": "string" | ||
}, | ||
"LNAME": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"language": { | ||
"type": "string" | ||
}, | ||
"vip": { | ||
"type": "boolean" | ||
}, | ||
"timestamp_signup": { | ||
"type": "string", | ||
"format": "date-time" | ||
}, | ||
"ip_signup": { | ||
"type": "string", | ||
"format": "ipv4" | ||
}, | ||
"timestamp_opt": { | ||
"type": "string", | ||
"format": "date-time" | ||
}, | ||
"ip_opt": { | ||
"type": "string", | ||
"format": "ipv4" | ||
} | ||
} | ||
}, | ||
"supported_sync_modes": ["incremental"], | ||
"source_defined_cursor": true, | ||
"default_cursor_field": ["timestamp_opt"], | ||
"source_defined_primary_key": [["email"]] | ||
}, | ||
{ | ||
"name": "Tags", | ||
"action": "create", | ||
"json_schema": { | ||
"type": "object", | ||
"additionalProperties": true, | ||
"required": ["email", "tags"], | ||
"properties": { | ||
"email": { | ||
"type": "string", | ||
"format": "email" | ||
}, | ||
"tags": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
}, | ||
"supported_sync_modes": ["incremental"], | ||
"source_defined_cursor": true, | ||
"default_cursor_field": ["updated"] | ||
}, | ||
{ | ||
"name": "Campaigns", | ||
"action": "create", | ||
"json_schema": { | ||
"type": "object", | ||
"additionalProperties": true, | ||
"required": ["subject", "from_name", "reply_to", "recipients"], | ||
"properties": { | ||
"subject": { | ||
"type": "string" | ||
}, | ||
"from_name": { | ||
"type": "string" | ||
}, | ||
"reply_to": { | ||
"type": "string", | ||
"format": "email" | ||
}, | ||
"recipients": { | ||
"type": "object", | ||
"properties": { | ||
"list_id": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"template_id": { | ||
"type": "string" | ||
}, | ||
"content": { | ||
"type": "string" | ||
}, | ||
"send_time": { | ||
"type": "string", | ||
"format": "date-time" | ||
} | ||
} | ||
}, | ||
"supported_sync_modes": ["full_refresh"], | ||
"source_defined_cursor": false | ||
} | ||
] | ||
} |
15 changes: 15 additions & 0 deletions
15
integrations/lib/multiwoven/integrations/destination/mailchimp/config/meta.json
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"data": { | ||
"name": "Mailchimp", | ||
"title": "Mailchimp", | ||
"connector_type": "destination", | ||
"category": "Marketing Automation", | ||
"documentation_url": "https://docs.multiwoven.com/destinations/crm/mailchimp", | ||
"github_issue_label": "destination-mailchimp", | ||
"icon": "icon.svg", | ||
"license": "MIT", | ||
"release_stage": "alpha", | ||
"support_level": "community", | ||
"tags": ["language:ruby", "multiwoven"] | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
integrations/lib/multiwoven/integrations/destination/mailchimp/config/spec.json
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"documentation_url": "https://docs.multiwoven.com/integrations/destination/mailchimp", | ||
"stream_type": "static", | ||
"connection_specification": { | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"title": "Mailchimp", | ||
"type": "object", | ||
"required": ["api_key", "list_id"], | ||
"properties": { | ||
"api_key": { | ||
"type": "string", | ||
"multiwoven_secret": true, | ||
"title": "API Key", | ||
"order": 0 | ||
}, | ||
"list_id": { | ||
"type": "string", | ||
"title": "List Id", | ||
"order": 1 | ||
}, | ||
"email_template_id": { | ||
"type": "string", | ||
"title": "Email Template Id", | ||
"order": 2 | ||
} | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
integrations/lib/multiwoven/integrations/destination/mailchimp/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.