Pages

Wednesday, 9 April 2014

Campaign Monitor Using ROR


Campaign Monitor

Campaign Monitor is an api which is basically a template builder. Which is very easy way to design a beautiful email template in a minute. Using campaign monitor we can design our email newsletters to make an email effective. You can send that email to multiple users at a time.

  • Create a free account on https://www.campaignmonitor.com.
  • Register an app in your account. Click on Account Setting on the top of the page and after that just click on 'manage connected apps' on the right hand side of the page.
  • Register the app and it will provide you the ClientId and other parameters.
  • And On the 'account setting' page click on 'show api key'. Note down the api key.
Steps to integrate in Rails app
  • Firstly, We need to install gem 'createsend', so write 'gem createsend' in the gem file and from console run the command 'bundle install'.
  • We want to sync the correspondents with the campaign monitor.
  • Write require 'createsend' on the top of the file where you are writing code for createsend .
  • For sync the correspondent, We have need of the list id
so steps to create a list and find list id
auth = { :api_key => '19cc85ed4775c1cf6e8cbab1ce540e06' } # Your api key cs = CreateSend::CreateSend.new auth #it will return a createsend object clients = cs.clients #it will return a list of clients and result will in array #Here i will find the first client of the list so
client = cs.clients.first
#Now create a list using client id
CreateSend::List.create(auth,client.ClientID,"spryuser","http://www.symfodium.com",false,"AllClientLists")
CreateSend::List.create(auth,client_id, title, unsubscribe_page, confirmed_opt_in, confirmation_success_page, unsubscribe_setting="AllClientLists")
# client_id - String representing the ID of the client for whom the list will be created.client id comes when we register the app. # title - String representing the list title/name. Which must me unique every time# unsubscribe_page - String representing the url of the unsubscribeconfirmation page # confirmed_opt_in - A Boolean representing whether this should be a confirmed opt-in (double opt-in) list # confirmation_success_page - String representing the url of the confirmation success page # unsubscribe_setting - A String which must be either "AllClientLists" or "OnlyThisList".
Note :- Title must be unique ever time
AllClientLists or OnlyThisList. If unsubscribe setting is set to AllClientLists, when someone unsubscribes from this list they will also be unsubscribed from all the client's lists (recommended). If unsubscribe setting is set to OnlyThisList, when someone unsubscribes from this list they will only be unsubscribed from this list.

  • This Command will return a list id
  • This is the 'listid' which we will use for synchronise the campaign monitor list to list of peoples.
  • From the code given below , we will find the list of people and import the list in campaign monitor list
corres = Correspondent.find(:all) # Correspondent is table name and fetch all the records from the this table subscribers = [] #an empty array corres.each do |c| # loop for the all records.  if c.email != ""     # keep remember format should like this     subscribers << { :EmailAddress => "#{c.email}", :Name => "#{c.name}" }  end end subs = subscribers.in_groups_of(950, false) # We can import only 1000 people batch at one time. so we will make groups of 950 or 1000 records.
subs.each do |sub|   results = CreateSend::Subscriber.import(auth, list_id, sub, true, true) #For import the     multiple subscribers end
CreateSend::Subscriber.import(auth, list_id, sub, true, true) command is in real CreateSend::Subscriber.import(auth,list_id,subscribers, resubscribe,queue_subscription_based_autoresponders=false, restart_subscription_based_autoresponders=false)
# auth as api key and list id we have create. #subscriber is list of people to import . # If the Resubscribe input value as true, people will be re-added to the list. If Resubscribe is specified as false, the subscriber will not be re-added to the active list.

Note:
  • if we change the email from database, campaign monitor will create a new record in list this is only true when the resubscribe value is true.
  • if we delete the record(email) from the campaign monitor and that record exist in the database of user than next time when we call method, it will active that record means record will be non deleted from campaign monitor list.
  • if we change the name of the person from the database and then run the command of import people it will change name from campaign monitor list but will not crate a new record.but if u will chage the email, it will create a new record in campign monitor.
  • You can import 1000 records at a time. For more records you need to make an another call.
  • username: t
  • password: tpenbake