Clicks (referrals) tracking

In this article we are going to explain what is click tracking code, why is it needed, how does it looks like and what additional options you can do with it. All the code examples are clickable and show you how the specific function is added in default code.

  1. What is click tracking code and why is it needed
  2. Different code examples
    1. Simple click tracking code
    2. Click tracking code with automatic protocol selection
    3. PHP version of the click tracking code
  3. Advanced click tracking code (additional functions and paramters)
    1. How to set Post Affiliate Network account ID
    2. How to disable creation of flash cookies
    3. How to create cookie for top domain
    4. How to force saving click with specified attributes
    5. How to write visitor's cookie ID or referring affiliate ID to site elements
    6. How to load referring affiliate ID to use in your code directly without writing it

 

What is click tracking code and why is it needed

To track affiliate referrals (both clicks and sales), your target pages (landing pages) has to contain click tracking code. Even if you want to track sales only, you have to insert click tracking code first. Without it, the sale will be registered to default affiliate or won't be registered at all.
The target page is the page to which your banners/links point. Usually the best practice is to put click tracking code somewhere into general footer template, so it is included in every page of your website.

To get the integration code you need to go into your Post Affiliate Pro (Network) merchant panel and copy the code from Tools->Integration->Click tracking.


 

Different code examples

Simple click tracking code

this is the simple version of the code, it has 4 requirements:

  1. Correct URL to your PAP instead of URL_TO_PostAffiliatePro
  2. Correct protocol HTTP/HTTPS, if the website which you are tracking has SSL certificate so is available via HTTPS you need to have SSL certificate for your PAP as well so it is available with HTTPS as well.
  3. Correct AccountId in PostAffTracker.setAccountId() line. All Post Affiliate Pro owners will always have account ID default1. Owners of Post Affiliate Network will find the account ID in Accounts->Accounts manager part of their owner panel.
  4. Code placed within <body> </body> section of your website so it can create all types of cookies.
<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>
 

Click tracking code with automatic protocol selection

this version of click tracking code will automatically chose the protocol with which it will try to access PAP based on protocol of your website, but the same applies as for the Simple code. Sometimes your website might not like advanced javascript functions so you will have to use the Simple code.

<script type="text/javascript">
  var papScriptHeader = document.createElement('script');
  papScriptHeader.setAttribute('src','//URL_TO_PostAffiliatePro/scripts/trackjs.js');
  papScriptHeader.setAttribute('id','pap_x2s6df8d');
  papScriptHeader.setAttribute('type','text/javascript');
  papScriptHeader.setAttribute('onload','papTrackingFunction()');
  document.getElementsByTagName('body')[0].appendChild(papScriptHeader);
  function papTrackingFunction() {
    PostAffTracker.setAccountId('default1');
    try {
      PostAffTracker.track();
    } catch (err) { }
  }
</script>
PHP version of the click tracking code
PHP version is similar to the javascript one. You can find the code and documentation here.

 

Advanced click tracking code (additional functions and paramters)

In click tracking code you can use the following additional variables and functions, the placing in the below code is important, it matters what you place above and what below the PostAffTracker.track() function so please amend your code with this in mind.
 
Here is how the code with all available options look like. Most probably you will never use a full code like this. Below is the explanation of each option.
<script type="text/javascript" id="pap_x2s6df8d" src="https://URL_TO_PostAffiliatePro/scripts/trackjs.js"></script>
<script type="text/javascript">
  PostAffTracker.setAccountId('default1');
  PostAffTracker.disableTrackingMethod('F');
  PostAffTracker.setCookieDomain('maindomain.com');
  var AffiliateID='affiliate id';
  var BannerID='banner id';
  var CampaignID='campaign id';
  var Channel='channel';
  var Data1='data1';
  var Data2='data2';
  try {
    PostAffTracker.track();
  } catch (err) { }
  PostAffTracker.writeCookieToCustomField('id_field');
  PostAffTracker.writeCookieToLink('id_field', 'papCookie');
  PostAffTracker.setParamNameUserId('a_aidCustom');
  PostAffTracker.writeAffiliateToCustomField('id_field');
  PostAffTracker.writeAffiliateToLink('id_field', 'a_aid');
</script>

How to set Post Affiliate Network account ID

ID of your network merchant account is set in the following function. Post Affiliate Pro owners will always have ID "default1":

PostAffTracker.setAccountId('default1');

How to disable creation of flash cookies

In case you wish to disable flash cookie creation for any reason, then you need to add the following line to your code:

PostAffTracker.disableTrackingMethod('F');

How to create cookie for top domain

In case you add your click tracking code to all your domains and subdomains (highly recommended) then you don't need to use this option.

However if you don't want to add click tracking code everywhere and your landing page is a subdomain and payment page is either the top domain or a different subdomain, then you need to create the 1st party cookie for the top domain instead of the landing subdomain. Example to make it more clear:

  • landing page is landing.maindomain.com
  • checkout is at checkout.maindomain.com

One option is to add click tracking to both subdomains, but if you want your click tracking only on landing.maindomain.com, then you must create the cookie for maindomain.com. Normally it would be created only on landing.website.com. The following function would help:

PostAffTracker.setCookieDomain('maindomain.com');

How to force saving click with specified attributes

There are many uses for the following. For example you want all visits to website A to be tracked in website A campaign so you would force a campaign. Other example is that you have a dedicated affiliate page which should tie the visitor to affiliate on visit so you would force an affiliate in the code.

You can use these options to force stuff into the click. If you force affiliate, then all visits of the page will be saved as clicks for that affiliate.

var AffiliateID='affiliate id';
var BannerID='banner id';
var CampaignID='campaign id';
var Channel='channel';
var Data1='data1';
var Data2='data2';

You can read more about Channel, Data1 and Data2 variables in our SubID article.

How to write visitor's cookie ID or referring affiliate ID to site elements

this section is useful mainly for server side integrations where you need to save/pass the visitor's identification so the server can work with it afterwards. For example you need to read the visitor's cookie and save it in your database so that your server can later execute postbacks to your PAP. In that case the ideal place for saving the cookie is during the visitor's registration on your website so you would add the cookie to hidden input field in your form.

  • The writeCookie functions return a 40 characters long string where the first 8 characters is the AccountId and the last 32 characters are the visitorId, so you'd need to split the string before you send it to sale.php.
  • Function setParamNameUserId() is available since 5.6.1.1 and is used when in your Configuration->URL parameter names you have defined a different Affiliate ID parameter, than the default a_aid.
    The function tells the writeAffiliate functions to look into the URL of the opened page for that specific parameter in case your writeAffiliate function will not recognize affiliate from cookie. This does NOT need to be used with the writeCookie functions.
  • Affiliate might not be recognized from cookie if the affiliate link pointed directly to the page where the writeAffiliate function was used because the click on link was not processed yet by PAP, it can take a minute. You might want to check if the affiliate ID was actually written to your field/link before the page with this code is left by the customer and execute your writeAffiliate function again. This does NOT apply to the writeCookie functions.
// writes the cookie value to an input field inside your HTML with defined ID.
PostAffTracker.writeCookieToCustomField('id_field');

// appends the cookie value to <a> link with id="id_field" after papCookie URL parameter. 
PostAffTracker.writeCookieToLink('id_field', 'papCookie');

// if you use different affiliate parameter than a_aid in Configuration->URL parameter names specify it here.
PostAffTracker.setParamNameUserId('a_aidCustom');

// writes the ID of the referring affiliate to an input field inside your HTML with defined ID. 
PostAffTracker.writeAffiliateToCustomField('id_field');

// writes the ID of the referring affiliate to <a> link with id="id_field" after a_aid URL parameter.
PostAffTracker.writeAffiliateToLink('id_field', 'a_aid');

How to load referring affiliate ID to use in your code directly without writing it

available since 5.5.55.1. This option is useful if you don't really need to write the ID of the referring affiliate to any element of your website but you want to use it directly in your code. Without this new option you'd need to use some timeout or interval to check whether the ID was written to your element to get the ID from there. With this new option you don't need to write the ID at all.


PostAffTracker.getAffInfo().call(function() {
	alert(PostAffTracker.getAffInfo().getAffiliateId());
});