Let's imagine, that you are a developer of Android or Apple iOS applications and you would like to know the  top referring source of purchases of your application or of in-apps of your application.

This article is meant to be read by actual mobile app developers!
Don't expect to understand all the steps if you are not a mobile app developer.

Maybe you would say that Google Play and Apple Store allows you to do so, but unfortunately that is not the case. This article is going to shed some light on tracking purchases/downloads of mobile applications (or in-apps) on Android and Apple iOS for iPhones and iPads. It will show you also an example how to keep track of conversions brought by Google Adwords. Let's begin: The initial setup of Post Affiliate Pro campaign and banner differs when using a hosted solution of Post Affiliate Pro or owned solution of Post Affiliate Pro.

Case: Hosted Post Affiliate Pro

In Post Affiliate Pro (PAP) you need to create a campaign and choose "Standard link (redirect)" linking method in this campaign. It is essential. Then you need to create a banner (link banner) that will be assigned to the particular campaign.

The destination URL of the banner has to be the URL leading to the site of your application in Google Play or Apple Store

Sub-case: using GoogleAdwords: https://support.qualityunit.com/196374-Anchor-links

Afterwards, let's have some dummy affiliate, e.g. 'GA Affiliate' whose referral id will be 'gaaffiliate'. Let's log in to the account of that particular affiliate and find the link of the banner we created. This banner will show an URL leading to: PAP_URL/scripts/click.php?a_aid=gaaffiliate&a_bid=someBannerId Now you need to place that URL into Google AdWords.

This way, you ensure, that whoever clicks the particular Google AdWords add he/she will be redirected to the page of your application in Google Play or Apple Store while there will be a click tracked by PAP.

Case: Owned solution of Post Affiliate Pro

In an owned (self-hosted) Post Affiliate Pro (PAP) you would leave the campaign to use 'new style link' or 'anchor links' linking method. The Destination URL of the link banner would point a custom site of yours where you would have the following script:

<html>
<head> </head>
<body>
    <script type="text/javascript">
        document.write(unescape("%3Cscript id='pap_x2s6df8d' src='" + (("https:" == document.location.protocol) ? "https://" : "https://") + "URL_To_PAP/scripts/trackjs.js' type='text/javascript'%3E%3C/script%3E"));
    </script>
    <script type="text/javascript">
        PostAffTracker.setAccountId('default1');

        function detectIOS() {
            return (navigator.userAgent.toLowerCase().indexOf("iphone") > -1);
        }

        function detectAndroid() {
            return (navigator.userAgent.toLowerCase().indexOf("android") > -1);
        }
        try {
            PostAffTracker.track();
            if (detectIOS()) {
                window.location = "URL_To_Your_Application_At_The_Apple_Market";
            } else if (detectAndroid()) {
                window.location = "URL_To_Your_Application_At_The_Android_Market";
            } else {
                window.location = "Your_Main_Site";
            }
        } catch (err) {}
    </script>
</body>
</html>

The script above handles the redirection to the proper store based on the operating system of the mobile device the customer uses.

Now let's track the particular action (download/purchase). In case a user downloads and installs your application an invisible WebView (a component to display a web page) will be executed  and by that there will be a URL open that leads to the sale tracking script (can be downloaded from PAP).   An example of such a code is below:

<html>
<head> </head>
<body>
    <script type="text/javascript">
        document.write(unescape("%3Cscript id=%27pap_x2s6df8d%27 src=%27" + (("https:" == document.location.protocol) ? "https://" : "https://") + "URL_TO_PAP/scripts/trackjs.js%27 type=%27text/javascript%27%3E%3C/script%3E"));
    </script>
    <script type="text/javascript">
        // Better not to touch this...
        PostAffTracker.setAccountId('default1');

        function setData(type, data) {
            // create new sale
            var sale = PostAffTracker.createSale();
            // Without these parameters it does not work at all.
            // Order id and product id must not be empty,
            // total cost must not be equal to 0,
            // otherwise PAP does not track it as a sale.
            sale.setOrderID('order_id');
            sale.setTotalCost('1.00');
            sale.setProductID("Some Product ID or Product Name");
            if (type) {
                if (type == "1") {
                    sale.setData1('download');
                } else if (type == "2") {
                    sale.setData1('purchase');
                }
            }
            if (data && typeof(data) == "object") {
                if (data.ProductId) {
                    sale.setProductID(data.ProductId);
                }
                if (data.Device) {
                    if (data.Device == "1") {
                        sale.setData2("iOS");
                    } else if (data.Device == "2") {
                        sale.setData2("Android");
                    }
                }
            }
        }

        function registerSale() {
            // register all created sales (setData may be called more than once)
            PostAffTracker.register();
        }
    </script>
</body>
</html>

In that code there are 2 very important functions: setData() and registerSale() The setData() function is called from the mobile application (both Android and iOS allow to call and execute through the mentioned WebView component JS functions) and sets different action parameters: - is it a download or purchase? - what product? - what OS? - etc.

There are many options concerning action parameters, since the PAP sale tracking code provides up to 5 extra data to be tracked – so you can use the setData() function multiple times. After having all of those action parameters set it is time to call the registerSale() function that will forward all of the tracked actions to PAP, while the tracked transaction will be assigned to the proper affiliate.

Example WebView script for Android:

WebView webView = new WebView(dataHelper.getContext());
webView.getSettings().setJavaScriptEnabled(true);
// webView.getSettings().setCacheMode(webView.getSettings().LOAD_NO_CACHE);
class JavaScriptInterface {
    Context mContext;
    WebView webView;
    JavaScriptInterface(Context c, WebView w) {
        mContext = c;
        webView = w;
    }
    public void loadData() {
        for (Pair < String, String > event: events) {
            final Pair < String, String > e = event;
            webView.loadUrl("javascript:setData('" + e.first + "', " + e.second + ")");
        }
        webView.loadUrl("javascript:registerSale()");
    }
    public void showToast(String text) {
        Log.d("", text);
    }
}
webView.loadUrl(Settings.PAP_SALE_URL);
// DO NOT FORGET TO SET HERE THE URL LEADING TO YOUR SALE TRACKING SCRIPT