// 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 += "
\n"; html += this.traffic.first_times + " new users
\n"; html += this.traffic.users - this.traffic.first_times + " returning users
\n"; if (this.traffic.campaign_visits) html += this.traffic.campaign_visits + " campaign clicks
\n"; } this.widget.display(html); // Get last week's traffic to compare if (this.date_requested < 999) // relative date { this.secs = report.secs; var args = {site : this.site, channel : this.channel, report : 'traffic,hour_of_day_hits', date : this.date_requested + 7, callback : this.onLastWeek}; this.api.getChannelReports(this, args); } } // Compare with last week's traffic this.onLastWeek = function(channel) { var traffic_report = channel[0].report[0]; var hourly_report = channel[0].report[1]; // Get last week's report data var last_week = {users : 0, visits : 0, hits : 0, first_times : 0}; var data = traffic_report.data; var rows = data.length; for (var row = 0; row < rows; row++) { last_week[data[row].field] = data[row].value; } // Calculate returning users this.traffic.returning = this.traffic.users - this.traffic.first_times; last_week.returning = last_week.users - last_week.first_times; // Calculate percentage changes var forecast = this.widget.getForecast(hourly_report, this.secs); var first_times_arrow = this.widget.getArrow(this.traffic.first_times, last_week.first_times, forecast); var returning_arrow = this.widget.getArrow(this.traffic.returning, last_week.returning, forecast); var user_arrow = this.widget.getArrow(this.traffic.users, last_week.users, forecast); var campaign_arrow = this.widget.getArrow(this.traffic.campaign_visits, last_week.campaign_visits, forecast); var visit_arrow = this.widget.getArrow(this.traffic.visits, last_week.visits, forecast); var hit_arrow = this.widget.getArrow(this.traffic.hits, last_week.hits, forecast); var html = ''; html += user_arrow + ' ' + this.traffic.users + " users
\n"; html += visit_arrow + ' ' + this.traffic.visits + ' visits (' + this.fraction(this.traffic.visits, this.traffic.users) + " per user)
\n"; html += hit_arrow + ' ' + this.traffic.hits + ' page views (' + this.fraction(this.traffic.hits, this.traffic.visits) + " per visit)
\n"; html += "
\n"; html += first_times_arrow + ' ' + this.traffic.first_times + " new users
\n"; html += returning_arrow + ' ' + this.traffic.returning + " returning users
\n"; if (this.traffic.campaign_visits) html += campaign_arrow + ' ' + this.traffic.campaign_visits + " campaign clicks
\n"; html += '
Comparing same day last week
'; this.widget.display(html); } };