Skip to content

Instantly share code, notes, and snippets.

@raafirivero
Forked from ridem/-Shopify-Sendy.md
Last active July 15, 2021 06:12
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 raafirivero/1aeb97d5016e458d82d729f6e50d44e1 to your computer and use it in GitHub Desktop.
Save raafirivero/1aeb97d5016e458d82d729f6e50d44e1 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. Here's a fork of ridem's Shopify gem, updated for a modern install of Ruby.

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.

Installation

First make sure that you have Ruby installed with the following gems: activerecord, protected_attributes_continued, shopify_api, yaml, mysql2 If you install them with RVM then you'll get the needed depenedencies as well.

  • 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.

Run

Simply run ruby shopify_customers.rb

Automation

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):
HOME=/home/ubuntu/tasks/
PATH=/home/ubuntu/.rbenv/plugins/ruby-build/bin:/home/ubuntu/.rbenv/shims:/home/ubuntu/.rbenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

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_continued'
require 'shopify_api'
require 'yaml'
require 'mysql2'
SHOPIFY_API_KEY = 'my-key' # To get an API Key, go to Apps > Private Apps
SHOPIFY_PASSWORD = 'my-pass'
STORE_NAME = 'my-store' # As in my-store.myshopify.com
SENDY_LIST_ID = 4 ## CHANGE ME!
# 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
# Note: it might be useful to use a full path in the line below -- something like /home/mysite/tasks/sendy_db.yml
dbconfig = YAML.load(File.open('sendy_db.yml'))
ActiveRecord::Base.establish_connection(dbconfig)
shop_url = "https://#{SHOPIFY_API_KEY}:#{SHOPIFY_PASSWORD}@#{STORE_NAME}.myshopify.com/admin"
ShopifyAPI::Base.site = shop_url
ShopifyAPI::Base.api_version = '2021-07'
# find the latest stable api_version here: https://shopify.dev/concepts/about-apis/versioning
class Subscriber < ActiveRecord::Base
attr_accessible :name, :email, :join_date, :timestamp, :list, :unsubscribed, :userID, :custom_fields
end
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.last_name}"\
"%s%#{customer.orders_count}"\
"%s%#{customer.total_spent}"\
"%s%#{customer.last_order_name}"\
"%s%#{customer.default_address? ? customer.default_address.country : ''}"
## 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: customer.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)
customer.save
end
subscriber.update name: name,
userID: SENDY_USER_ID,
custom_fields: custom_fields,
list: SENDY_LIST_ID,
unsubscribed: customer.accepts_marketing ? 0 : 1,
join_date: DateTime.parse(customer.created_at),
timestamp: DateTime.parse(customer.updated_at)
end
@raafirivero
Copy link
Author

raafirivero commented Jul 14, 2021

minimal changes from the original:
changed line 3 above to: require 'protected_attributes_continued' so that it was compatible with my version of Ruby
and the Shopify API now requires you to declare a version. Do that at line 22 like this: ShopifyAPI::Base.api_version = '2021-07' # find the latest stable api_version here: https://shopify.dev/concepts/about-apis/versioning

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