Automatically Track App Sale Referrals

On September 13, 2011

Michael Tyson is an independent iOS developer, and runs a small artisan software development business called A Tasty Pixel. He’s passionate about creating outstanding user experiences, and spends his days sweatin’ the details from his mobile office, a motorhome called Nettle in which he and his partner are spending a few years exploring Europe. Think French wine, brie, baguettes, and Cocoa.

Michael writes about tech stuff and being an indie on the A Tasty Pixel blog, and chronicles the travel stuff at Technomadics.

One of the tricky things about an App Store-based business is tracking where your sales are actually coming from. As Apple don’t offer any kind of referral reporting interface, we developers are pretty much limited to guesswork when assessing the impact our various publicity efforts are having.


Actually, it’s not all that bad. Back in 2009, Mobile Orchard featured an article on connecting click-throughs to app sales. This involved using the affiliate marketing company LinkShare to create affiliate links with which we can track sales, thanks to their partnership with iTunes.  Create a link, append a “signature” parameter, and we get reports on sales generated via that link, corresponding to the signature we provide.

It gets better, though: the totally freeform nature of the signature lends itself to an even more flexible approach than than presented in the original article.  By using the signature to represent the domain name of a referring site, we can track all referrals, automatically.

This way if, say, TUAW link to your app site, then if someone clicks through then clicks the download link on your app site and buys, the resulting order will be reported as coming from “tuaw”. If someone clicks through from your Facebook page, it’ll come up as coming from “facebook”. You can even modify the script further to report more precise details (like the path), if you like.

Here’s how it’s done. This approach assumes you’re using PHP, but the principle’s the same for any other language.

Step 1: Sign up to LinkShare

First, if you haven’t already, sign up to the LinkShare program. Once you’ve created a LinkShare account, join the Apple affiliate program via the “Programs” tab. After 3 days, you’ll get an email welcoming you to the program, and you’ll be good to go.

Step 2: Create a product link

Once you’re admitted to the program, open up the “My Advertisers” sub-tab from the LinkShare Programs tab, and open the “Link Maker Tool”. This lets you search for products, and create a link that will open up your app’s App Store page, and will be associated with your LinkShare account.

Step 3: Create a download redirection script

Now we’re going to set up a script on your app site which will redirect the visitor to the URL you just created (which in turn, redirects straight to the App Store page). It’ll add a “signature” parameter to the URL, which corresponds to the original referrer, so you can track where orders came from.

Create a file called ‘download.php’ in the root of your app site, with the following content, with your LinkShare URL inserted where indicated:

// Replace the following URL with the LinkShare URL you created
$linkshare_url = "";

$referer = $_SESSION['original_referer'];
if ( !$referer ) $referer = $_SERVER["HTTP_REFERER"];

if ( $referer ) {
  $signature = preg_replace("@https?://(?:www.)?([^/]+?)(?*@", "$1", $referer);
} else {
  $signature = preg_replace("@^(?:www.)?(.+?)(?[email protected]", "$1", $_SERVER["HTTP_HOST"]);

$signature = preg_replace("@[^a-zA-Z0-9]@", "", $signature);

header("Location: ".$linkshare_url."&u1=".$signature);

This script looks for the original referrer in a session variable (which we’ll set up in the next step), so that the domain of the site that links to your app site is used, not just your app site’s domain. Then it creates a properly-formatted signature parameter (just alphanumeric), appends it to your LinkShare URL, and sends the viewer onwards.

Bonus points: I prefer to get rid of the ‘php’ extension to make the URL a bit cleaner. Pop the following into your .htaccess file to access ‘download.php’ as just ‘download’:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule . %{REQUEST_FILENAME}.php [L]

Step 4: Remember the referrer

Now, on the landing page script for your app site (or the site header), pop this in at the very start:

if ( !$_SESSION["original_referer"] ) $_SESSION["original_referer"] = $_SERVER["HTTP_REFERER"];

This stores the original referrer URL in a session variable, to use when we actually link the viewer through to the App Store.

Step 5: Test it

To make sure everything’s working properly, open download.php again, and replace “header” at the bottom with “echo”, so that instead of redirecting the browser, we just print out the URL where we would be redirecting to.

Open your appsite/download URL, and make sure the URL ends with “&u1=appsite”. That’s for direct visitors. Now, click through to your app site from another site, then click your “download” link. You should now see the name of the original site you linked from as the “u1” parameter at the end of the URL.

Once you’re satisfied that you’re good to go, make sure you replace “echo” with “header” again.

Step 6: Track

Now that you’re ready to track referrals, you can give out your https://yourappsite/download URL as your app’s direct iTunes download link (to reviewers, in your press releases, etc).

You can view a report showing clicks and orders associated with each referrer on the LinkShare page — create an advanced report by clicking the “Advanced Reports” sub-tab, then select your desired date range (I use “Since Last Payment”), and under “Report Type”, select “Signature Activity”. Hit “View Report”, and you’ll see your clicks and sales versus each referrer (“Member ID”, on the report).


Voila! Omnipotence achieved.

Two final notes about this approach: This technique works for tracking referrers, but if you’re wanting to track the performance of ads (say, with AdMob), you’ll want to use your original LinkShare URL, with a custom “&u1? signature parameter. As ad platforms like AdMob link directly, and don’t, as far as I know, send a referrer parameter, this script won’t pick up that it’s from your ad platform.

Finally, LinkShare’s reports don’t distinguish between products, so if you’ve multiple apps, you might want to add a prefix to your signature parameter to tell them apart. You could, for example, replace the header("Location: ".$linkshare_url."&u1=".$signature); line with something like header("Location: ".$linkshare_url."&u1=myapp".$signature);.