Browse by...
Home Resources Blog

Andrew Spear

Guest posting today is Andrew Spear, the Director of Plethora, a New Zealand-based company that builds tools for designers. He will be following up our earlier post, ‘V is for Viral: An inspiring subscribe form’ with this tutorial on how to build an email subscribe form that generates a share link upon signup and encourages referrals.

How the form works
To help build interest in our new product, ‘My Little Geek‘, I decided to take some inspiration from Forkly’s beta invite page and build my own landing site, featuring a socially shareable email subscribe form. With a few lines of PHP code, I managed to not only hook up the form to my Campaign Monitor list, but also build a pretty effective way to share the page via Twitter, Facebook and email.

Lets look under the bonnet. As we’ll be tinkering with code, a basic knowledge of PHP and HTML will go a long way in regards to understanding this tutorial.

The concept behind our viral form

The main idea behind our viral subscribe form is that we offer customers a reward for subscribing – Forkly gave people access to their beta test, we offer people 10% off our book. Secondly, we want to give people an even bigger reward for sharing the love – if three friends subscribe, the referrer will get a 20% discount, instead of the regular 10%. In order to achieve this, each subscriber is given a unique URL to share with their friends, so that we can track who referred who.

Technically, what we’re going to do is:

  1. Allocate a unique ID to each visitor on our subscribe page.
  2. Store that ID in a Campaign Monitor custom field when they subscribe.
  3. Create a unique URL from this ID which the customer can share with others.
  4. If someone comes to the site from another subscriber’s unique URL, record it.

This may sound pretty complex, but we’ll go through it, bit by bit.

Setting up your account

First, create a list for your subscribers and add two custom fields – one for the subscriber’s unique ID and one for the ID of the person who referred them.

Next, set your own subscribe confirmation page (or ‘initial confirmation page’, if you’re using double opt-in). For simplicity, I made this the same URL as the subscribe page, but with a ‘flag’ on the end of the link (ie. ?subscribe=true). We’ll need this flag to tell our PHP code that we’ve added someone new to our subscriber list:

Custom confirmation page

The final thing to do is create a subscribe form for your website. Make sure you include the two custom fields that we created above.

Setting up your subscribe page

Now, lets look at the snippet of PHP you need to add before the HEAD tag of your subscribe page’s HTML code. This code is necessary to start a session and generate a unique URL for each subscriber. Take a look at the comments to get an idea of what’s going on and how you’ll need to customize the code – for instance, you’ll want to change the ‘‘ bit to the URL of your subscribe form.

// Tell PHP that we're using sessions
// If this user just subscribed, create a sharable URL from their unique ID
$share_url = (isset($_GET['subscribe']) && isset($_SESSION['id'])) ? ''.$_SESSION['id'] : '' ;
// Did this visitor come from another user's unique URL?
$referid = (isset($_GET['ref'])) ? $_GET['ref'] : '';
// Let's create this user's unique ID, we'll store in the session so we can pick it up after they've subscribed
if (!isset($_SESSION['id'])) {
$_SESSION['id'] = '';
$chars = '23456789bcdfghjkmnpqrstvwxyz';
for ($n=0; $n<6; $n++) { $_SESSION['id'] .= $chars[mt_rand(0, strlen($chars)-1)]; }

Now we need to add the actual subscribe form to the page and also give subscribers their unique URL to share. This code is added to the BODY section of your HTML code, wherever you need the form to be added. Most of the work is done by the subscribe form code that Campaign Monitor generated for you earlier, but we’ll make a couple of small alterations. Again, take note of the comments:

<?php if (isset($_GET['subscribe'])) { ?>
<!-- Message seen after subscribing -->
Thanks for subscribing, remember to share this address with your friends to double your discount: <?php echo $share_url ?>
<?php } else { ?>
<!-- Message seen before subscribing -->
Just fill in the form below to be notified when the book comes out and get your 10% discount:
<!-- Campaign Monitor subscribe form begins -->
<form id="subForm" action="" method="post"><label for="xxxxx-xxxxx">Email:</label>
<input id="xhkqt-xhkqt" name="cm-xxxxx-xxxxx" type="text" /><!-- Edited CM form code, featuring session & referrer IDs -->
<input id="id" name="cm-f-yyyyyy" type="hidden" value="<?=$_SESSION['id']?>" />
<input id="referid" name="cm-f-zzzzzz" type="hidden" value="<?=$referid?>" />
<input type="submit" value="Subscribe" /></form><!-- Campaign Monitor subscribe form ends -->
<?php } ?>

Still with me? I hope so. With that successfully in place, you should be merrily collecting new subscribes and referral information in your Campaign Monitor subscriber list.

By the way, you may have noticed that I haven’t included the Twitter, Facebook and email share links that feature on my site – these are very simple to add. For example, here’s the code for a ‘Share on Twitter’ link, featuring a unique share URL:

<a href="<?=$_SESSION['id']?>&text=Get+a+sneak+peek+of+the+%23MyLittleGeek+kids+book+and+get+20%25+off+when+it%27s+released%21&via=plethoraco" target="_blank">Share on Twitter</a>

The end result (click to demo): Share on Twitter

Counting your referrals

You can use a database to record your referrals, but I just left this to the custom fields we created earlier in our Campaign Monitor account. When you are ready, simply export your subscribers to a CSV file, then use a simple COUNTIF formula in Google Docs, Excel or Numbers to count referrals and view your most active referrers. Here’s a sample spreadsheet, based on my data:

Spreadsheet with referrals

I’ve only had the form up a few days, but I’ve already had up to 6 referrals generated by a single subscriber. Nice.

In conclusion…

Since adding this subscribe form to my landing page, things have been working well. The new subscribers have been rolling in and there’s evidence that a healthy amount of sharing across Twitter and Facebook is going on.

Finally, I’m sure there’s a million ways that you can customise and improve the code to suit, so hopefully this gets some ideas flowing. Many thanks for reading this tutorial – I’d love to hear what success you’ve had from this technique and any suggestions you might have to make it even better!

This blog provides general information and discussion about email marketing and related subjects. The content provided in this blog ("Content”), should not be construed as and is not intended to constitute financial, legal or tax advice. You should seek the advice of professionals prior to acting upon any information contained in the Content. All Content is provided strictly “as is” and we make no warranty or representation of any kind regarding the Content.
Straight to your inbox

Get the best email and digital marketing content delivered.

Join 250,000 in-the-know marketers and get the latest marketing tips, tactics, and news right in your inbox.


Get started with Campaign Monitor today.

With our powerful yet easy-to-use tools, it's never been easier to make an impact with email marketing.

Try it for free