Adding Stats to Affiliate Pages
Quick_stats.php is a function in NATS3 that allows you to add affiliate stats to a page in your NATS system. This function is included with your system, however you must set up your own Smarty plugin in order to call the function. Quick_stats.php can display affiliate stats in e-mails sent to affiliates, as well as other NATS pages.
Setting up the script
In your ../nats/includes folder create a php file. For this example, we can refer to it as mystats.php. In this file, you can use the following code, or alter it to meet your needs:
<? include_once('quick_stats.php'); $data = quick_stats('normal', $GLOBALS['loginid'], 1); $top_data = top_stats('2', '2', '10'); $this->assign('top_data', $top_data); $this->assign('data', $data); ?>
There are two functions in the quick_stats.php script, quick_stats and top_stats.
quick_stats
Quick_stats contains six parameters that designate which stats are displayed, as well as when you want these stats from. These parameters are:
- $type - Sets the type of stats you wish to display. This can be set to owed (payments that have been built but have not yet been paid), site (reseller stats broken down by site), and normal (reseller stats broken down by day).
- $loginid - Use an affiliate login ID to grab stats for that particular affiliate.
- $range_type - Sets what time period stats are displayed for (this period, this month, etc. This is explained in more detail below).
- $range_start - Sets the start date for your selected date range. This can be expressed in year-month-day (yyyy-mm-dd) form.
- $range_end - Sets the end date for your selected date range. This can be expressed in year-month-day (yyyy-mm-dd) form.
- $return_total - Displays affiliate statistic totals, such as total signups.
top_stats
Top_stats contains seven parameters that must be set as well in order to display top affiliate stats. These parameters include:
- $type - Sets the type of stats you wish to display. This can be set to 1 (top sites by joins), 2 (top sites by ratio), 3 (top affiliates by ratio), 4 (worst affiliates by ratio), or default (top affiliates by amount earned).
- $range_type - Sets what time period stats are displayed for (this period, this month, etc. This is explained in more detail below).
- $num - Sets the number of results to return.
- $range_start - Sets the start date for your selected date range. This can be expressed in year-month-day (yyyy-mm-dd) form.
- $range_end - Sets the end date for your selected date range. This can be expressed in year-month-day (yyyy-mm-dd) form.
- $limit_sites - Allows you to set an array of site stats you want to display, or set a comma-separated list of site stats to display.
- $ratio - Sets how you want an affiliate's join ratio to be displayed. Must be set as unq, raw, or qual.
Note: The top_stats function should be run as a cron that only updates once an hour. We strongly recommend that you store the results in a flat file and display them from the file.
Your options for time period ($range_type parameter) are:
- 0 = today
- 1 = this period
- 2 = this month
- 3 = previous day
- 4 = previous period
- 5 = previous month
- 6 = freeform; to use this you will have to include a 4th and 5th parameter as start and end dates. These values can be dates or statements like "friday 3 weeks ago". For more information, please see our Freeform Date article.
Including mystats.php on a template
If you would like to include your example script (mystats.php) on a template, locate the template you would like your affiliate statistics to appear on in your "../nats/templates" directory. In the template you wish to edit, you can include mystats.php like the following example:
{include_php file="mystats.php"}
You can use the following variables to get your stats:
- {$data.raw|default:0} gives you raw hits
- {$data.unq|default:0} gives you unique hits
- {$data.qual|default:0} gives you qualified hits
- {$data.signups|default:0} gives you joins
- {$data.a_signups|default:0} gives you active joins
- {$data.earned|default:0} gives you the amount earned on the sale
- {$data.ratio} gives you a ratio of joins to raw hits
Example top_stats_cron:
<? $do_db = 1; // we want to connect to the db $do_login = 0; // we want to try to login if login info is provided include_once('../includes/header.php'); include_once('../includes/quick_stats.php'); $top_data = top_stats(2, 1, 5); $filename = 'top_data.txt'; $data = ''; foreach($top_data as $order => $info){ $data_info = Array(); foreach($info as $field => $value){ $data_info[] = "'$field'='$value'"; } $data .= implode(', ', $data_info); $data .= "n"; } if (is_writable($filename)){ if (!$handle = fopen($filename, 'w')) { exit; } if (fwrite($handle, $data) === FALSE) { exit; } fclose($handle); } ?>
Example Smarty plugin to read top_data.txt file:
<? function smarty_function_get_top_stats($params, &$smarty){ $top_data = Array(); if(!$params['file']) return FALSE; $filename = $params['file']; if(!filesize($filename)) return FALSE; $handle = fopen($filename, 'r'); $contents = fread($handle, filesize($filename)); fclose($handle); $data = explode("\n", $contents); foreach($data as $values){ $values = substr($values, 1); $values = substr($values, 0, -1); $vdata = explode("', '", $values); $info = Array(); foreach($vdata as $vinfo){ list($field, $val) = explode("'='", $vinfo); $info[$field] = $val; } $top_data[] = $info; } $smarty->assign('top_data', $top_data); return; } ?>
Call to plugin on the template:
{get_top_stats file="/pathtonats/tools/top_data.txt"}