statistics.module 5.66 KB
Newer Older
Dries's avatar
 
Dries committed
1
<?php
2
// $Id$
Dries's avatar
 
Dries committed
3

Dries's avatar
   
Dries committed
4
if (variable_get("referrer", 0) && $referrer = getenv("HTTP_REFERER")) {
Dries's avatar
   
Dries committed
5
  db_query("INSERT INTO referrer (URL, timestamp) values ('%s', '%s')", $referrer, time());
Dries's avatar
   
Dries committed
6
7
8
9
10
11
}

function statistics_help() {
  $output .= "<p>The statistics module gathers and presents useful log information from your Drupal site. Currently, the statistics module is limited to internal and external referrals display, but other analysis capabilities might be added in future.</p>";
  $output .= "<p>1. The external referrer log indicates which other sites are linking your website and how many visitors they refer. Each link made to your site - when a user on another site clicks on a link to your site - generates a referral entry in the log.</p>";
  $output .= "<p>2. The internal referrer log indicates the referrals within the domain of your site.  This log is useful for assessing and evaluating the structure of your website, to learn which pages are being accessed, and to gain insight into the way users are navigating your site.</p>";
Dries's avatar
   
Dries committed
12
  $output .= "<p>Drupal automatically rotates the referrer logs after a set period of time. The life-time of the accumulated data can be configured via the <i>site configuration</i> option under site administration.</p>";
Dries's avatar
   
Dries committed
13
  $output .= "<p>Warning: Drupal gets the referrer information from the HTTP_REFERER environment variable. This is not always set properly by web browsers.</p>";
Dries's avatar
   
Dries committed
14
  return $output;
Dries's avatar
 
Dries committed
15
16
17
}

function statistics_cron() {
Dries's avatar
   
Dries committed
18
  db_query("DELETE FROM referrer WHERE ". time() ." - timestamp > ". variable_get("referrer_clear", 604800));
Dries's avatar
 
Dries committed
19
20
21
}

function statistics_perm() {
Dries's avatar
   
Dries committed
22
  return array("administer statistics");
Dries's avatar
 
Dries committed
23
24
}

Dries's avatar
   
Dries committed
25
function statistics_link($type) {
Dries's avatar
   
Dries committed
26
  if ($type == "admin" && user_access("administer statistics")) {
Dries's avatar
   
Dries committed
27
    $links[] = la(t("statistics"), array("mod" => "statistics"));
Dries's avatar
   
Dries committed
28
29
30
31
32
  }

  return $links ? $links : array();
}

Dries's avatar
 
Dries committed
33
function statistics_conf_options() {
Dries's avatar
   
Dries committed
34
  $period = array(3600 => format_interval(3600), 10800 => format_interval(10800), 21600 => format_interval(21600), 32400 => format_interval(32400), 43200 => format_interval(43200), 86400 => format_interval(86400), 172800 => format_interval(172800), 259200 => format_interval(259200), 604800 => format_interval(604800), 1209600 => format_interval(1209600), 2419200 => format_interval(2419200), 4838400 => format_interval(4838400), 9676800 => format_interval(9676800));
Dries's avatar
   
Dries committed
35
36
  $output .= form_select("Track referrers", "referrer", variable_get("referrer", 0), array("Disabled", "Enabled"), "If enabled, Drupal will count how many times your website is referred to by other websites.");
  $output .= form_select("Discard referrers older than", "referrer_clear", variable_get("referrer_clear", 604800), $period, "The time referrer entries should be kept.  Older entries will be automatically discarded.  Requires crontab.");
Dries's avatar
   
Dries committed
37
  return $output;
Dries's avatar
 
Dries committed
38
39
}

Dries's avatar
   
Dries committed
40
41
function statistics_table_1($query) {
  $result = db_query($query);
Dries's avatar
   
Dries committed
42

Dries's avatar
   
Dries committed
43
44
  $output .= "<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\">\n";
  $output .= " <tr><th>URL</th><th>date</th></tr>\n";
Dries's avatar
   
Dries committed
45
46
  while ($referrer = db_fetch_object($result)) {
    $output .= "<tr><td><a href=\"". check_output($referrer->url) ."\">". substr(check_output($referrer->url), 0, 100) ."</a></td><td>". format_date($referrer->timestamp, "small") ."</td></tr>";
Dries's avatar
   
Dries committed
47
  }
Dries's avatar
   
Dries committed
48
  $output .= "</table>\n";
Dries's avatar
   
Dries committed
49

Dries's avatar
   
Dries committed
50
51
52
  return $output;
}

Dries's avatar
   
Dries committed
53
54
function statistics_table_2($query) {
  $result = db_query($query);
Dries's avatar
   
Dries committed
55

Dries's avatar
   
Dries committed
56
57
  $output .= "<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\">\n";
  $output .= " <tr><th>URL</th><th>number</th></tr>\n";
Dries's avatar
   
Dries committed
58
59
  while ($referrer = db_fetch_object($result)) {
    $output .= "<tr><td><a href=\"". check_output($referrer->url) ."\">". substr(check_output($referrer->url), 0, 100) ."</a></td><td>". check_output($referrer->count) ."</td></tr>";
Dries's avatar
 
Dries committed
60
  }
Dries's avatar
   
Dries committed
61
  $output .= "</table>\n";
Dries's avatar
 
Dries committed
62
63
64
65

  return $output;
}

Dries's avatar
   
Dries committed
66
function statistics_referrer_internal() {
Dries's avatar
   
Dries committed
67
68
  global $HTTP_HOST;

Dries's avatar
   
Dries committed
69
70
  $output .= "<h3>Most recent internal referrers</h3>\n";
  $output .= statistics_table_1("SELECT url, timestamp FROM referrer WHERE url LIKE '%". check_input($HTTP_HOST) ."%' ORDER BY timestamp DESC LIMIT 15");
Dries's avatar
   
Dries committed
71

Dries's avatar
   
Dries committed
72
73
  $output .= "<h3>Internal referrers of the last ". format_interval(variable_get("referrer_clear", 604800)) ."</h3>\n";
  $output .= statistics_table_2("SELECT url, COUNT(url) AS count FROM referrer WHERE url LIKE '%". check_input($HTTP_HOST) ."%' GROUP BY url ORDER BY count DESC, timestamp");
Dries's avatar
   
Dries committed
74
75
76
77

  return $output;
}

Dries's avatar
   
Dries committed
78
function statistics_referrer_external() {
Dries's avatar
   
Dries committed
79
  global $HTTP_HOST;
Dries's avatar
   
Dries committed
80

Dries's avatar
   
Dries committed
81
82
  $output .= "<h3>Most recent external referrers</h3>\n";
  $output .= statistics_table_1("SELECT url, timestamp FROM referrer WHERE url NOT LIKE '%". check_input($HTTP_HOST) ."%' ORDER BY timestamp DESC LIMIT 15");
Dries's avatar
   
Dries committed
83

Dries's avatar
   
Dries committed
84
85
  $output .= "<h3>External referrers of the last ". format_interval(variable_get("referrer_clear", 604800)) ."</h3>\n";
  $output .= statistics_table_2("SELECT url, COUNT(url) AS count FROM referrer WHERE url NOT LIKE '%". check_input($HTTP_HOST) ."%' GROUP BY url ORDER BY count DESC, timestamp");
Dries's avatar
   
Dries committed
86
87
88
89

  return $output;
}

Dries's avatar
 
Dries committed
90
function statistics_admin() {
Dries's avatar
   
Dries committed
91
  global $op, $type;
Dries's avatar
   
Dries committed
92
93
94

  if (user_access("administer statistics")) {

Kjartan's avatar
Kjartan committed
95
    print "<small>". la(t("internal referrers"), array("mod" => "statistics", "type" => "internal referrer")) ." | ". la(t("external referrers"), array("mod" => "statistics", "type" => "external referrer")) ." | ". la(t("help"), array("mod" => "statistics", "op" => "help")) ."</small><hr />\n";
Dries's avatar
 
Dries committed
96

Dries's avatar
   
Dries committed
97
98
99
    switch ($op) {
      case "help":
        print statistics_help();
Dries's avatar
   
Dries committed
100
101
        break;
      default:
Dries's avatar
   
Dries committed
102
103
104
105
106
107
108
109
110
        switch ($type) {
          case "internal referrer":
            print statistics_referrer_internal();
            break;
          case "external referrer":
            // fall through:
          default:
            print statistics_referrer_external();
        }
Dries's avatar
   
Dries committed
111
    }
Dries's avatar
 
Dries committed
112
113
  }
}
Dries's avatar
   
Dries committed
114

Dries's avatar
 
Dries committed
115
?>