// Copyright (c) 2007 Legendum LLC
// This code may be used for free,
// provided this notice is included.
function Search(api, element)
{
this.api = api;
this.widget = new Widget(element);
this.search = {};
this.secs = 0;
this.site = null;
this.channel = null;
this.date_requested = null;
// Get a search engine report
this.display = function(args)
{
this.site = args.site;
this.channel = args.channel;
this.date_requested = args.date;
this.widget.setName('Search');
var query = '?token=' + this.api.getToken() + '&widget=search&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 = 'referrer_search';
args.callback = this.onChannelReports;
this.api.getChannelReports(this, args, 300); // every 5 minutes
}
// Display the search engine report
this.onChannelReports = function(channel)
{
var report = channel[0].report[0];
// Get the traffic report data
this.search = {total : 0, google : 0, yahoo : 0, msn : 0, other : 0};
var data = report.data; // a list of one
var rows = data.length;
for (var row = 0; row < rows; row++)
{
this.countSearch(this.search, data[row]);
}
// Display the search engine report
var date = report.start_date;
this.widget.setDate(date);
var html = '';
html += this.search.total + " searches
\n";
html += "
\n";
html += this.search.google + " Google searches
\n";
html += this.search.yahoo + " Yahoo searches
\n";
html += this.search.msn + " MSN searches
\n";
html += this.search.other + " other searches
\n";
this.widget.display(html);
// Get last week's searches to compare
if (this.date_requested < 999) // relative date
{
this.secs = report.secs;
var args = {site : this.site,
channel : this.channel,
report : 'referrer_search,hour_of_day_visits',
date : this.date_requested + 7,
callback : this.onLastWeek};
this.api.getChannelReports(this, args);
}
}
// Compare with last week's searches
this.onLastWeek = function(channel)
{
var search_report = channel[0].report[0];
var hourly_report = channel[0].report[1];
// Get last week's report data
var last_week = {total : 0, google : 0, yahoo : 0, msn : 0, other : 0};
var data = search_report.data;
var rows = data.length;
for (var row = 0; row < rows; row++)
{
this.countSearch(last_week, data[row]);
}
// Calculate percentage changes
var forecast = this.widget.getForecast(hourly_report, this.secs);
var total_arrow = this.widget.getArrow(this.search.total, last_week.total, forecast);
var google_arrow = this.widget.getArrow(this.search.google, last_week.google, forecast);
var yahoo_arrow = this.widget.getArrow(this.search.yahoo, last_week.yahoo, forecast);
var msn_arrow = this.widget.getArrow(this.search.msn, last_week.msn, forecast);
var other_arrow = this.widget.getArrow(this.search.other, last_week.other, forecast);
var html = '';
html += total_arrow + ' ' + this.search.total + " searches
\n";
html += "
\n";
html += google_arrow + ' ' + this.search.google + " Google searches
\n";
html += yahoo_arrow + ' ' + this.search.yahoo + " Yahoo searches
\n";
html += msn_arrow + ' ' + this.search.msn + " MSN searches
\n";
html += other_arrow + ' ' + this.search.other + " other searches
\n";
html += 'Comparing same day last week
';
this.widget.display(html);
}
this.countSearch = function(search, row)
{
var value = Math.floor(row.value);
search['total'] += value;
if (row.field.indexOf('google.') > -1)
search['google'] += value;
else if (row.field.indexOf('yahoo.') > -1)
search['yahoo'] += value;
else if (row.field.indexOf('msn.') > -1)
search['msn'] += value;
else if (row.field.indexOf('live.') > -1)
search['msn'] += value;
else
search['other'] += value;
}
};