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.)?(.+?)(?$@", "$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 http://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);.

  • popo

    Hmm… help me out here. If you use LinkShare to redirect visitors to iTunes — doesn’t LinkShare take a chunk of the revenue? How does this affiliate stuff work again?

    • Michael Tyson

      Actually, nope – the 5% comes out of Apple’s cut. So basically, on sales that go via your affiliate link, you earn 75% rather than the usual 70%.

  • Dave

    How are you able to see how many people actually complete the transaction in the app store? Sure you can track the number of people that hit the link (which you could do with Google Analytics), but how can you connect the dots with the person that actually purchase the app? Won’t this also track people who go to the app store, but don’t complete the transaction? Or am I missing something?

    • Michael Tyson

      The secret ingredient is LinkShare’s arrangement with Apple – when a user is directed to the App Store via a LinkShare URL, they carry a tracking code along with them which, if they make a purchase, gets linked to that transaction.

      In turn, that gets carried back to the stats you see in LinkShare’s admin interface, so you can track the whole process.

  • Michael Seibel

    Does this work for free apps?

    • Michael Tyson

      My guess is that it wouldn’t – just because the useful part of the stats are the sales figures, which would be all zero for free apps. But I’m not 100% sure…

      • Ryan

        More and more apps are going free and monetizing with in-app transactions. So, no, the sales are not the useful part, they are just one part. Knowing who is sending your downloads, not just sales, is extremely important in marketing/advertising your apps. Without this piece, free apps are just “throwing darts in the dark.”

  • Mike Flat

    Hi Michael, That means the tracking system has no “margin of error”, otherwise is a huge loss for the brands using it right??

    • Michael Tyson

      I’d say so, Mike, yeah – it’ll be accurate.

      Mind you, it isn’t necessarily timely – I’ve noticed (and others have verified) that the sales stats are very delayed, and inconsistently so. I have some entries with hundreds of clicks, but 0 sales, which I know for a fact is incorrect. I suspect that this will all come good once the stats have had time to trickle through to the reporting interface, but it makes it tricky to track.

  • Pujangga Langit

    The advertiser got visitor
    and I’m Publisher got Nothing
    Yeah…Nice Site for advertiser and nice social Job for me :))

  • David Katz

    We have been using to track download referrals and post download conversions. We have very happy with the service. I honestly think that this is a must have service for every app.

  • Mark

    Hi Michael,

    This worked great for me until now. Do you know of a similar process for the new Apple Affiliate programme with PHG, as Apple are moving away from LinkShare altogether?



    • Christoph Eberhardt

      Hi Michael,

      thank you for this blog entry. Would it be possible to use this, say, for customer identification? Like if someone wants to share your app via a customised link and your app on another device which came to be installed via this link knows about this exact link. Such an “app referral” functionality would be great for our app, as it’s about achieving things together in a group.