-
Notifications
You must be signed in to change notification settings - Fork 1
/
views_analytics.module
94 lines (82 loc) · 2.63 KB
/
views_analytics.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/**
* Hook views_pre_render to add to count.
*/
function views_analytics_views_pre_render(&$view) {
if (!$view->preview) {
db_query('UPDATE {views_analytics_views}
SET count = count + 1,
timestamp = %d
WHERE vid = %d',
time(), $view->vid);
// If we affected 0 rows, this is the first time viewing the node.
if (!db_affected_rows()) {
// We must create a new row to store counters for the new View.
db_query('INSERT INTO {views_analytics_views} (vid, count, timestamp) VALUES (%d, 1, %d)', $view->vid, time());
}
// the following is stolen from the statistic module
db_query('UPDATE {views_analytics_displays}
SET count = count + 1,
timestamp = %d
WHERE vid = %d
AND id = "%s"',
time(), $view->vid, $view->current_display);
// If we affected 0 rows, this is the first time viewing the node.
if (!db_affected_rows()) {
// We must create a new row to store counters for the new View.
db_query('INSERT INTO {views_analytics_displays} (vid, id, count, timestamp) VALUES (%d, "%s", 1, %d)',
$view->vid, $view->current_display, time());
}
}
}
function hook_views_pre_view(&$view) {
print 'banana';
}
/**
* Gather the analytics for a View.
*/
function views_analytics_get_view_analytics($view) {
if ($view->type != 'Default') {
$view = views_get_view($view->name);
$stats = db_query('SELECT * FROM {views_analytics_views} WHERE vid = %d', $view->vid);
$analytics = db_fetch_object($stats);
if (!$analytics) {
$analytics->vid = $view->vid;
$analytics->count = '0';
$analytics->timestamp = NULL;
}
return $analytics;
}
else {
return FALSE;
}
}
/**
* Output analytics as a printable string.
*/
function views_analytics_theme_view_analytics($view) {
if ($view->type != 'Default') {
$analytics = views_analytics_get_view_analytics($view);
$view_analytics = '<div class="view-analytics">This View has been called <strong>' . $analytics->count . '</strong> times.';
if ($analytics->count > 0) {
$view_analytics .= ' It was last called called on <strong>'. format_date($analytics->timestamp, 'custom', 'F jS, Y \a\t g:ia') .'</strong>.';
}
$view_analytics .= '</div>';
return $view_analytics;
}
else {
return FALSE;
}
}
/**
* To Do: Implement cleanup of tables. The following is from the statistics module.
*/
/*
function statistics_nodeapi(&$node, $op, $arg = 0) {
switch ($op) {
case 'delete':
// clean up statistics table when node is deleted
db_query('DELETE FROM {node_counter} WHERE nid = %d', $node->nid);
}
}
*/