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.
- What is click tracking code and why is it needed
- Different code examples
- Simple click tracking code
- Asynchronous click tracking code
- PHP version of the click tracking code
- How to set Post Affiliate Network account ID
- How to tell the script that the default a_aid parameter has changed
- How to disable creation of cookies
- How to create cookie for top domain
- How to force saving click with specified attributes
- How to write visitor’s cookie ID or referring affiliate ID to site elements
- How to load referring affiliate ID to use in your code directly without writing it
- How to set own tracking cookie / PAPVisitorId
- 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:
- Correct URL to your PAP instead of URL_TO_PostAffiliatePro
- 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.
- 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.
- Code placed within section of your website so it can create all types of cookies.
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.
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":
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.
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:
disable HTTP 3rd party cookie:
disable HTML5 local storage cookie:
enable all cookies back (e.g. when visitor agree with cookies):
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:
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:
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.
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 link with 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 link with 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.
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.
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.
Simple Ajax with PHP click tracker
This tutorial shows how to track your vistors click using simple php and Ajax. To implement the click tracking tool we need to create 2 files:
Demo.html: This file contains the html with the links and the Ajax code.
clickTracker.php: This files will be called by Ajax and records the click event.DEMO.HTML will look like this
Test Click
As you can see the destination URL have been added to the function doTrack() as shown above. Replace that with the URL of your choice.
Next step is to create clickTracker.php file
".$src." : ".$dst." : ".$timestamp); fwrite ($f, "\n"); fclose($f); ?>
We will write data dynamically using AJAX to a simple text file, If you are not sure how to read/write data usinga text file in php then GO HERE
Don’t forget to create a file with name clickReport.txt. Also make sure clickReport.txt has the sufficient permission for the Apache/Nginx/ PHP fpm process to write.
Now you can track timestamp of your website clicks.
Some bugs fixed and Demo added on May 25, 2022
Related Tutorials: