Asynchronous tracking scripts

In case you need your scripts to be loaded asynchronously, try to follow this article on how to change it...

 

Default click tracking code

The default click tracking code which is available in your merchant panel looks like this:


<script type="text/javascript" id="pap_x2s6df8d" src="https://URL_TO_PostAffiliatePro/scripts/trackjs.js"></script>
<script type="text/javascript">
  PostAffTracker.setAccountId('default1');
  try {
    PostAffTracker.track();
  } catch (err) { }
</script>

With this code the code is loaded alongside all the other webpage content. This may result in slow loading of the webpage in case the Post Affiliate Pro server is slower or not responding. In case the tracking code is placed in beginning of the HTML body, the whole page would not display until the server with PAP or PAN responds... This can take from a few seconds to tens of seconds. We are sure this is something you would not like your customers to see.

To avoid this, (mainly in case your PAP or PAN server is slower) you should use asynchronous approach.

 

Asynchronous click tracking code

The difference between the standard and the asynchronous approach is in when the scripts are loaded. Standard approach loads scripts one by one while displaying the page in browser so in case of a problem the other content waits for the problematic script. In case of the asynchronous approach scripts are loaded after the page is displayed. The following code is the click tracking code which will be displayed asynchronously:


<script type="text/javascript">
  (function() {
    var papScriptHeader = document.createElement('script');
    papScriptHeader.src = 'https://URL_TO_PostAffiliatePro/scripts/trackjs.js';
    papScriptHeader.id = 'pap_x2s6df8d';
    papScriptHeader.type = 'text/javascript';
    papScriptHeader.onload = function() {
      try {
        PostAffTracker.setAccountId('default1');
        PostAffTracker.track();
      } catch (err) { }
    }
    document.body.appendChild(papScriptHeader);
  })();
</script>


The code has to be added somewhere in the HTML body of the page. Note that the asynchronous approach may result in missing clicks. As the code will be loaded after the whole page is loaded, it can happen that a "quick customer" (clicking too fast without waiting for the site to fully load) won't be tracked at all.

 

Asynchronous sale tracking code

The following code is the sale tracking code which will be displayed asynchronously:


<script type="text/javascript">
  (function() {
    var papScriptHeader = document.createElement('script');
    papScriptHeader.src = 'https://URL_TO_PostAffiliatePro/scripts/trackjs.js';
    papScriptHeader.id = 'pap_x2s6df8d';
    papScriptHeader.type = 'text/javascript';
    papScriptHeader.onload = function() {
      try {
        PostAffTracker.setAccountId('default1');
        var sale = PostAffTracker.createSale();
        sale.setTotalCost('120.50');
        sale.setOrderID('ORD_12345XYZ');
        sale.setProductID('test product');
        PostAffTracker.register();
      } catch (err) { }
    }
    document.body.appendChild(papScriptHeader);
  })();
</script>

NOTICE:
In all the codes here we are using an example URL to PAP. If you want to use these codes you must change the URL_TO_PostAffiliatePro to match the real URL of your installation of Post Affiliate Pro (Network).

In case of Post Affiliate Network you also have to make sure the account ID in function call PostAffTracker.setAccountId('default1'); is set correctly as well (each merchant can find their account ID in merchant panel Tools > Integration).

 

In the sale tracking code do not forget to replace the sample values of tracking parameters (setTotalCost() , setOrderID() etc.) with some dynamic variables of your shopping cart.

×