<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DIYNINJAS &#187; CampaignMonitor</title>
	<atom:link href="http://diyninjas.com/tag/campaignmonitor/feed/" rel="self" type="application/rss+xml" />
	<link>http://diyninjas.com</link>
	<description>The Thesis Theme Pros</description>
	<lastBuildDate>Sat, 11 Apr 2009 03:58:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Improved Subscriber Management with the Campaign Monitor API</title>
		<link>http://diyninjas.com/improved-subscriber-management-with-the-campaign-monitor-api/</link>
		<comments>http://diyninjas.com/improved-subscriber-management-with-the-campaign-monitor-api/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 06:46:18 +0000</pubDate>
		<dc:creator>Mitch Cooper</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[CampaignMonitor]]></category>
		<category><![CDATA[email marketing]]></category>

		<guid isPermaLink="false">http://diyninjas.com/?p=390</guid>
		<description><![CDATA[For many of our clients, an integral part of maximising their web presence is through effectively leveraging email marketing as a medium for promoting their brand and products. While simple Feedburner email is a great solution for giving your readership a means to receive your updates in their inbox, if you really want to make [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>For many of our clients, an integral part of maximising their web presence is through effectively leveraging email marketing as a medium for promoting their brand and products. While simple Feedburner email is a great solution for giving your readership a means to receive your updates in their inbox, if you really want to make an impact with email you can&#8217;t look past <a title="Campaign Monitor" href="http://www.campaignmonitor.com">CampaignMonitor</a>.</p>
<div id="attachment_400" class="wp-caption aligncenter" style="width: 412px">
	<img class="size-full wp-image-400" title="Subscriber Management with the CampaignMonitor API" src="http://diyninjas.com/wp-content/uploads/2009/04/campaign-monitor-api-email-marketing.jpg" alt="Subscriber Management with the CampaignMonitor API" width="412" height="260" />
	<p class="wp-caption-text">Subscriber Management with the CampaignMonitor API</p>
</div>
<p>With it&#8217;s designer focussed approach, powerful and elegant analytics and can&#8217;t-be-bet client publishing tools we&#8217;ve found CampaignMonitor was perfect out of the box for just about everything. Recently however we faced a scenario where CampaignMonitor&#8217;s <em>&#8217;simple, yet effective&#8217; </em>subscriber management process wasn&#8217;t quite enough: <strong>in step the powerful CampaignMonitor API.</strong></p>
<p><span id="more-390"></span>We had a client who needed a customized valediction line, signing the email off according to who referred the client to the database. This was super easy to achieve using CampaignMonitor&#8217;s custom fields, but there was a fair bit of a juggling involved in ensuring we never overwrote the initial referring agent in the database in the event someone else landed the same referral.</p>
<h3>Creating the Sign-up Form</h3>
<p>To overcome this, we first built our sign-up form. Because <span style="text-decoration: line-through;">we&#8217;re lazy</span> we truly believe <em>forms should be fun</em> we headed straight over to Wufoo.com and put together the form. Utilizing Wufoo&#8217;s awesome export code function, we had a beautiful form together in no time. The ingredients: name, email address, referring agent in a drop down box and a tick box asking agent&#8217;s to confirm they&#8217;d received explicit permission to add their client.<sup class='footnote'><a href='#fn-390-1' id='fnref-390-1'>1</a></sup></p>
<p>You&#8217;ll want to paste this into a new PHP file &#8211; call it signup.php</p>
<h3>Accessing the API</h3>
<p>To access the API, we grabbed the sweet CampaignMonitor PHP API wrapper from <a title="CampaignMonitor API PHP Wrapper" href="http://code.google.com/p/campaignmonitor-php/">here</a>.</p>
<p>You&#8217;ll want extract CMBase.php to the folder where you&#8217;ve placed signup.php and paste the following right after the &lt;head&gt; tag in the signup form file:</p>
<pre class="brush: php;">

$api_key = 'YourCampaignMonitorAPIKey';
$client_id = 'YourClientsClientID';
$campaign_id = null;
$list_id = 'TheIDofTheListYouWantToChange';
$cm = new CampaignMonitor( $api_key, $client_id, $campaign_id, $list_id );
</pre>
<h3>Populating the Drop Down with Custom Fields</h3>
<p>You can ignore the formatting details in the code below and you&#8217;ll likely need to amend exactly what you&#8217;re trying to output in the drop down, but this code should serve as an workable starting point to get that drop down populated<sup class='footnote'><a href='#fn-390-2' id='fnref-390-2'>2</a></sup>:</p>
<pre class="brush: php;">

&lt;?php     
 echo '&lt;select id=&quot;Field109&quot; name=&quot;Field109&quot; class=&quot;field select medium&quot; tabindex=&quot;3&quot;&gt;';
 foreach($fields[&quot;anyType&quot;]['ListCustomField']['1']['FieldOptions']['1']['string'] as $key =&gt; $value) {
 echo &quot;&lt;option value=\&quot;$value\&quot;&gt;$value&lt;/option&gt;&quot;;
 }
 echo &quot;&lt;/select&gt;&quot;;
?&gt;
</pre>
<p>You&#8217;ll also want to note down the <strong>name</strong> so we can grab the details from the field to pass up to Campaign Monitor through the API.</p>
<h3>Getting Ready to Work with the Subscriber Data</h3>
<p>We&#8217;ve kept it simple in deploying this form and simple created a second PHP file called signup-submit.php which we&#8217;ll use to review the data submitted by the agent, let them know what happened and pass it up through the API if the subscriber does not exist.</p>
<p>You&#8217;ll want to create a similar file. You&#8217;ll need to include the API details as in signup.php and you&#8217;ll want to set the action of the previous form to send everything through to signup-submit.php.</p>
<p>You&#8217;ll want to grab the form data and set them as variables so we can work with them easily in PHP. Here&#8217;s what we&#8217;ve done:</p>
<pre class="brush: php;">

$name = $_POST[&quot;Field1&quot;];            // Name
$email_in = $_POST[&quot;Field2&quot;];        // Raw email from form
$email_out = strtolower($email_in); // Strip emails to lower case
$ref = $_POST[&quot;Field109&quot;];            // Referring Agent
</pre>
<h3>Checking if the Subscriber Exists</h3>
<p>You might&#8217;ve noticed above, that we&#8217;ve got a vairable for $email_in and $email_out. Our agent&#8217;s have a nasty habit of capitalising emails, so to keep everything tidy on our side of the equation, we&#8217;ve opted to strip the caps out of anything they submit. I don&#8217;t believe it&#8217;s necessary &#8211; but a handy trick, nonetheless!</p>
<p>So, with the email ready we&#8217;ll want to check through the API if it&#8217;s already registered. With the PHP wrapper it&#8217;s just a simple function away:</p>
<pre class="brush: php;">

$var = $cm-&gt;subscribersGetIsSubscribed($email_out,$list_id);
</pre>
<h3>Actioning the Results</h3>
<p>If the subscriber is already registered we want to let the agent know and call it a day. Otherwise, we&#8217;ll want to get them subscribed quick smart. The API call we used in the previous step returns a value of true or false, depending on whether or not the subscriber exists. With this, we can simply use an if / else approach to adding subscribers. Here we go:</p>
<p><strong>If the Subscriber Exists</strong></p>
<p>This one&#8217;s pretty simple &#8211; if the subscriber exists and the variable $var returns true we&#8217;ll let the agent know and leave it that, providing a link so they can jump back and try another address if need be:</p>
<pre class="brush: php;">

if ($var['anyType'] === 'True') {
 echo &quot;&lt;strong&gt;Sorry, the email address &lt;em&gt;$email_out&lt;/em&gt; is already in the database!&lt;/strong&gt;&quot;;
 echo '

';
 echo 'Click &lt;a href=&quot;/signup.php&quot; title=&quot;Add a New User&quot;&gt;here&lt;/a&gt; or use the back button in your browser to try a different subscriber.';
 }
</pre>
<p><strong>If the Subscriber is New</strong></p>
<p>If $var does not return true (the subscriber does not exist) we can go ahead and add the subscriber through the CampaignMonitor API:</p>
<pre class="brush: php;">

else {
 // Add to Database with Custom Fields
 $result = $cm-&gt;subscriberAddWithCustomFields(&quot;$email_out&quot;,&quot;$name&quot;, array('ReferringAgent' =&gt; $ref,'SignatureImage' =&gt; $image));
 if($result['Result']['Code'] == 0) {
 echo &quot;Success! You've added &lt;strong&gt;$name&lt;/strong&gt; with the email address &lt;strong&gt;$email_out&lt;/strong&gt; to the database.&quot;;
 echo &quot;
$ref
&quot;;
 echo 'Click &lt;a href=&quot;/signup.php&quot; title=&quot;Add a New User&quot;&gt;here&lt;/a&gt; or use the back button in your browser to try a different subscriber.';
 }
 else {
 echo 'Error : ' . $result['Result']['Message'];
 }
 }
</pre>
<p>You&#8217;ll need to change the field variables according to what you&#8217;ve used, but that should pretty much have you set. Drop us a comment if you have any trouble implementing this and we&#8217;ll see what we can do!</p>
<h3>Bonus for the Thesis Framework</h3>
<p>If you&#8217;re running on <a title="Why Thesis Rocks for Web Developers" href="/why-thesis-rocks-for-web-developers">Thesis</a>, as many of our subscribers are, getting your shiny new sign-up form into your site is as simple as hooking it in. We&#8217;ve got multiple sign-up forms on different pages for one client, so here&#8217;s how we hooked &#8216;em in:</p>
<pre class="brush: php;">

/* Email Sign Up Forms */
function email_forms() {
 if (is_page('427')) {  // Albany ?&gt;
 &lt;iframe height=&quot;739&quot; allowTransparency=&quot;true&quot; frameborder=&quot;0&quot; scrolling=&quot;no&quot; style=&quot;width:100%;border:none&quot; src=&quot;http://domain.com/api/signup.php&quot;&gt;&gt;&lt;/iframe&gt;
 &lt;?php    }
 elseif (is_page('414')) { // Devonport ?&gt;
 &lt;iframe height=&quot;739&quot; allowTransparency=&quot;true&quot; frameborder=&quot;0&quot; scrolling=&quot;no&quot; style=&quot;width:100%;border:none&quot; src=&quot;http://domain.com/api/signup2.php&quot;&gt;&gt;&lt;/iframe&gt;
 &lt;?php    }
 }
add_action('thesis_hook_after_post','email_forms');
</pre>
<h3>Final Note</h3>
<p>Just a big shout out to the folks over on the <a title="PHP Freaks Forums." href="http://www.phpfreaks.com/forums/">PHP Freaks forums</a> and the <a title="Campaign Monitor" href="http://campaignmonitor.com">CampaignMonitor</a> support team who went well above the call of duty to help us nail this one quickly. Couldn&#8217;t have done this without &#8216;em!
<div class='footnotes'>
<div class='footnotedivider'></div>
<ol>
<li id='fn-390-1'>We haven&#8217;t had any trouble with this so far, but we reckon as developers it&#8217;s crucial we take <em>every</em> opportunity to reinforce the importance of permission in permission based marketing. <span class='footnotereverse'><a href='#fnref-390-1'>&#8617;</a></span></li>
<li id='fn-390-2'>If all you&#8217;re looking to do is create a form that will check for duplicates you can skip this step. If you&#8217;re looking for a bit more in terms of custom fields, you can add as many of these as you like. Beauty. <span class='footnotereverse'><a href='#fnref-390-2'>&#8617;</a></span></li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://diyninjas.com/improved-subscriber-management-with-the-campaign-monitor-api/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
