Wednesday 5 May 2010

Email campaign tracking with Silverpop

SilverPop offer themselves to the world as an "Engagement marketing solution". They provide click-stream and conversion tracking based on your email marketing campaigns, by putting unique identifiers in the links of your mailings (which they can send out in bulk). If you store these IDs and ping them back to SilverPop's click-stream and conversion-tracking servelets, this means you can get complete conversion analysis based on your email marketing campaigns.

@ Moneyspyder, one of our clients has signed up for a Silverpop account - which means we need a way to easily integrate with SilverPop - and the examples they give are all in php (and a little less than dynamic). So here's our Ruby-on-Rails solution.

1) Cookify the IDs

When a customer clicks on a link in one of the SilverPop emails - Silverpop has cleverly adding tracking IDs to identify the mailing-job and individual customer. When we send tracking information back to SilverPop - we need to send back these ids so the click-tracking is stored against the correct campaign and individual.

Of course, the parameters will disappear after the first time the user clicks another link, and we need to persist the data - to keep tracking all the pages they click on until they eventually convert!

The easiest way to keep track of this to stick it all into the session - then we can just check if it's there and send it each time we want to ping to SilverPop... and of course the best place to do session-wrangling is in a before_filter. So stick the following into something like application.rb

before_filter :save_silverpop_data_in_session

# This method does the storing of the ids from the URL into a session
# cookie for later sending.
# It will replace any existing values in the cookie - which represents the
# user having returned to the site after viewing (and clicking on) another
# link from a different email campaign.
def save_silverpop_data_in_session
if %w{spMailingID spUserID spJobID spReportID}.all? {|f| params.has_key?(f) }
session[:silverpop] = {:m => params[:spMailingID], :r => params[:spUserID],
:j => params[:spJobID], :rj => params[:spReportID]}

2) Configure your pod

SilverPop calls it's servers "pods" - and you could be using any one of them. This works better as a configuration option than hard-coded in your code - and lets you set up a link to the test-server on your dev/test environments. but in environment.rb you'll have something like this:

# SilverPop URL = mailing list manager/ClickStream Analysis

3) Helping hands

Next up is to figure out how to ping SilverPop... which they helpfully give us an img tag example of how to build up the correct url with all the requisite values.

SilverPop has two servelets - one for accepting pings for click-stream tracking and one for the conversions. But they're almost identical, just taking different parameters... and I'm lazy and don't want to have to remember all the common details of how to do this in each place in the site. Thus: helpers to the rescue!

# This method generates the code that calls the ClickStream tracking
# servelet - we pass in the page name and page URL, along with the values
# passed through from the last silverpop email - saved in the session
def silverpop_click_stream_ping(page_name, page_url)
silverpop_link('cst', :name => page_name, :s => page_url)

# This method generates the code that calls the Conversion Tracking
# servelet - we pass in the "action", "detail" and "value" flags -
# eg "silverpop_conversion_ping 'CompletedOrder',, order.grand_total"
# along with the values passed through from the last silverpop email -
# saved in the session
def silverpop_conversion_ping(action,detail,value = nil)
silverpop_link('cot', :action => action, :detail => detail, :value => value)

# generate the SilverPop ping-image based on required servelet and parameters
def silverpop_link(servelet, options)
# skip out early if this user hasn't come through a silverpop email
return nil unless session[:silverpop].present?

image_tag "http://#{SILVERPOP_SITE_URL}/#{servelet}?#{session[:silverpop].merge(options).to_query}",
:height => 1, :width => 1, :alt => "Silverpop #{servelet.upcase} Servelet Ping"

4) Click-stream - analyse!

So, now it's time to get down and dirty with the click-stream analysis. This couldn't be more simple. Just use the helper to pop a link into your main layout. eg:

<%= silverpop_click_stream_ping(@page_title, url_for(:only_path => false)) -%>

Now, as you can see, this mainly works by using our dynamic page-title and a link to the current-page (using the empty url_for trick). Note that if you leave off the "only_path=false" option it won't provide the hostname. This may be what you want if you want to roll up multiple mirrored domains. You'll also have to adjust the page-name parameter as necessary for the way you generate the page-title... but otherwise you're good to go and this means every page-click gets tracked back to SilverPop from now on.

With one caveat... if you have AJAX-updating, you may need to figure out a neato trick of putting the ping-link into the newly-generated page-pieces or these "clicks" won't get tracked as the layout won't see them as new pages. I'll leave that as an exercise for the reader as it's very site-specific.
5) Mine your conversion gold

Now only the final, and most important, part is left - tracking actual conversions.

A lot has already been written about what constitutes an important conversion for your site. I won't repeat it all here as you can track heaps, and it's really down to what is important for your business. So I'll pretend we only care about when a customer completes an order - which we know because they land on the "thank you" page.

Which means we need to pop a link to the COT servelet there and pass in the important details... nothing easier:

<%= silverpop_conversion_ping("Order Complete",, @order.grand_total) -%>

Now SilverPop will monitor our marketing mails from go to whoa - and even know which order they completed and, most important, how much money they ended up spending ... Gold!

Taryn East