I’ve been having a few people ask me for my class to login and pull stats automatically from the different affiliate networks that run on Directtrack’s Affiliate Solution. In one of my previous posts there’s a section of the db called conversions. Well what that section is for is to put all your conversions in from Directtrack. If you go into your directtrack account and hit advanced stats you’re going to see all your subid’s you’ve passed for conversions. This class does that automatically. You’ll need to write a little script to implement the class. But it’ll save you a bunch of time on trying to figure out how to login, grab and parse your affiliate stats. To use this you’ll also need to setup a writable dir for you cookie files. Because when this script log’s in it needs to save a cookie so that it can tell copeac or cpa empire that you’re a verified user.
<?
class DirectTrack {
private $loggedin;
private $login;
private $password;
private $site;
private $cookiefile;
private $start_day;
private $start_month;
private $start_year;
private $end_day;
private $end_month;
private $end_year;
/**
* logs you into the dt system of your choice
*
* @param string $login – login for directtrack
* @param string $password – password for directtrack
* @param string $site – i.e. affiliates.copeac.com or monetizeit.net
* @param string $cookie – cookie location
*/
public function __construct($login,$password,$site,$cookie){
$this->loggedin = false;
$this->login = null;
$this->password = null;
$this->site = null;
$$this->cookiefile = null;
$this->start_day = null;
$this->start_month = null;
$this->start_year = null;
$this->end_day = null;
$this->end_month = null;
$this->end_year = null;
$this->setLogin($login);
$this->setPassword($password);
$this->setSite($site);
$this->setCookieLocation($cookie);
$this->DTLogin();
}
/**
* deletes the cookie file on destroying the object remember to unset your object
*
*/
public function __destruct(){
unlink($this->cookiefile);
}
/**
* login setter, url encodes
*
* @param string $login
*/
public function setLogin($login){ $this->login = urlencode($login); }
/**
* password setter, url encodes
*
* @param string $password
*/
public function setPassword($password){ $this->password = urlencode($password);}
/**
* Directtrack site domain setter
*
* @param string $site – i.e. affiliates.copeac.com or monetizeit.net
*/
public function setSite($site){ $this->site = $site;}
/**
* setter for the cookie directory, randomizes the cookie as well incase multi threads are running
* Please make sure your directory is writable
*
* @param string $cookie
*/
public function setCookieLocation($cookie){
$this->cookiefile = $cookie.”/cookie”.rand(11111111,99999999).”.txt”;
}
/**
* getter for the currently generated cookie file location
*
* @return string
*/
public function getCookieLocation(){return $this->cookiefile;}
/**
* Checks the status of being logged In
*
* @return array – array(‘msg’=> ”,’status’=>”) 0 logged out, 1 logged in.
*/
public function LoggedInStatus(){
if($this->loggedin){
return array(“msg”=>”Logged In!”,”status”=>1);
}else{
return array(“msg”=>”Not logged in!”,”status”=>0);
}
}
/**
* sets the start date for the report you’re looking for
*
* @param int $day
* @param int $month
* @param int $year
*/
public function setStartDate($day,$month,$year){
$this->start_day = $day;
$this->start_month = $month;
$this->start_year = $year;
}
/**
* sets the ending date for the report you’re looking for
*
* @param int $day
* @param int $month
* @param int $year
*/
public function setEndDate($day,$month,$year){
$this->end_day = $day;
$this->end_month = $month;
$this->end_year = $year;
}
/**
* Combo function which gets the advanced report from direct track that has all the
* specific sub-id data then parses it into a multi dimensional array
*
* @return array – array[line][fields]
*/
public function AdvancedStats(){
if(!$this->loggedin){
$this->DTLogin();
}
$stat_page = $this->DTfetchAdvancedStats();
return $this->csv2array($stat_page);
}
/**
* extends AdvnacedStats, and then inserts the data into the db
*
* @return array – advanced stats array incase you want it for something else
*/
public function AdvancedStats2DB(){
$stats = $this->AdvancedStats();
$this->DBconversionInsert($stats);
return $stats;
}
/**
* gets totals based on account for dates set
*
*/
public function Totals(){
$csv = $this->DTfetchTotals();
return $this->csv2array($csv);
}
/**
* NOT COMPLETE
* -Needs the regex to work properly
* -paging for sites that have multiple pages of results
*
* @return array
*/
public function AllOffers(){
$campaign_page = $this->DTfetchAllCamapigns();
$regex_sections = “/<\/a><\/td>.+?program_id=(\d*)&.+?\’>(.+?)<.+?top\’>\$*(.+?)/*<*.+?’>(.+?)<.+?’>(.+?)</s”;
preg_match_all($regex_sections,$campaign_page,$matches);
foreach($matches[0] as $section){
echo “<br><br>$section<br><br>”;
//get section name
$regex_section = “/Category: (.+?)</”;
preg_match($regex_section,$section,$match);
$section_name = “<b>”.$match[1].”</b>”;
$regex_offers = “/program_id=(\d*)&/s”;
preg_match_all($regex_offers,$section,$matches);
var_dump($matches);
exit;
$offers = array();
$sets = 0;
foreach ($matches as $data){
if($sets != 0){
$i = 0;
foreach ($data as $offer_data){
$offers[$i][] = $offer_data;
$i++;
}
}
$sets++;
exit;
}
array_unshift($offers,$section_name);
var_dump($offers);
}
return $offers;
exit;
}
//// Private //////
/**
* the function that logs into your directtrack account
*
* @return string – curl output
*/
private function DTLogin(){
$url = “https://$this->site/index.html”;
$post = “DL_AUTH_USERNAME=$this->login&DL_AUTH_PASSWORD=$this->password”;
$ch = new CurlPost($url,$post);
$ch->setCookie($this->cookiefile);
$ch->SSL();
$c_out = $ch->execute();
unset($ch);
if(1){ //parse for successful login
$this->loggedin = true;
}
return $c_out;
}
/**
* gets the advanced stats once logged in
*
* @return string – curl output – csv format
*/
private function DTfetchAdvancedStats(){
$url = “https://$this->site/partners/monthly_affiliate_stats.html?program_id=0&affiliate_stats_start_month=$this->start_month&affiliate_stats_start_day=$this->start_day&affiliate_stats_start_year=$this->start_year&affiliate_stats_end_month=$this->end_month&affiliate_stats_end_day=$this->end_day&affiliate_stats_end_year=$this->end_year&breakdown=cumulative&get_lead_info=Download+Optional+Info”;
$ch = new CurlGet($url);
$ch->useCookie($this->cookiefile);
$ch->SSL();
$curl_stats = $ch->execute();
unset($ch);
return $curl_stats;
}
/**
* breaks csv data into a multi-dim array with array[line][fields]
*
* @param string $curl_data – csv from advanced page
* @return array – multi-dim array[line][fields]
*/
private function csv2array($curl_data){
$return = array();
//lines to array
$stats = explode(“\n”,trim($curl_data));
foreach($stats as $line){
//fields to array
$fields = explode(“,”,trim($line));
$record = array();
foreach($fields as $field){
$record[] = trim($field);
}
$return[] = $record;
}
return $return;
}
/**
* takes the multi-dim array from DTparseAdvancedStats and inserts it into a db
*
* @param array $stats
*/
private function DBconversionInsert($stats){
$count = 0;
foreach($stats as $row){
if($count != 0){
//assignments
$click_id = trim($row[2]);
$source = $this->site;
$date = $this->_ChangeDateFormat($row[0]);
$campaign = addslashes(trim($row[1]));
$campaignID = trim($row[3]);
$transactionID = trim($row[4]);
$lineitemID = trim($row[5]);
$commission = $this->_CleanCommission($row[6]);
$sql = “INSERT INTO `conversions` (`click_id`,`source`,`date`,`campaign`,`campiagnID`,`transactionID`,`lineitemID`,`commission`) VALUES (‘$click_id’,’$source’,’$date’,’$campaign’,’$campaignID’,’$transactionID’,’$lineitemID’,’$commission’)”;
//echo “$sql<br>”;
if(!strpos($click_id,”blogs”))
{
mysql_query($sql);
}
//if(mysql_error()){ echo mysql_error().”\n”; }
}
$count ++;
}
}
/**
* gets the totals for all sales by campaign
*
* @return string – curl output
*/
private function DTfetchTotals(){
$url = “https://$this->site/partners/monthly_affiliate_stats.html?program_id=0&affiliate_stats_start_month=$this->start_month&affiliate_stats_start_day=$this->start_day&affiliate_stats_start_year=$this->start_year&affiliate_stats_end_month=$this->end_month&affiliate_stats_end_day=$this->end_day&affiliate_stats_end_year=$this->end_year&breakdown=cumulative&get_csv=1”;
$ch = new CurlGet($url);
$ch->useCookie($this->cookiefile);
$ch->SSL();
$curl_stats = $ch->execute();
unset($ch);
return $curl_stats;
}
/**
* once logged in curl to get the campaigns
*
* @return string – curl page
*/
private function DTfetchAllCamapigns(){
$url = “https://$this->site/partners/search_program_categories.html”;
$ch = new CurlGet($url);
$ch->useCookie($this->cookiefile);
$ch->SSL();
$curl_stats = $ch->execute();
unset($ch);
return $curl_stats;
}
// private functions
private function _ChangeDateFormat($date)
{
$parts = explode(“/”,$date);
$date = $parts[2].”-“.$parts[1].”-“.$parts[0];
//echo $date.”<br>”;
return $date;
}
private function _CleanCommission($dollar)
{
return str_ireplace(“$”,””,$dollar);
}
}
// THIS IS HOW YOU USE THE CLASS
//$cookieDir = “cookies”;
//$dt = new DirectTrack(‘my@email.com’,’password’,’copec.com’,$cookieDir);
//$dt->setStartDate(1,6,2007);
//$dt->setEndDate(18,6,2007);
//var_dump($dt->LoggedInStatus());
//echo $dt->getCookieLocation();
//var_dump($dt->AdvancedStats());
//var_dump($dt->Stats2DB());
//var_dump($dt->Totals());
//var_dump($dt->AllOffers());
//unset($dt);
?>
P.S. Sorry for the crappy display of the code. Unlike Uberaffiliate I don’t make money from my blog so it doesn’t get that much attention. 😀