Installing counter and setting up clicks tracking.

It's necessary to install a system counter at the website's pages to collect the statistics about the traffic at this website. Counter code is inserted into the HTML document body, i.e. between the tags <boby> (<boby>counter code</boby>). You are recommended to locate the counter as close to the beginning of the page as possible.
! As soon as the system counter is installed, the image will appear at the pages containing it.

Clicks.
Click analysis allows receiving the fullest information about the traffic at the site. In order to collect the information about clicks, you need to make sure that system can track all the clicks made at the site.
In order to do it, you will need to replace all links at your site with links to system counter, with the old link being stated in address parameters. As soon as the user clicks this link, system records the information about the click and redirects the user to the web address stated in link parameters.
Separate links with parameters received from CA system counter should be created on the page for every visit of the user.
There are two ways of replacing the old links: on the server or in the user's browser.
1. The old links are replaced on the server with the help of programming languages (PHP/Perl/C++) run on the server by means of generating a page with links to CA system. This is the most reliable method allowing tracking all clicks made at your site. This method might be rather difficult in case you use third-party content rotation scripts (rotators) at your sites.
2. Link replacement in user’s browser is carried out with the help of the JavaScript script provided below. This method is easy to implement but depends on the work of scripts in the user’s browser largely - it won’t work in a browser with deactivated scripts as well in some older browser versions.

! When the process of link replacement is complete, the links on the page must be to cacount.cgi script (<a href="/cgi-bin/cacount.cgi?cafn=click&y=16820416&z=WLnzfBiRMHPGEa&l=http://mysite.com/">My Site</a>).

PHP pages (http://domain.com/main.php?trader=ID).
Getting counter code ($COUNTER) and old link replacement parameters from CA system.

<?php
$CA_URL="/cgi-bin/cacount.cgi";    // change this value to your URL
$CA_PATH="./cgi-bin/cacount.cgi";  // change to your path to cacount.cgi  

if( isset($HTTP_SERVER_VARS) ) {  $SERVER_VARS=$HTTP_SERVER_VARS; }  
if( isset($_SERVER) ) {  $SERVER_VARS=$_SERVER; }  
foreach ($SERVER_VARS as $k => $v) {  
  if( strstr($k,"HTTP_") || strstr($k,"REMOTE_") || strstr($k,"QUERY_") ||   
      strstr($k,"REQUEST_") || strstr($k,"SERVER_") || strstr($k,"GEOIP_") ) {     
    putenv("$k=$v");
  }
}
$COUNTER=`$CA_PATH load`;
// $COUNTER=`$CA_PATH load GROUP`;

$CA_PREURL="";  // code reference is inserted before the old link
if( $COUNTER && preg_match("/cafn=rcntr&y=(\w+)&z=(\w+)&/", $COUNTER, $mathces)) {
 $CA_PREURL=$CA_URL."?cafn=click&y=$mathces[1]&z=$mathces[2]&l=";
} else {
 echo "Error exec ($CA_PATH)!";
}
echo $COUNTER; // output of the counter code, you can have anywhere in the page between the tags "body"
?>


Replacement of old links.
1. Replacement of old links on the server.
In order to replace the old links, you need to insert $CA_PREURL variable before the old URL.

Example links:

<a href="<?php echo $CA_PREURL; ?>http://domain1.com/">Link 1</a>
<a href="<?php echo $CA_PREURL; ?>http://domain2.com/">Link 2</a>


2.Replacement of old links in the user's browser.

<script language="JavaScript">
var nl=0, ldct=0;
var CA_PREURL="<?php echo $CA_PREURL; ?>";
function ldlk() {
  var els = document.getElementsByTagName('a');
  if(els.length!=nl) {
    nl=els.length;
    for ( i=0; i < nl; i++ ) {
      if(els[i].href.indexOf("cacount.cgi")<0)
        els[i].href=CA_PREURL+els[i].href;
    }
  } else {
    ldct++;
  }
  if(ldct<10)
    setTimeout(ldlk, 1000);
}
ldlk();
</script>

Perl pages (http://domain.com/cgi-bin/main.cgi?trader=ID).
Getting counter code ($COUNTER) and old link replacement parameters from CA system.

#!/usr/bin/perl
print "Content-type: text/html\n\n"  # output header (if necessary)
$CA_URL="/cgi-bin/cacount.cgi";      # change this value to your URL
$CA_PATH="./cgi-bin/cacount.cgi";    # change to your path to cacount.cgi

$CA_PREURL="";
$CACOUNT=`$CA_PATH load`;
# $CACOUNT=`$CA_PATH load GROUP`;
if( $CACOUNT ) {
 if( $CACOUNT =~ /cafn=rcntr&y=(\w+)&z=(\w+)&/ ) {
   $CA_PREURL=$CA_URL."?cafn=click&y=$1&z=$2&l=";	 
 }
} else {
 $CACOUNT="Error exec cacount.cgi!";
} 
print $CACOUNT;   # output of the counter code, you can have anywhere in the page between the tags "body"


Replacement of old links.
1. Replacement of old links on the server.
In order to replace the old links, you need to insert $CA_PREURL variable before the old URL.
Sample of a page with replaced links:

print <<EOF;
<html>
<head>
<title>MyPage</title>
</head>
<body>
<p><a href="$CA_PREURL$\http://domain1.com/">Site 1</a></p>
<p><a href="$CA_PREURL$\http://domain2.com/">Site 2</a></p>
<p><a href="$CA_PREURL$\http://domain3.com/">Site 3</a></p>
</body>
</html>
EOF


2.Replacement of old links in the user's browser.
Taking JavaScript code from Perl for old link replacement in the user's browser.

print <<EOF;
<script language="JavaScript">
var nl=0, ldct=0;
var CA_PREURL="$CA_PREURL";
function ldlk() {
  var els = document.getElementsByTagName('a');
  if(els.length!=nl) {
    nl=els.length;
    for ( i=0; i < nl; i++ ) {
      if(els[i].href.indexOf("cacount.cgi")<0)
        els[i].href=CA_PREURL+els[i].href;
    }
  } else {
    ldct++;
  }
  if(ldct<10)
    setTimeout(ldlk, 1000);
}
ldlk();
</script>
EOF