Automated Marketing with Ruby on Rails

Tressa Sanders
8 min readMar 14, 2018

--

Do you have a sweet Ruby on Rails web application that could use some automation? Even if it’s not sweet, this is the article for you.

The automation in this article is not for the purposes of keeping you from engaging with your audience. It is to save you some time and trouble by helping you:

  1. Publish your marketing content on a set schedule even if you create the content in batches ahead of time.

I’m going to assume you have a content marketing plan and that with this plan, you have a posting schedule. For example, the marketing plan for StackEnglish.com is to post an article three times a week and a video two times a week. That is a lot of content to create when you are just starting out. However it must be done and to make my life easier, it is better to create the articles in batches for the month and the videos in batches for the month.

Once the content is created, it can be added to the website using the admin panel. There it will remain hidden to the public and unpublished. So this article will show you how to publish this pre-loaded content on the schedule you have set for your content without having to go back into your admin panel and manually publish it.

2. Post “staple” marketing content whenever you like and as often as you need.

So what is “staple” marketing content? It is one-off content that never changes like landing pages to sign up for your mailing list or a service that you offer. It is also content that is no longer new but still relevant to your audience such as old blog posts, articles, videos, podcasts, etc. This is particularly helpful when you are just starting out and you don’t have enough new content to keep your social media feeds as fresh as an Adidas jogging suit in the 80s.

Marketing Channels:

Below are the basic social media channels through which we distribute our content. Your list may be longer or you may be focused on only one channel or those outside of social media. However, we will focus on social media in this article because this is where we can manage automation.

  1. Twitter: I always include Twitter in my marketing plan because it offers an easy to use API, a variety of ways for effective marketing and a large, active and loyal user base.
  2. LinkedIn: I have only recently included LinkedIn in my marketing plan because, for StackEnglish.com, this is where I will find most of my target market. It offers the perfect type of customer for an English learning business for IT professionals.
  3. Facebook: I must admit, Facebook is more of a formality for me. I’m on Facebook all the time and interact with a lot of people on a daily basis there so I thought it would be wise to have a Facebook business page to direct them to if asked. However, depending on what you are marketing, this has been an amazing channel for many businesses.

Useful Ruby Gems:

Here are the essential gems you’ll need to get your automation going. This isn't for beginners so grab these gems if you don’t already use them and get them setup in your Rails app.

  1. Twitter: The twitter gem simply rocks. The only issue I have with it is that it gives you very little control over rate limiting so I’m unable to implement some cool ideas I have for marketing on Twitter. Otherwise, it’s awesome and you need it so get it here: https://github.com/sferik/twitter
  2. Omniauth-Twitter: Grab this gem for all of your Twitter authentication needs: https://github.com/arunagw/omniauth-twitter
  3. Bitly: We are going to be creating short URLs for our automation links so you are going to want to grab this gem: https://github.com/philnash/bitly
  4. Whenever: What would automation be without cron jobs? Well, there are other ways you could do it but, shut up, I’m going to talk about cron jobs. So you will need this sweet Whenever gem so you can easily create a schedule for content that will run via cron jobs. https://github.com/javan/whenever

Marketing Elements:

Below are a list of content types that I use for StackEnglish.com. It in no way limits or defines what you can use to market your business. So be creative!

  1. Articles: I write articles covering English improvement for non-native, English-speaking IT professionals as well as an occasional article about Ruby on Rails. These are published on Mondays, Wednesdays and Fridays.
  2. Videos: I produce videos to help English learners effectively answer interview questions in English. These are published on Tuesdays and Thursdays.
  3. Mini-Courses: I have landing pages where people can enroll in mini-courses that I produce only for my mailing list. Video mini-courses are the only content I distribute using my mailing list.
  4. Quiz Questions: These are multiple choice quiz questions related to Business English. I have created over 50 of these quiz questions as high-quality branded images to be distributed via social media.

Code Examples:

Since I’ve rambled on about automated marketing, I guess I have to actually show you some code too. You’re so demanding. Ha!

All of my automation is accomplished through rake tasks. Surprise!

Rake tasks are under lib/tasks. Log files for the rake tasks go under /log/tasks/directory-name-for-tasks. Do not forget to create these log files or your rake task will fail.

Note: In the code below, there is a reference to an article “slug” because I use the Stringex gem and its “act_as_url” feature to create friendly urls for my articles and videos. You can get it here: https://github.com/rsl/stringex

For the url_hash you will need to pass in the friendly url slug or the article/video id.

Below are some examples of the code I use. You will quickly see a pattern and that not a whole lot changes between use cases. I will also assume you have your Facebook account connected to your Twitter account. If you do, whatever you tweet will also go to your Facebook business page (optional).

Let’s get started.

Publish Articles: lib/tasks/articles/publish_article.rake

namespace :articles do
desc “This task looks for an unpublished article then publishes and tweets it.”
task publish_article: :environment do
log = ActiveSupport::Logger.new(‘log/tasks/articles/publish_article.log’)
start_time = Time.now
log.info “Task started at #{start_time}”
article = Article.where(published: 0).take
article.publish_on = Date.today
article.tweeted_on = Date.today
article.published = 1
article.save
ArticleMailer.publish_article(article).deliver_later
photo = “public” + article.image_url
article_base_url = ‘https://yourdomain.com/articles/’
article_url_hash = “#{article.slug}"
article_url = “#{article_base_url}#{article_url_hash}”
client = Bitly.client
short_url = client.shorten(article_url).short_url
$twitter.update_with_media(“#{article.title} : #{short_url} — ##{article.tag}”, File.new(photo))

end_time = Time.now
duration = (start_time — end_time) / 1.minute
log.info “Task finished at #{end_time} and lasted #{duration} minutes.”
log.close
end
end

Tweet Articles: lib/tasks/tweets/tweet_article.rake

namespace :tweets do
desc “This tweets one article from our database of articles.”
task tweet_article: :environment do
log = ActiveSupport::Logger.new(‘log/tasks/tweets/tweet_article.log’)
start_time = Time.now
log.info “Task started at #{start_time}”

article = Article.where(published: 1).where.not(tweeted_on: Date.today).take
article.tweeted_on = Date.today
article.save
photo = “public” + article.image_url
article_base_url = ‘https://yourdomain.com/articles/’
article_url_hash = “#{article.slug}”
article_url = “#{article_base_url}#{article_url_hash}”
client = Bitly.client
short_url = client.shorten(article_url).short_url
$twitter.update_with_media(“#{article.title} : #{short_url} — ##{article.tag}”, File.new(photo))
end_time = Time.now
duration = (start_time — end_time) / 1.minute
log.info “Task finished at #{end_time} and lasted #{duration} minutes.”
log.close
end
end

Tweet Mini-Course: lib/tasks/tweets/tweet_mini_course_name.rake

namespace :tweets do
desc “This tweets the link to a mini course landing page.”
task tweet_mini_course_name: :environment do
log = ActiveSupport::Logger.new(‘log/tasks/tweets/tweet_mini_course_name.log’)
start_time = Time.now
log.info “Task started at #{start_time}”

photo = “public/assets/images/” + ‘mini-course-name.jpg’
course_base_url = ‘https://yourdomain.com/mini-course-name'
course_url = “#{course_base_url}”
client = Bitly.client
short_url = client.shorten(course_url).short_url
$twitter.update_with_media(“This is where you can manually type in ad copy for your mini-course. #{short_url}”, File.new(photo))
end_time = Time.now
duration = (start_time — end_time) / 1.minute
log.info “Task finished at #{end_time} and lasted #{duration} minutes.”
log.close
end
end

Setting Up A Schedule For Cron:

Now that you have some rake tasks to run, you will need to setup a schedule for cron. When you setup the Whenever gem, it will create the following file: config/schedule.rb

Here is an example schedule for running you newly created rake tasks.

# Use this file to easily define all of your cron jobs.set :environment, “production”
set :output, {:error => “log/cron_error.log”, :standard => “log/cron.log”}
every :monday, :at => '9am' do
rake "articles:publish_article"
end
every :wednesday, :at => '9am' do
rake "articles:publish_article"
end
every :friday, :at => '9am' do
rake "articles:publish_article"
end
every 10.hours do
rake "tweets:tweet_article"
end
every 1.week, :at => ‘5:00 am’ do
rake “tweets:tweet_mini_course_name”
end

Once you deploy your code to production you will need to run the following command whenever you need to update cron:

whenever — update-crontab

RSS Feeds & Hootsuite for LinkedIn:

Because the LinkedIn API is not as easy to setup as Twitter, I felt it would be less painful to just use HootSuite to post content to LinkedIn. By using HootSuite’s RSS feed feature, I can have it post new articles and videos to LinkedIn whenever a new article or video is published.

  1. Creating an RSS feed: Setting up an rss feed in Rails 5 is easy peasy. Put the following code in your public-facing articles (or videos or podcasts) controller (app/controllers/articles_controller.rb).
def feed
@articles = Article.where(:published => true).order(‘publish_on DESC’)
respond_to do |format|
format.html
format.rss { render :layout => false }
end
end

You will also need to create this file in the appropriate directory. This kind of file will need to be created for each RSS feed type (you will need to use the carrierwave gem for images to setup the (:listview) attribute. This is optional): app/views/articles/index.rss.builder

xml.instruct!
xml.rss :version => ‘2.0’, ‘xmlns:atom’ => ‘http://www.w3.org/2005/Atom' do

xml.channel do
xml.title ‘Name Your RSS Feed’
xml.description ‘Describe the content of your RSS feed.’
xml.link root_url + ‘articles’
xml.language ‘en’
xml.tag! ‘atom:link’, :href => root_url + ‘articles.rss’, :rel => ‘self’, :type => ‘application/rss+xml’

for article in @articles
xml.item do
xml.title article.title
xml.category article.category
xml.pubDate article.publish_on.to_s(:rfc822)
xml.link article_url(article)
xml.guid article_url(article)
xml.description(h(article.body))
xml.image do |image|
image.url ‘https://yourdomain.com’ + article.image.url(:listview)
image.title article.title
image.link article_url(article)
end
end
end

end

end

Then put the following code in your public-facing layout file (app/views/layouts/file-name.html.erb). *You will need one of these lines for each type of RSS feed you create. Or you could create one mega feed which I must do now that I think about it…

<%= auto_discovery_link_tag :rss, articles_url(:format => :rss) %>

If you also wish to create a public-facing link to the RSS feed you can use the code below:

<%= link_to “RSS Feed”, articles_url(:rss) %>

2. Setting up your RSS feed in Hootsuite: Now it’s time to setup your RSS feed in Hootsuite. You should now have an RSS feed URL that looks like this: https://yourdomain.com/articles.rss

From your Hootsuite account go to Publisher =>RSS Feeds => RSS/Atom. Click the + to add a new feed. Supply the link to your RSS feed, select LinkedIn from the network dropdown and fill in the rest of the fields however you like. Save the feed and you’re done!

That’s it!

Now you can focus on creating content and interacting with your target audience.

This information should also help you get more ideas about what other content or tasks you can automate or how you can automate to other channels.

Do you currently automate your marketing tasks with Ruby on Rails? How? What else can you automate? Let me know in the comments, chat with me on Twitter, LinkedIn or if you are looking to improve your Technical or Business English, subscribe to my newsletter for video mini-courses!

--

--

Tressa Sanders

Technical Writer, Senior English Trainer and Ruby on Rails Developer with over 20 years working in the Information Technology industry.