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. Asynchronous click tracking code
    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 tell the script that the default a_aid parameter has changed
    3. How to disable creation of cookies
    4. How to create cookie for top domain
    5. How to force saving click with specified attributes
    6. How to write visitor's cookie ID or referring affiliate ID to site elements
    7. How to load referring affiliate ID to use in your code directly without writing it
    8. How to set own tracking cookie / PAPVisitorId
    9. How to load the tracking cookie / ID / PAPVisitorId 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>
 

Asynchronous click tracking code

this version of click tracking code won't slow down loading of your page if accessing your Post Affiliate Pro installation takes a bit longer. On the other hand it might not track clicks of very fast visitors who do not wait for the website to fully load before navigating to another page. The same principles 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">
  (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>
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.setParamNameUserId('new_a_aid_parameter');
  PostAffTracker.disableTrackingMethod('C');
  PostAffTracker.setCookieDomain('maindomain.com');
  window.AffiliateID='affiliate id';
  window.BannerID='banner id';
  window.CampaignID='campaign id';
  window.Channel='channel';
  window.Data1='data1';
  window.Data2='data2';
  try {
    PostAffTracker.track();
  } catch (err) { }
  PostAffTracker.writeCookieToCustomField('id_field');
  PostAffTracker.writeCookieToLink('id_field', 'papCookie');
  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 tell the script that the default a_aid parameter has changed

If you have changed the Affiliate ID / referrer ID parameter under Configuration->URL Parameter Names section of your Post Affiliate Pro merchant panel you should have this function added in your click tracking. It must be added before the track() function.

PostAffTracker.setParamNameUserId('new_a_aid_parameter');

How to disable creation of cookies

In case you wish to disable cookie creation for any reason, you can use one of the following lines in your tracking code:

disable HTTP 1st party cookie:

PostAffTracker.disableTrackingMethod('1');

disable HTTP 3rd party cookie:

PostAffTracker.disableTrackingMethod('3');

disable HTML5 local storage cookie:

PostAffTracker.disableTrackingMethod('S');

disable all cookies:

PostAffTracker.disableTrackingMethod('C');

enable all cookies back (e.g. when visitor agree with cookies):

PostAffTracker.enableTrackingMethods();

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. Variables must be set as global.

window.AffiliateID='affiliate id';
window.BannerID='banner id';
window.CampaignID='campaign id';
window.Channel='channel';
window.Data1='data1';
window.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());
});

How to set own tracking cookie / PAPVisitorId

This option is useful if you have visitorId value and you want to store it to 1st party cookie and HTML5 local storage.

PostAffTracker.setVisitorId('visitoridvalue');

PostAffTracker.track(); //if you want to store it to 3rd party cookies or track click

How to load the tracking cookie / ID / PAPVisitorId to use in your code directly without writing it

This option is useful if you don't really need to write the cookie ID 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.executeOnResponseFinished.push(function() {
    var visitorid = PostAffTracker.getVisitorId(); //you can store the visitorid by custom script
    alert(visitorid );
});

Old function:

PostAffTracker.executeOnResponseFinished.push(function() {
    alert(PostAffTracker._getAccountId() + PostAffTracker._cmanager.getVisitorIdOrSaleCookieValue());
});
×