// Copyright (c) 2007 Legendum LLC
// This code may be used for free,
// provided this notice is included.
function Traffic(api, element)
{
this.api = api;
this.widget = new Widget(element);
this.traffic = null;
this.secs = 0;
this.site = null;
this.channel = null;
this.date_requested = null;
// Get a traffic report
this.display = function(args)
{
this.site = args.site;
this.channel = args.channel;
this.date_requested = (args.date ? args.date : 0);
this.widget.setName('Traffic');
var query = '?token=' + this.api.getToken() + '&widget=traffic&site_id=' + args.site + '&channel_id=' + args.channel + '&date=' + args.date;
if (!args.no_link) this.widget.setLink('mystats/widget.php' + query);
if (!args.no_open) this.widget.setOpen('mystats/window.php' + query);
this.widget.loading();
args.report = 'traffic';
args.callback = this.onChannelReports;
this.api.getChannelReports(this, args, 300); // every 5 minutes
}
// Return a fraction to 1 decimal place
this.fraction = function(num, div)
{
return div ? Math.round(10 * num / div) / 10 : 0.0;
}
// Display the traffic report
this.onChannelReports = function(channel)
{
var report = channel[0].report[0];
// Get the traffic report data
this.traffic = {users : 0, visits : 0, hits : 0, first_times : 0};
var data = report.data; // a list of one
var rows = data.length;
for (var row = 0; row < rows; row++)
{
this.traffic[data[row].field] = data[row].value;
}
// Display the traffic report
var date = report.start_date;
this.widget.setDate(date);
var html = '';
html += this.traffic.users + " users
\n";
if (this.traffic.users)
{
html += this.traffic.visits + ' visits (' + this.fraction(this.traffic.visits, this.traffic.users) + " per user)
\n";
html += this.traffic.hits + ' page views (' + this.fraction(this.traffic.hits, this.traffic.visits) + " per visit)
\n";
html += "