Skip to content

Instantly share code, notes, and snippets.

Last active November 18, 2023 21:41
Show Gist options
  • Save ridem/2d511f3889edd3fe6ea8 to your computer and use it in GitHub Desktop.
Save ridem/2d511f3889edd3fe6ea8 to your computer and use it in GitHub Desktop.
Synchronize Shopify customers with Sendy

Synchronize Shopify customers with Sendy subscribers

Sendy woudln't be the Mailchimp killer without a proper Shopify integration. There we go, thanks to the Shopify gem and some ActiveRecord awesomeness.

NB: The script always keep in sync Shopify's accepts_marketing field with Sendy's unsubscribed field. I added custom fields like country and order count as an example of what we can do with it.


First make sure that you have the right ruby install (with rbenv for instance), with the right gems installed (like activerecord, shopify_api, etc) and the right mysql packages for the activerecord adapter;

  • Put those two files in the same folder, from which you will need to run the script (for instance /home/ubuntu/tasks/)
  • Edit them to match your config
  • Make sure you set the right custom_fields variable line 30. If you don't have any custom field on your sendy install, make it an empty string.


Simply run ruby shopify_customers.rb


If you want to run this task periodically (e.g. every two hours), you can set up a cron task:

  • crontab -e
  • Add those lines (note that we update the PATH to get the right ruby environment - that one corresponds to a standard rbenv install):

0 */2 * * * ruby shopify_customers.rb > /dev/null 2>&1
adapter: mysql2
host: your_sendy_mysql_host
username: your_sendy_mysql_user
password: your_sendy_mysql_pass
database: your_sendy_mysql_db_name
require 'rubygems'
require 'active_record'
require 'protected_attributes'
require 'shopify_api'
require 'yaml'
require 'mysql2'
SHOPIFY_API_KEY = 'my-key' # To get an API Key, go to Apps > Private Apps
STORE_NAME = 'my-store' # As in
# This has to be the send list ID from the database, that you can grab by going to the sendy list page:
# http://my-sendy-install/subscribers?i=1&l=4 for SENDY_LIST_ID = 4
SENDY_USER_ID = 1 # You probably don't want to change this
dbconfig = YAML.load('sendy_db.yml'))
shop_url = "https://#{SHOPIFY_API_KEY}:#{SHOPIFY_PASSWORD}@#{STORE_NAME}" = shop_url
class Subscriber < ActiveRecord::Base
attr_accessible :name, :email, :join_date, :timestamp, :list, :unsubscribed, :userID, :custom_fields
ShopifyAPI::Customer.all.each do |customer| ## Unfortunately they don't support find_each
name = "#{customer.first_name} #{customer.last_name}"
custom_fields = "#{customer.first_name}"\
"%s%#{customer.default_address? ? : ''}"
## The order of the custom fields has to be the same than the order in which you created them
## Make it an empty string if you don't have any
subscriber = Subscriber.find_or_initialize_by(email:
## If the user was resubscribed/unsubscribed in Sendy, we keep Shopify informed.
if subscriber.timestamp? && subscriber.timestamp > DateTime.parse(customer.updated_at).to_i
customer.accepts_marketing = (subscriber.unsubscribed == 0)
subscriber.update name: name,
custom_fields: custom_fields,
unsubscribed: customer.accepts_marketing ? 0 : 1,
join_date: DateTime.parse(customer.created_at),
timestamp: DateTime.parse(customer.updated_at)
Copy link

tmushy commented Dec 2, 2016

Thank you for your script. Does this work with ruby 2.2? I cant seem to get it to work

Thank you

Copy link

Hi ! I'm really interested in this but i'm not ruby dev, i have this error :

Unable to activate protected_attributes-1.1.3, because activemodel-5.0.1 conflicts with activemodel (< 5.0, >= 4.0.1) (Gem::ConflictError)

can you help ?

Thank you !

Copy link

does this still work? i see there's a zapier plugin I could use..

Copy link

Still working? Would love to try this as Shopify in Zapier is "Premium Only"

Copy link

Hi, it is a couple of years since the last comment and wondering whether anyone is still using this. I'd be interested if there is anyone who knows of a Zap way to do it as the coding is a bit beyond me. Or anyone who can help me with the coding and how to do it in Shopify.

We have Sendy set up, so half way there.

Copy link

This still works in principle. I made a couple modifications for more modern installs of Ruby and Shopify but all of the core ideas from this one still work. As the original poster says, you'll have to have a server you can control with Ruby installed and possibly setup a cron job if you want this to run automatically. My forked version of this script is here:

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