MyBB Community Forums

Full Version: [Tutorial]: Automatically Track All Referral Links with Bitly
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I was looking to do this so that I could also track clicks of referral links (to see if members are actually sharing the referral links) and to offer cool QR codes that could be shared as well. Credits to Prateek, a commenter here for the coding. I just implemented it for MyBB usage.

1. Add this line to your .htaccess
RewriteRule ^referral-([^./]+) member.php?action=register&referrer=$1

2. Sign up for bit.ly if don't already have it. Get your username and your API key ready.

3. Go to the User CP and copy the referral text there, or make up whatever you want to use for the following.
Go to templates > User Control Panel Templates > usercp_referrals,
Add this on it's own line at the beginning of the template
<!-- Bitly -->
<?php
/**
* @author Praateek
* @copyright 2010
*/
function bitly_shorten($url)
{
$username = "YOUR_BITLY_USERNAME_HERE";
$apikey = "YOUR_BITLY_APIKEY_HERE";
$response = file_get_contents("http://api.bit.ly/v3/shorten?login=$username&apiKey=$apikey&longUrl=$url&format=json&quot")  ;
$response = json_decode($response);
if ($response->status_code == 200 && $response->status_txt == "OK")
{
return $response->data->url;
} else
{
return null;
}
}
$link = urldecode("{$mybb->settings['bburl']}/referral-{$mybb->user['uid']}")  ;
echo bitly_shorten($link);
?>
<!-- Bitly -->

And replace
 {$referral_link} 
with the copied referral message or your own message. Something like "To refer a member to NAME_OF_YOUR_SITE, copy and share your personal Referral Link below" And below it add:
</br> <? echo bitly_shorten($link); ?>

4. You can also include a link to, or display, a QR code with the following:
For the link:
<a href="<? echo bitly_shorten($link); ?>.qrcode" target="_blank"><b>QR Code</b></a>

For the image:
<img src="<? echo bitly_shorten($link); ?>.qrcode" height=150 width=150>

Pretty simple. Here's what I used for my forum, with some things changed

<!-- Bitly -->
<?php
/**
* @author Praateek
* @copyright 2010
*/
function bitly_shorten($url)
{
$username = "YOUR_BITLY_USERNAME_HERE";
$apikey = "YOUR_BITLY_APIKEY_HERE";
$response = file_get_contents("http://api.bit.ly/v3/shorten?login=$username&apiKey=$apikey&longUrl=$url&format=json&quot")  ;
$response = json_decode($response);
if ($response->status_code == 200 && $response->status_txt == "OK")
{
return $response->data->url;
} else
{
return null;
}
}
$link = urldecode("{$mybb->settings['bburl']}/referral-{$mybb->user['uid']}")  ;
echo bitly_shorten($link);
?>
<!-- Bitly -->

<strong>{$lang->members_referred}</strong> {$mybb->user['referrals']}<br />
<!-- {$referral_link} -->
</br>
To refer a member to NAME_OF_YOUR_SITE, copy and share your personal Referral Link below
</br>
<? echo bitly_shorten($link); ?>
 or share your <a href="<? echo bitly_shorten($link); ?>.qrcode" target="_blank"><b>QR Code</b></a>