Here is a simple ClickBank tracking script written in PHP that you can install on any of your websites free of charge. It can be used to track sales from AdWords and sales from EzineArticles and other article directories.
/ Z4 x8 b5 G4 \6 I# Z
6 s9 G+ Z. e8 z YInstallation. q3 S1 ?+ L# z) U* s* |2 u
Installation is simple, just copy the file shown below (after modifying it slightly) up to your web hosting service. If you』re doing EzineArticles-friendly domain serving, install it in the root of the server, otherwise you can just create a new folder and install it there. For example, I might create a folder called 「burnthefat」 and place the index.php file in there. Any subsequent access to 「http://www.mydomain.com/burnthefat/」 would invoke the script and do a redirection.
2 T0 j# s2 U9 o: V
* V" Y1 z6 X2 r( A. s" h( xBefore uploading the script, be sure to modify the default $affiliateid and $vendorid values. They are currently set to 『egiguere』 (my primary CB affiliate ID) and 『burnthefat』 (the vendor ID for Burn the Fat, Feed the Muscle). Set them to appropriate defaults. You can override these settings when invoking the script. (If you』re planning on using the domain with EzineArticles, you MUST set the affiliate and vendor ID values correctly because you can only link to the root of the URL with no parameters.)1 {9 y) v9 N. u" s
9 d1 V- x+ L) ]% R( D3 E1 @8 s7 z" @
Now this is totally optional, but you may also want to add these lines to a .htaccess file that you place in the same directory as the index.php file:/ e* r9 U3 a# ~1 \9 Y: n
* M6 L6 y/ N( s( l; a' l<Files *.log>) }3 e! t- w+ R, f
order allow,deny A6 t, ?- w% _! F
deny from all- [( M* _/ ]' h1 U
</Files>
' r2 \3 c5 U, J" x# v6 _9 HThese lines ensure that no one can download the .csv files the script creates — you』ll have to fetch them yourself using an FTP client. (Like I said, optional…)
: ^( l0 S8 m& i( X |
$ M3 `/ j4 Y- r/ j. c6 pTracking EzineArticles and Other Sites
& b& t/ B& J0 R. M) _3 W/ l! S8 rThe script looks at the referrer header and tries to build a tracking ID that incorporates part of the domain name and some kind of unique identifier. For EzineArticles, for example, it uses the 「id」 field (the article identifier). So you can easily tell which article of yours generated the click. It looks for 「id」, 「C」 (which is used by GoArticles) and 「kw」. You can easily modify it to look for other things.
! m1 n) |% ^; `7 ]* U" k% J
) G6 f8 D/ M2 W) g5 N* ^The tracking ID generated in these cases consists of the date and time (in MMDDHHSS format — 8 characters long, i.e. 「03091347〞), some portion of the domain name (i.e. 「ezineartic」), and the article ID (i.e. 「672234〞). So when you see this show up in your ClickBank report:2 s2 Q" }1 V& e; r' ?1 f$ i8 V
! G. {8 R6 G8 P* `& F) X8 a; _
03091347ezineartic672234
6 _ g) n- I+ c2 tyou』ll know the click came on 13:47 on March 9 from EzineArticles article #672234.3 {4 J$ u o% u" u. X0 T
! o! A. q1 e2 T! L0 g9 OTracking AdWords Clicks) S* g2 @4 A+ Z, s4 @& D
The script has some built-in features for tracking AdWords clicks, but only if you use a special syntax in your destination URLs, like so:
7 Z" l5 E+ S; h# E6 u" u: b' \0 J* ~& G+ o
http://www.yoursite.com/?ag=rf01&nw={ifsearch:s}{ifcontent:c}
- f% P( K9 D. u! z9 F&kw={keyword:none}&pl={placement}+ h; ~) w/ X+ {- B; h. \9 j
This is what the values mean:
6 j0 A3 t# f! N+ b# k9 {& U' c* J
ag — an identifier for the ad group or whatever unique thing you want to track, i.e. if you have two different ad texts you might want to assign two different values here5 I8 c( d' B: y
nw — the network being used. Note the special 「{ifsearch:s}{ifcontent:c}」 syntax. On the search network, the value will end up being 「nw=s」, on the content network it will end up being 「nw=c」. This is how you tell where the click comes from.) w7 `% d5 y t! m
kw — the keyword that triggered the ad, if known, or 「none」 if not known.$ H9 m Q* N9 e1 X
pl — the website the ad was featured on if it』s shown on a third-party site.
) p- n; x+ A1 ?: C8 B9 b% M: qAs you see, you can pack a lot of information into the destination URL. The script will store all this information in the CSV files it creates and it will use it to create a tracking ID. The tracking ID will start with the date (MMDDHHSS as before) followed by 「aw」 (for 「AdWords」), followed by one character for the network (「s」 for search, 「c」 for content, 「u」 if unknown), followed by the ad group (the 「ag」 value), followed by as much of the keyword (the 「kw」 value) as will fit. (Tracking IDs are limited to 24 characters, remember.)
& P- j6 p- L% D5 g; A" v$ h" G8 N: H2 b" S I/ V8 k' P T" Y
Other Sources6 L# o( Y) ]5 |9 B* H+ `
If the script doesn』t have enough information to create a (semi)human-readable tracking ID, it generates a random number and prefixes it with the date (in MMDDHHSS format) and uses that as the tracking ID.
i1 t& P' P6 ?. H) U4 `
% T# W) j5 q! w* M0 r3 V GOverriding Affiliate and Vendor IDs( a* M2 c8 H9 S7 g
You can set the affiliate ID and vendor ID explicitly by using the 「affiliateid」 and 「vendorid」 query parameters, as in:* Q6 S% k9 u* U+ p8 A* z. y7 q) ?* I, U# X
; M: Z. m+ l& p2 r7 X' Q5 E
http://www.feedthemuscleburnthefat.com/?vendorid=4idiots
- @9 b; K( Q Z y4 [. m9 ?This is useful if you want to use the same script for multiple redirections.
$ y% a& ^9 A+ H; }
* x; c# v3 G9 T# X9 [Analyzing the Data in Detail4 o M) F. Z5 S9 u0 ?' u: v. Y
The tracking IDs the script creates are meant to be easily eyeballed in the ClickBank sales report. But if you want to know more about the context of a particular tracking ID, the script stores information in simple CSV (comma-separated value) files that you can easily download and load into a spreadsheet application like Microsoft Excel. A separate file is created for each day, so the clicks for March 8, 2010 are found in 「clicks.2010-03-08.csv」, in the same folder as the index.php file.
7 l6 O" g0 ?+ ~# n. U
- `' o/ I4 r! k) O h4 [$ rSo when you see a click in your ClickBank sales report and you want to know more about it, look at the first 4 characters of the tracking ID to extract the month and day to know which file to download and open in Excel. The following data is stored, in this order:+ P+ Y' }. I; n# Z) c3 `; |
: E+ `; `) B' ?
The full date and time5 J( K7 |6 a, H$ s
The tracking ID4 c: `; z+ q* w+ [( X7 T* v2 R
The final affiliate ID, R; ?! T+ [) x
The visitor』s IP address- f3 H0 V+ [ w) _' U
The full path that was requested, including any parameters
3 @+ T+ u) P: f9 [3 aThe referrer URL, if any: X6 d" C/ a, S( Q R
The user-agent header, if any (useful for discerning robots from humans)- l4 S% e$ K* \; O' X8 L6 f
The Script. ?6 O* @! \0 Q; v K$ K5 S: w* z% s
Here it is. Either download this text file or copy the text below; place the text into a file called index.php and copy it up to your webserver.
$ d8 I. l# K* J$ R+ E! C9 c Z, G% z1 G
<?php
3 k) N& p6 q" ?* N//*************************************************************
9 T$ j" e" f3 Y- _//# c9 a7 V5 @. ?& Z6 H
// ClickBank Tracking Script* Y3 ]* m# ?& P, I+ h$ |( r+ ~
// Version 1.0+ B" }7 C: D% V/ z. S& R5 V
// Copyright 2010 by Eric Giguere
u; W" k5 c! ]( m: S// [email protected]
4 |3 I# e/ s3 o' x X//
Q0 H2 V n7 L' e& x3 w// You may use this script for free on as many sites as you want.; V1 A% I8 B D# ?4 N
// You cannot sell this script or otherwise claim ownership.* R0 C: |. m4 m: U
// This script is provided as-is, no warranties are implied.; J8 i# N$ ~" G" o, e$ N/ {# f
//: H! w8 l* d5 @6 N! g
// For installation instructions, see
8 r: O1 q9 y- C9 G( z q4 o! t7 e' |// http://www.memwg.com/clickbank-tracking-script/
! h" y1 S6 ^! v; W' O+ h/// E) E; N1 P: I% Y9 Q% Z5 M
//*************************************************************' H. M5 ^% L3 _7 h# g
// Modify these values to set your affiliate ID, the vendor ID,6 R o8 V" i: Z8 o
// and any additional data (i.e. '&page=1928') to append to the
7 G+ Q. N: y2 G2 C5 }// final hoplink after the tracking ID.
9 ]* h# ~3 f4 w9 z- K9 Y% w//*************************************************************
8 y7 Y: K4 ?; d+ l2 H$affiliateid = $_GET['affiliateid'];# w1 o, h. i# j8 H3 F
$vendorid = $_GET['vendorid'];
- E# `' M$ n, |0 u$extra = $_GET['extra'];
2 p5 C5 D# I8 p% ?: q1 ]0 [% H/ ~if( empty( $affiliateid ) ) $affiliateid = 'egiguere';
" S8 m3 P4 z. [) G5 zif( empty( $vendorid ) ) $vendorid = 'burnthefat';
7 e' [% R" I' k' s//*************************************************************& k6 P9 R" |1 }
// You shouldn't need to change anything below this point., r9 B5 T& s v# F
//*************************************************************
4 O+ [8 W4 `, r* P+ A// Gather information about the visitor.
' r) r5 x4 j0 x1 ?3 k$ip = $_SERVER['REMOTE_ADDR'];
' @$ x9 I$ X0 s+ L0 V$referrer = $_SERVER['HTTP_REFERER'];8 T. t2 c5 @, W& T" B U% u b
$browser = $_SERVER['HTTP_USER_AGENT'];
# ~; \$ R6 N$ V8 j5 H$request = $_SERVER['REQUEST_URI'];# M) _% g- J$ i
$reqtime = date( 'Y/m/d H:i:s T' );' w( l/ G: E! ^) r
$logfile = 'clicks.' . date( 'Y-m-d' ) . '.csv';
- g, i" p: m6 ~4 }' L5 z$prefix = date( 'mdHi' );0 T9 Y8 E3 S5 L1 j& u% `3 m: w
$source = '';% W0 B! a# x8 Y$ v
$tid = '';
" v. g3 W, J7 P8 p2 S6 F- }3 U// Check query parameters to determine if we are being called8 B$ a# @0 s$ Q9 `& z
// from AdWords. If we are, gather the requisite info. If not,
* A5 m9 d% h* P" b: E// try to figure out where we're being called from using the
: s9 {0 `9 @& _; A// referrer header.
7 S: C0 p L! Iif( !empty( $_GET['kw'] ) || !empty( $_GET['ag'] ) || !empty( $_GET['nw'] ) ){
. l( D$ ]8 G4 D m; w0 z4 ]/ r$nw = strtolower( $_GET['nw'] );
; L, w: I/ e2 r, Vif( strlen( $nw ) != 1 ){
0 H/ a% _) _3 m7 J$nw = 'u';# h, a1 W+ a) p! U% y
}
5 C( a( U. p: b$ Q' U$source = 'aw' . $nw;* B1 n r" j5 n( ^3 m8 |
$keyword = $_GET['kw'];
' I6 w0 q1 x" c' k$adgroup = $_GET['ag'];
" O4 M; V. A" l: D% r$tid = substr( $prefix . $source . $adgroup . makealphanum( $keyword ), 0, 24 );/ K' ]+ J! ^' _* G+ M
} 2 g2 m8 L3 O6 r7 C
// If we weren't called from AdWords, check the referrer header and
. A" `5 G" m0 a! @! b5 o2 j// see if we can suss out an identifier of some kind and combine it
0 L9 Y! z5 ?8 w/ u9 @// with the domain name to create a tracking ID.9 l; h3 N3 n0 {8 d1 g! S6 ^/ Q
if( empty( $tid ) && !empty( $referrer ) ){
( @+ S! K% B+ v$referrerparts = parse_url( trim( $referrer ) );
2 h! d+ K% B _+ Z$host = ( $referrerparts['host'] ? $referrerparts['host'] :/ p; Y- E& g- x
array_shift( explode( '/', $referrerparts['path'], 2 ) ) );) a+ P# {" v# ~& q( A
$params = parse_query( $referrerparts['query'] );. N# T! U' d( X7 H/ V# F
$hostparts = preg_split( '/\./', $host );' p- K, _/ k2 b& U5 `4 b5 }
$tld = count( $hostparts ) - 1;' B3 @& v; x3 s8 g* d
if( $tld > 0 ){8 R( ?9 P Y6 z1 N$ y% O& w3 v
$domain = $hostparts[$tld-1];
% I# l8 M/ m8 n$ids = array( 'id', 'C', 'kw' );
- j5 S7 v# X) E0 z6 Hforeach( $ids as $param ){2 b, Q7 Q, V" M& V: F
if( !empty( $params[$param] ) ){
/ t" X7 E* F2 Q& F+ J; X$source = makealphanum( $params[$param] );; C/ o5 S+ p, |& s! V
}
5 ^+ G$ \: v7 I6 ~if( $source ) break;
. g( D3 c7 g9 d) F, H}. }& i( V8 \2 r- g" Y) e+ }
if( !empty( $source ) ){1 R0 e4 S3 d0 m; X% @; x
$tid = $prefix . substr( $domain . $source, -16 );
( u/ ^9 K: p" Y; J w% i}' J# l$ R2 T3 T: l1 [
}
3 n5 Z" _3 X, O! a5 W; }}3 ]2 l# i9 g' ]+ i0 C5 Z8 |2 N
// Last resort: we weren't able to create a tracking ID, so$ W, g& }, E3 b9 _% t) D2 \
// generate a unique string to server as our ID.
, \" v; H6 }+ U$ C% eif( empty( $tid ) ){: l- y: }3 L/ P
$tid = substr( getfaketid( $prefix ), 0, 24 );2 X+ y& ~7 o2 D: H, w
}
0 s7 }& L: \3 c5 f+ }/ y/ }$afflink = 'http://' . $affiliateid . '.' . $vendorid .
6 N. s2 H9 T) b* K$ [2 R'.hop.clickbank.net/?tid=' . $tid . $extra;: S/ |' A \* ~. g3 g" _- K
header( "Location: $afflink" );
# H9 d5 r- h( c- T% I+ R// Write out the data to our CSV file9 Y" j% H3 k* ^+ Q: w; N& n
$fp = fopen( $logfile, 'a' );' d6 @1 i0 d# ?3 l0 }
if( $fp ){# G: T( g% Q2 {
fputs( $fp, logmsg() );! `" \7 T" c1 K* Z8 l$ G( d
fclose( $fp );8 b4 P2 J' ]; ^) s: J
}1 r) ^: j+ z+ L: N* H
//*************************************************************, M' j6 |5 A* `0 \. e
// Functions used by the code above.... Q" k) }( w+ M' b8 |$ X
//*************************************************************
$ s0 t' H3 v' Q2 K% V' L8 ^6 s// Convert the string to a lower case alphanumeric-only string
6 @2 O3 m4 q' n8 `/ Z9 Y' e! ^function makealphanum( $str ){
6 |8 l* H, v' r% nreturn ereg_replace( '[^a-z0-9]', '', strtolower( urldecode( $str ) ) );
7 w5 f7 h+ H* ]8 J}
9 y; `3 l! [# }# n% L// Parse a query string into its constituent parts, T- [& f: F- T! x* ~
function parse_query( $var ){9 s& j. \* h' p
$var = html_entity_decode( $var );
/ \: a$ \& T8 z9 K. C d$var = explode( '&', $var ); z, G9 h- r% I+ ]5 E
$arr = array();
$ X3 {) P6 t+ [& c+ {( \foreach( $var as $val ){
- c# V2 ?" ~" ~& s& ^$x = explode( '=', $val );
. `! y0 }7 F* E9 O) g9 `/ C, t4 `$arr[$x[0]] = $x[1];4 t7 ^* _0 z5 b2 T `* `
}
0 Q; @# M1 [1 g( q! ?2 Punset( $val, $x, $var );% z5 C) T" p& f1 B
return $arr;6 Q$ }3 P: U0 B5 x% M
}
, V M' Z9 _0 e' W' K/ ?// Encode URLs for saving in the CSV file by converting
+ X7 M( o7 z* ?& t1 z// quotes and commas to URL escapes./ A0 T$ c8 U' f% k, u' i; J( g8 G
function csvencode( $str ){% M* j! o8 x: w" f! v1 {" B1 A8 T
$str = str_replace( '"', '%22', $str );; L0 c. Z1 _- A! R; C
$str = str_replace( ',', '%2C', $str );
# _% a, | A- A. Z [return $str;
, Y- n, s/ f, r. p; h0 V8 Q# R}
7 U. {5 {$ ?: a; b) r& T* r// Create the line of CSV data to append to the log file
- k: |3 {. }# O J# M/ i, z! Q+ wfunction logmsg(){ t7 I. m3 T% {! b- h
global $request;
! h$ [8 j9 p/ [" Zglobal $ip;! A# w, S' X2 W' }! `
global $referrer;
4 B* s; O ~3 ]" r" Oglobal $browser;6 i% m( N5 y# L! @' b3 u0 L7 c
global $tid;/ N5 ~/ t9 ?% u2 x& Q
global $reqtime;8 `: \% g- f; h5 u! P
global $afflink;
i8 U L. m2 w1 w3 M0 [; x$msg = "$reqtime,$tid,$afflink,$ip," . csvencode( $request ) . ","
3 T: @. G6 Z; ~! V3 A" S. csvencode( $referrer ) . "," . csvencode( $browser );' L9 E$ D" ^* B; `5 N9 F) X7 }
return $msg . "\n";
" R* j3 ~" f. ]2 Q6 v8 V}
6 `/ M h2 I/ r: c- h8 W// Generate a fake TID with the given prefix., g5 q& `0 i: L! h
function getfaketid( $prefix ){
# J6 b% y& ?: L& C1 [' Nreturn uniqid( $prefix );- ]/ E1 I# F! k/ U
}
* g9 Y& n) o* H2 k- M" D4 H, d?>
; G0 [9 G. \: e) _$ y- mFinal Thoughts
z3 @ E: X5 @OK, user-friendly this isn』t, I admit it. It』s very geeky. But if you』re even a bit technically inclined you should be able to install this script and use it. If you have questions about it, please leave them as comments here rather than mailing me, it』ll be more useful for others.+ d2 z- o. A! o% z e0 x9 b. l
|