This is an error 404
There are `0` results
preferences:
1584.69 ms | 1399 KiB | 21 Q<?php
/*
Actions will be taken based on a threshold for the conversion rates set in the offer reference ID. The offer reference ID should be in this format: FirstGoalName_min=10;FirstGoalName_max=15;SecondGoalName_min=10;SecondGoalName_max=15
For example: offer ID 11421 would be Email_min=10;Email_max=15;FB_min=10;FB_max=15;Twitter_min=10;Twitter_max=15
The values on lines 9 - 12 must be set correctly
*/
$NetworkID = "revenues"; //CHANGE THIS TO YOUR HASOFFERS NETWORK ID
$NetworkToken = "NETUFdVdjMV89ZW1ojl23DJxVgZOJn"; //CHANGE THIS TO YOUR HASOFFERS API KEY
$ApiDomain = "http://revenues.api.hasoffers.com"; //CHANGE THIS TO YOUR HASOFFERS NETWORK_ID.api.hasoffers.com
$GroupId = "6";//CHANGE THIS TO THE OFFER GROUP ID WHICH IS BEING PROCESSED
//looks up the offer IDs from the offer group set above
$offerIds = array ();
$goal = GetOfferIds($NetworkID, $NetworkToken, $ApiDomain, $GroupId);
if ($goal['status'] != '1'){
file_put_contents('GetOfferIds_errorLog.txt', print_r($goal, true), FILE_APPEND);
exit("There was an error pulling GetOfferIds from HasOffers. Check the Error Log");
}
foreach ($goal['data']['data'] as $offer){
array_push ($offerIds, $offer['Offer']['id']);
}
echo "Offer IDs taken from the offer group: <br>";
print_r ($offerIds);
echo "<br>";
//Pulls a report for the last 7 days which is sorted by goal ID
$start_date = date_create(date("Y/m/d"));
$start_date = date_sub($start_date, date_interval_create_from_date_string("300 days"));
$start_date = date_format($start_date,"Y-m-d");
$end_date = date_create(date("Y/m/d"));
$end_date = date_format($end_date,"Y-m-d");
$HOreport = getStats($NetworkID, $NetworkToken, $ApiDomain, $start_date, $end_date, $offerIds, "1");
if ($HOreport['status'] != '1'){
file_put_contents('getStats_errorLog.txt', print_r($HOreport, true), FILE_APPEND);
exit("There was an error pulling stats from HasOffers. Check the Error Log");
}
echo "Raw report: <br>";
print_r ($HOreport);
echo "<br>";
//handling for more than one page of data being returned in the getStats API call
$stats = array();
array_push ($stats, $HOreport['data']['data']);
$pageCount = $HOreport['data']['pageCount'];
for ($x = 2; $x <= $pageCount; $x++){
$HOreportPages = getStats($NetworkID, $NetworkToken, $ApiDomain, $start_date, $end_date, $offerIds, $x);
if ($HOreportPages['status'] != '1'){
file_put_contents('getStats_errorLog.txt', print_r($HOreportPages, true), FILE_APPEND);
exit("There was an error pulling stats from HasOffers. Check the Error Log");
}
array_push ($stats, $HOreportPages['data']['data']);
}
echo "Stats with pages <br>";
print_r ($stats);
echo "<br> <br>";
foreach ($stats as $stat){
foreach ($stat as $item){
print_r ($item['Stat']);
echo "<br>";
}
}
//Pulls affiliate Manager's emails
$ManagerEmails = getManagerEmails ($NetworkID, $NetworkToken, $ApiDomain);
if ($ManagerEmails['status'] !=1){
file_put_contents('ManagerEmails_errorLog.txt', print_r($ManagerEmails, true), FILE_APPEND);
exit("There was an error pulling ManagerEmails from HasOffers. Check the Error Log");
}
$ManagerEmails = array ($ManagerEmails['data']);
echo "affiliate manager emails: <br>";
print_r ($ManagerEmails);
echo "<br>";
//arrays to hold and process report information
$installs = array();
$CSVheaders = array();
$CSVvalues = array();
$events = array();
//parses each row of data from HasOffers
foreach ($stats as $stat){
foreach ($stat as $row){
echo "The current row in the report which is being processed: <br>";
print_r ($row);
echo "<br>";
//adds column names to an array to be used when creating the CSV report
foreach (array_keys($row['Stat']) as $header){
array_push ($CSVheaders, $header);
}
echo "CSV Headers: <br>";
print_r ($CSVheaders);
echo "<br>";
//if the conversions are for the default goal then this code calculates the click to install CR and saves it in arrays for the CSV report
if ($row['Stat']['goal_id'] == 0){
if ($row['Stat']['conversions'] == "0" || $row['Stat']['clicks'] == "0"){
$clicktoInstallCR = "0";
}
else{
$clicktoInstallCR = $row['Stat']['conversions'] / $row['Stat']['clicks'] * 100;
}
echo "The click to install CR is: ".$clicktoInstallCR."% <br>";
//sends alerts to affiliate mangers and can block an affiliate from an offer based on the CR
$thresholds = explode(";",$row['Offer']['ref_id']);
foreach ($thresholds as $threshold){
$thresholdValues = explode('=', $threshold);
$thresholdsIndv[$thresholdValues[0]] = $thresholdValues[1];
}
echo "CR thresholds: <br>";
print_r ($thresholdsIndv);
echo "<br>";
$managerEmail = getManagerEmail ($managerId = $row['Stat']['affiliate_manager_id'], $ManagerEmails);
//Emails are sent and affiliates are blocked from offers based on the CRs
if ($clicktoInstallCR > 2 && $clicktoInstallCR < 10 && $row['Stat']['conversions'] >= 20){
$msg = "Affiliate ID ".$row['Stat']['affiliate_id']." has a CR of ".$clicktoInstallCR."% from click to install for offer ID ".$row['Stat']['offer_id'];
echo $msg;
$msg = wordwrap($msg,70);
mail($managerEmail,"Conversion Rate Alert",$msg);
}
if ($clicktoInstallCR > 10 && $row['Stat']['conversions'] > 20){
$msg = "Affiliate ID ".$row['Stat']['affiliate_id']." has a CR of ".$clicktoInstallCR."% from click to install for offer ID ".$row['Stat']['offer_id']." and has be blocked from this offer";
echo $msg;
$msg = wordwrap($msg,70);
mail($managerEmail,"Conversion Rate Alert",$msg);
$affiliateStatus = blockAffiliate ($NetworkID, $NetworkToken, $ApiDomain, $row['Stat']['offer_id'], $row['Stat']['affiliate_id']);
if ($affiliateStatus['status'] !=1){
file_put_contents('affiliateStatus_errorLog.txt', print_r($affiliateStatus, true), FILE_APPEND);
exit("There was an error updating the affiliateStatus in HasOffers. Check the Error Log");
}
print_r ($affiliateStatus);
}
//formats the click to install CR to include only 2 decimal palces and a % sign
$clicktoInstallCR = (number_format($clicktoInstallCR,2))."%";
//adds the column for the new click to install CR to the CSV header array
array_push($events,"click_to_install_CR");
array_push($CSVheaders,"click_to_install_CR");
//adds the click to install CR to the report
$row['Stat']['click_to_install_CR'] = $clicktoInstallCR;
//adds the values for this row into the new CSV report
array_push($CSVvalues,array_values($row['Stat']));
//records the number of installs to be used to calculate the install to events CR in case there are other goals in a later row of the HasOffers report.
if ($row['Stat']['conversions'] != '0'){
$install = array(
'affiliate_id' => $row['Stat']['affiliate_id'],
'offer_id' => $row['Stat']['offer_id'],
'installs' => $row['Stat']['conversions']
);
array_push($installs, $install);
}
echo "The installs are added to this array to be used to calculate the CR between installs and events: <br>";
print_r ($installs);
echo "<br>";
}
//if the conversions are for in app events and not installs then it enters this section of code
else{
//checks the $installs array to retrieve the number of installs, then calculates the install to event CR and saves it in the report
foreach ($installs as $install){
//matches the in app events with the correct installs
if ($install['offer_id'] == $row['Stat']['offer_id'] && $install['affiliate_id'] == $row['Stat']['affiliate_id']){
//then there are relevant installs. The install to event CR is calculated and the column header is added to the array which will be used when creating the CSV report.
$installtoEventCR = $clicktoInstallCR = $row['Stat']['conversions'] / $install['installs'] * 100;
$columnHeader = "install_to_".$row['Goal']['name']."_CR";
echo "The install to event CR is ".$installtoEventCR."% <br>";
//sends alerts to affiliate mangers and can block an affiliate from an offer based on the CR
$managerEmail = getManagerEmail ($managerId = $row['Stat']['affiliate_manager_id'], $ManagerEmails);
echo "The affiliate manager's email is ".$managerEmail."<br>";
if ($installtoEventCR > $thresholdsIndv[$row['Goal']['name'].'_min'] && $installtoEventCR < $thresholdsIndv[$row['Goal']['name'].'_max'] && $install['installs'] >= 200){
$msg = "Affiliate ID ".$row['Stat']['affiliate_id']." has a CR of ".$installtoEventCR."% from install to ".$row['Goal']['name']." for offer ID ".$row['Stat']['offer_id'];
echo $msg."<br>";
$msg = wordwrap($msg,70);
mail($managerEmail,"Conversion Rate Alert",$msg);
}
if ($installtoEventCR > $thresholdsIndv[$row['Goal']['name'].'_min'] && $installtoEventCR > $thresholdsIndv[$row['Goal']['name'].'_max'] && $install['installs'] >= 200){
$msg = "Affiliate ID ".$row['Stat']['affiliate_id']." has a CR of ".$installtoEventCR."% from install to ".$row['Goal']['name']." for offer ID ".$row['Stat']['offer_id']." and has be blocked from this offer";
echo $msg."<br>";
$msg = wordwrap($msg,70);
mail($managerEmail,"Conversion Rate Alert",$msg);
$affiliateStatus = blockAffiliate ($NetworkID, $NetworkToken, $ApiDomain, $row['Stat']['offer_id'], $row['Stat']['affiliate_id']);
if ($affiliateStatus['status'] !=1){
file_put_contents('affiliateStatus_errorLog.txt', print_r($affiliateStatus, true), FILE_APPEND);
exit("There was an error updating the affiliateStatus in HasOffers. Check the Error Log");
}
}
//adds an empty column in the CSV data to compensate for new column headers
foreach ($events as $event){
if ($event != $columnHeader){
$row['Stat'][$event] = ' ';
}
}
array_push($events,$columnHeader);
array_push($CSVheaders,$columnHeader);
array_push($row['Stat'],$row['Stat'][$columnHeader] = (number_format($installtoEventCR,2))."%");
$row['Stat'][$columnHeader] = (number_format($installtoEventCR,2))."%";
array_push($CSVvalues,$row['Stat']);
}
else{
//if there are no installs
$columnHeader = "install_to_".$row['Goal']['name']."_CR";
$row['Stat'][$columnHeader] = '0.00%';
echo "There are no installs in this report for this offer and affiliate <br>";
}
}
}
}
$events = array_unique($events);
echo "<br><br>";
}
//writes the updated report with CRs to a CSV file
$CSVheaders = array_unique($CSVheaders);
$fh = fopen('CR_Report.csv', 'w');
fputcsv($fh, $CSVheaders);
foreach($CSVvalues as $array){
fputcsv($fh, $array);
}
function GetOfferIds ($NetworkID, $NetworkToken, $ApiDomain, $groupId){
$base = $ApiDomain.'/Apiv3/json?';
$params = array(
'Target' => 'Offer',
'Method' => 'getOfferListByGroupId',
'NetworkId' => $NetworkID,
'NetworkToken' => $NetworkToken,
'group_id' => $groupId
);
$url = $base . http_build_query( $params );
$result = file_get_contents( $url );
$array = json_decode($result, true);
$array = $array['response'];
return $array;
}
function getStats($NetworkID, $NetworkToken, $ApiDomain, $start_date, $end_date, $offerIds, $page){
$base = $ApiDomain.'/v3/Report.json?';
$params = array(
'Method' => 'getStats',
'NetworkId' => $NetworkID,
'NetworkToken' => $NetworkToken,
'fields' => array(
'Offer.name',
'Goal.name',
'Affiliate.company',
'AffiliateManager.full_name',
'Stat.clicks',
'Stat.conversions',
'Stat.revenue',
'Stat.payout',
'Stat.profit',
'Offer.ref_id',
'Stat.offer_id',
'Stat.goal_id',
'Stat.affiliate_id',
'Stat.affiliate_manager_id'
),
'groups' => array(
'Affiliate.company',
'Goal.name',
'Offer.name',
'AffiliateManager.full_name',
'Stat.affiliate_id',
'Stat.offer_id',
'Stat.goal_id'
),
'filters' => array(
'Stat.offer_id' => array(
'conditional' => 'EQUAL_TO',
'values' => $offerIds
),
'Stat.date' => array(
'conditional' => 'BETWEEN',
'values' => array(
$start_date,
$end_date
)
)
),
'page' => $page,
'limit' => '500',
'data_start' => $start_date,
'data_end' => $end_date,
'sort' => array(
'Stat.goal_id' => 'asc',
'Stat.offer_id' => 'asc',
'Stat.affiliate_id' => 'asc'
)
);
$url = $base . http_build_query( $params );
$result = file_get_contents( $url );
$array = json_decode($result, true);
$array = $array['response'];
return $array;
}
function getManagerEmails($NetworkID, $NetworkToken, $ApiDomain){
$base = $ApiDomain.'/v3/Employee.json?';
$params = array(
'Method' => 'findAll',
'NetworkId' => $NetworkID,
'NetworkToken' => $NetworkToken,
'filters' => array(
'status' => 'active'
),
'fields' => array(
'id',
'email'
)
);
$url = $base . http_build_query( $params );
$result = file_get_contents( $url );
$array = json_decode($result, true);
$array = $array['response'];
return $array;
}
function getManagerEmail($managerId, $ManagerEmails){
$managerEmail = "";
foreach ($ManagerEmails as $Manager){
foreach ($Manager as $ManagerDetails){
//if the affiliate has a manager...
if ($managerId == $ManagerDetails['Employee']['id']){
$managerEmail = $ManagerDetails['Employee']['email'];
return $managerEmail;
}
}
}
}
function blockAffiliate($NetworkID, $NetworkToken, $ApiDomain, $offerID, $affiliateID){
$base = $ApiDomain.'/v3/Offer.json?';
$params = array(
'Method' => 'setAffiliateApproval',
'NetworkId' => $NetworkID,
'NetworkToken' => $NetworkToken,
'id' => $offerID,
'affiliate_id' => $affiliateID,
'status' => 'rejected'
);
$url = $base . http_build_query( $params );
$result = file_get_contents( $url );
$array = json_decode($result, true);
$array = $array['response'];
return $array;
}
?>