Skip to content

Instantly share code, notes, and snippets.

@ridem
Last active November 18, 2023 21:41
Show Gist options
  • Select an option

  • Save ridem/2d511f3889edd3fe6ea8 to your computer and use it in GitHub Desktop.

Select an option

Save ridem/2d511f3889edd3fe6ea8 to your computer and use it in GitHub Desktop.

Revisions

  1. ridem renamed this gist Jul 24, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. ridem renamed this gist Jul 24, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. ridem renamed this gist Jul 24, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. ridem revised this gist Jul 24, 2015. 2 changed files with 15 additions and 9 deletions.
    22 changes: 14 additions & 8 deletions Readme.md
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,31 @@
    # Synchronize Shopify customers with Sendy
    # Synchronize Shopify customers with Sendy subscribers

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

    ## Installation

    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.
    (like `activerecord`, `shopify_api`, etc) and the right mysql packages for the activerecord adapter;

    - Put those three files in the same folder (for instance `/home/ubuntu/tasks/`)
    - 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 29. If you don't have any custom field on your sendy install, make it an empty string.
    - 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. hourly), you can set up a cron task:
    If you want to run this task periodically (e.g. every two hours), you can set up a cron task:
    - `crontab -e`
    - Delete the content and put this conent instead:
    - 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 * * * * ruby shopify_customers.rb > /dev/null 2>&1
    0 */2 * * * ruby shopify_customers.rb > /dev/null 2>&1
    ```
    2 changes: 1 addition & 1 deletion shopify_customers.rb
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@

    SHOPIFY_API_KEY = 'my-key' # To get an API Key, go to Apps > Private Apps
    SHOPIFY_PASSWORD = 'my-pass'
    STORE_NAME = 'my-app' # As in my-store.myshopify.com
    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:
  5. ridem revised this gist Jul 24, 2015. 2 changed files with 10 additions and 1 deletion.
    2 changes: 2 additions & 0 deletions Readme.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    # Synchronize Shopify customers with Sendy

    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 the right ruby install (with rbenv for instance), with the right gems installed
    9 changes: 8 additions & 1 deletion shopify_customers.rb
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@

    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
    STORE_NAME = 'my-app' # 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:
    @@ -37,6 +37,13 @@ class Subscriber < ActiveRecord::Base
    ## 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,
  6. ridem revised this gist Jul 24, 2015. 1 changed file with 4 additions and 7 deletions.
    11 changes: 4 additions & 7 deletions shopify_customers.rb
    Original file line number Diff line number Diff line change
    @@ -21,13 +21,12 @@
    ShopifyAPI::Base.site = shop_url

    class Subscriber < ActiveRecord::Base
    attr_accessible :name, :email, :join_date, :timestamp, :list, :last_campaign, :userID, :custom_fields
    validates_uniqueness_of :email
    attr_accessible :name, :email, :join_date, :timestamp, :list, :unsubscribed, :userID, :custom_fields
    end

    ShopifyAPI::Customer.all(from: :search, params: { q: 'accepts_marketing:true' }).each do |customer|
    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}"\
    @@ -38,13 +37,11 @@ class Subscriber < ActiveRecord::Base
    ## Make it an empty string if you don't have any

    subscriber = Subscriber.find_or_initialize_by(email: customer.email)

    subscriber.update name: name,
    email: customer.email,
    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)
    subscriber.save
    end
  7. ridem revised this gist Jul 24, 2015. 4 changed files with 14 additions and 12 deletions.
    8 changes: 5 additions & 3 deletions Readme.md
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,15 @@
    # Installation
    # Synchronize Shopify customers with Sendy

    ## Installation

    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.

    - Put those three files in the same folder (for instance `/home/ubuntu/tasks/`)
    - Edit them to match your config
    - Make sure you set the right `custom_fields` variable line 29
    - Make sure you set the right `custom_fields` variable line 29. If you don't have any custom field on your sendy install, make it an empty string.

    # Automation
    ## Automation

    If you want to run this task periodically (e.g. hourly), you can set up a cron task:
    - `crontab -e`
    8 changes: 4 additions & 4 deletions sendy_db.yml
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    adapter: mysql2
    host: localhost
    username: user
    password: pass
    database: database_name
    host: your_sendy_mysql_host
    username: your_sendy_mysql_user
    password: your_sendy_mysql_pass
    database: your_sendy_mysql_db_name
    3 changes: 0 additions & 3 deletions shopify_api.conf
    Original file line number Diff line number Diff line change
    @@ -1,3 +0,0 @@
    SHOPIFY_API_KEY = 'my-key'
    SHOPIFY_PASSWORD = 'my-pass'
    STORE_NAME = 'my-store'
    7 changes: 5 additions & 2 deletions shopify_customers.rb
    Original file line number Diff line number Diff line change
    @@ -5,16 +5,18 @@
    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

    dbconfig = YAML.load(File.open('sendy_db.yml'))
    ActiveRecord::Base.establish_connection(dbconfig)

    eval File.open('shopify_api.conf').read
    shop_url = "https://#{SHOPIFY_API_KEY}:#{SHOPIFY_PASSWORD}@#{STORE_NAME}.myshopify.com/admin"
    ShopifyAPI::Base.site = shop_url

    @@ -33,6 +35,7 @@ class Subscriber < ActiveRecord::Base
    "%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)

  8. ridem revised this gist Jul 24, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Readme.md
    Original file line number Diff line number Diff line change
    @@ -4,9 +4,9 @@ First make sure that you have the right ruby install (with rbenv for instance),
    (like `activerecord`, `shopify_api`, etc) and the right mysql packages.

    - Put those three files in the same folder (for instance `/home/ubuntu/tasks/`)
    - Edit them to match your config
    - Edit them to match your config
    - Make sure you set the right `custom_fields` variable line 29
    -

    # Automation

    If you want to run this task periodically (e.g. hourly), you can set up a cron task:
  9. ridem created this gist Jul 24, 2015.
    21 changes: 21 additions & 0 deletions Readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    # Installation

    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.

    - Put those three files in the same folder (for instance `/home/ubuntu/tasks/`)
    - Edit them to match your config
    - Make sure you set the right `custom_fields` variable line 29
    -
    # Automation

    If you want to run this task periodically (e.g. hourly), you can set up a cron task:
    - `crontab -e`
    - Delete the content and put this conent instead:

    ```
    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 * * * * ruby shopify_customers.rb > /dev/null 2>&1
    ```
    5 changes: 5 additions & 0 deletions sendy_db.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    adapter: mysql2
    host: localhost
    username: user
    password: pass
    database: database_name
    3 changes: 3 additions & 0 deletions shopify_api.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    SHOPIFY_API_KEY = 'my-key'
    SHOPIFY_PASSWORD = 'my-pass'
    STORE_NAME = 'my-store'
    47 changes: 47 additions & 0 deletions shopify_customers.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    require 'rubygems'
    require 'active_record'
    require 'protected_attributes'
    require 'shopify_api'
    require 'yaml'
    require 'mysql2'

    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

    dbconfig = YAML.load(File.open('sendy_db.yml'))
    ActiveRecord::Base.establish_connection(dbconfig)

    eval File.open('shopify_api.conf').read
    shop_url = "https://#{SHOPIFY_API_KEY}:#{SHOPIFY_PASSWORD}@#{STORE_NAME}.myshopify.com/admin"
    ShopifyAPI::Base.site = shop_url

    class Subscriber < ActiveRecord::Base
    attr_accessible :name, :email, :join_date, :timestamp, :list, :last_campaign, :userID, :custom_fields
    validates_uniqueness_of :email
    end

    ShopifyAPI::Customer.all(from: :search, params: { q: 'accepts_marketing:true' }).each do |customer|
    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

    subscriber = Subscriber.find_or_initialize_by(email: customer.email)

    subscriber.update name: name,
    email: customer.email,
    userID: SENDY_USER_ID,
    custom_fields: custom_fields,
    list: SENDY_LIST_ID,
    join_date: DateTime.parse(customer.created_at),
    timestamp: DateTime.parse(customer.updated_at)
    subscriber.save
    end