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 ‘http://yourdomain.com‘ 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'])) ? 'http://yourdomain.com?ref='.$_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="http://youraccount.createsend.com/t/r/s/xxxxx/" 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="http://twitter.com/share?url=http%3A%2F%2Fplethora.co%2Fb%2F<?=$_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!

  • Scott Lyttle


    Great article thanks!
    Small error in your subscribe form PHP:

    <!– Campaign Monitor subscribe form begins –>
    <form action=”http://youraccount.creasend.com/t/r/s/xxxxx/’ or the form won’t action properly.

  • Ros Hodgekiss

    Fixed. Cheers for spotting that typo, Scott!

  • Kristian

    I love it! Spent all afternoon working with it and had no problems getting it to work with your instructions. TY

    It’s to bad the COUNTIF formula has to be run after the data is exported. It would be great if it could be calculated in CM, allowing an auto responder to send an email with a new/better coupon each time the subscriber hits a specific number of referrals.

  • Ros Hodgekiss

    Hi Kristian, we’re certainly interested in doing more with custom fields and autoresponders, so thank you very much for your feedback. I’ll make note of this internally and will let you know if custom field-based autos is something plan to introduce in the future.

  • A. Schrijvers


    I like the article and tried it.

    It seems not be working totally. Sometimes when I see the Subscribe conformation page the Shared URL is not showed. It look like that it happens when a user closed the browser so the session is ended and pressed in the conformation link url.

    For a total few, I’m using the double optin.

  • James Allen

    This is a great tutorial and very helpful for a project I am currently working on.

    I was wondering if there is anyway of sending the users unique id to them with an auto-responder email from CM?

  • Ros Hodgekiss

    @James – Sure, you can setup a basic autoresponder for the subscriber list you have setup for the form. To send the unique id to new signups, simply create an HTML email and add a custom field tag like so:

    “Share this link with your friends: http://yourdomain.com/[id,fallback=]”

    Here’s a similar example from our friends at Litmus. Hope this helps!

  • James Allen

    Great, thanks for your help Ros. Pretty straight forward really – for reasons unknown I was trying to put the PHP <?=$_SESSION[‘id’]?> code in instead of the CM [id,fallback=], was probably too late at night and my brain not functioning properly :)

    If you’re interested in seeing where this tutorial has been applied, see: http://www.bbesquire.com

    Thanks again.

  • Ros Hodgekiss

    Well done, James – that’s a gorgeous signup page! Glad we could help out :)

  • Andrew

    We’re launching a new web app soon to help companies schedule their resources so this is absolutely perfect for the teaser site we’re developing. Thanks for sharing!

  • Mark Shingleton

    That is a fantastic breakdown and perfectly timed for us. I’ve been wanting to do more with Social media and our monthly newsletter … you’ve certainly provided some great inspiration.
    Thanks very much Andrew and Campaign Monitor!

    Mark Shingleton
    Sentinel Monitoring

  • Michael Mulligan

    Stumbled upon this post as we were building our signup page, worked perfectly!


  • Andrew

    We’ve just launched our teaser site for a new resource scheduling app based on your concept – https://resourceguruapp.com. It’s working brilliantly – we’ve had over 100 sign ups in just a few days. We’re giving away an iPad as a little incentive to pass it on to friends. Thanks a lot for the tips – really appreciate it.

  • Christine Rondeau

    With regards to the share this links on twitter.
    The code that you use has the title and link to the newsletter.
    What if you wanted to add this to a template? Is there a snippet of code that generates the url for you?

  • Ros Hodgekiss

    @Christine – That share link in mention is to be featured on your website, not in an HTML email. You can’t run the scripts required in an email, I’m sorry to say.

    If you’re keen on adding social sharing links to your campaigns, you can use our <tweet> tags – here’s the skinny on this.

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.


See why 200,000 companies worldwide love Campaign Monitor.

From Australia to Zimbabwe, and everywhere in between, companies count on Campaign Monitor for email campaigns that boost the bottom line.

Get started for free