forked from mshmsh5000/pixel_pusher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pixel_pusher.module
166 lines (141 loc) · 4.83 KB
/
pixel_pusher.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* Pixel Pusher
*
* Integrates with Pixel Ping, a pixel-tracking service from Pro Publica
* and DocumentCloud that runs on Node.js.
*
* http://documentcloud.github.com/pixel-ping/
* http://www.propublica.org/nerds/item/pixel-ping-a-nodejs-stats-tracker
*
* Requires json_decode(), and thus either PHP 5.2 or the json PECL
* extension.
*/
/**
* Implementation of hook_menu().
*/
function pixel_pusher_menu() {
$items = array();
$items['pixel_pusher/save_hits'] = array(
'title' => t('Pixel Ping flush callback'),
'page callback' => 'pixel_pusher_save_hits',
'type' => MENU_CALLBACK,
'access arguments' => array('access content'),
);
$items['admin/settings/pixel_pusher'] = array(
'title' => t('Pixel Pusher settings'),
'page callback' => 'drupal_get_form',
'page arguments' => array('pixel_pusher_admin_settings'),
'type' => MENU_NORMAL_ITEM,
'access arguments' => array('administer pixel_pusher'),
);
return $items;
}
/**
* Implementation of hook_perm().
*/
function pixel_pusher_perm() {
return array('administer pixel_pusher');
}
/**
* Admin settings form.
*/
function pixel_pusher_admin_settings() {
$form['pixel_ping'] = array(
'#type' => 'fieldset',
'#title' => t('Pixel Ping integration'),
'#collapsible' => TRUE,
);
$form['pixel_ping']['pixel_pusher_remote'] = array(
'#type' => 'textfield',
'#title' => t('Pixel Ping server'),
'#default_value' => variable_get('pixel_pusher_remote', 'http://127.0.0.1'),
'#description' => 'The base URL where the pixel will be served. Do not include <em>/pixel.gif</em> or a trailing slash; for instance, enter <em>http://something.com:9000</em>.',
);
$form['pixel_pusher_debug'] = array(
'#type' => 'checkbox',
'#title' => t('Enable verbose logging'),
'#default_value' => variable_get('pixel_pusher_debug', 0),
'#description' => t('<strong>Not recommended for production environments:</strong> Activate this option if you want many debug messages sent to Watchdog.'),
);
return system_settings_form($form);
}
/**
* Implementation of hook_block().
*/
function pixel_pusher_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[0] = array(
'info' => t('Pixel Counter tracker pixel'),
'weight' => 0,
'cache' => BLOCK_CACHE_PER_PAGE,
);
return $blocks;
case 'view':
$block = array();
switch ($delta) {
case 0:
if (!empty($_GET['q'])) {
$nid = str_replace('node/', '', $_GET['q']);
$node = new stdClass();
$node->nid = $nid;
}
else {
$node = menu_get_object('node');
}
if (!empty($node) && is_null(arg(2))) {
$block['subject'] = FALSE;
$server_url = variable_get('pixel_pusher_remote', 'http://127.0.0.1');
$block['content'] = '<img src="' . $server_url . '/pixel.gif?key=node,' . $node->nid . '" width="1" height="1" alt="" />';
}
return $block;
}
}
}
/**
* Callback for Pixel Ping's flush request. A 'json'
* object should be posted with keys and hit counts.
*/
function pixel_pusher_save_hits() {
if (!empty($_REQUEST['json'])) {
$results = json_decode($_REQUEST['json'], TRUE);
if ($results) {
pixel_pusher_write_hits($results);
if (variable_get('pixel_pusher_debug', 0)) {
watchdog('Pixel Ping', '@var' , array('@var' => print_r($results, TRUE)), WATCHDOG_DEBUG);
}
}
}
else {
watchdog('Pixel Ping', 'No "json" object received. Raw data: @var', array('@var' => print_r($_REQUEST, TRUE)), WATCHDOG_WARNING);
}
}
/**
* Given a parsed $results array from pixel_pusher_save_hits, increment
* the appropriate nodes.
*
* Almost all of this code is copied, with gratitude, from the excellent
* boost_stats.php script that ships with the Boost module.
*/
function pixel_pusher_write_hits($results) {
if (!empty($results)) {
foreach ($results as $key => $val) {
$path = split(',', $key);
if ('node' == $path[0] && !empty($path[1])) {
// Mimic the "node view" event.
db_query('UPDATE {node_counter} SET daycount = daycount + %d, totalcount = totalcount + %d, timestamp = %d WHERE nid = %d',
$val, $val, time(), $path[1]);
// 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 node.
db_query('INSERT INTO {node_counter} (nid, daycount, totalcount, `timestamp`) VALUES (%d, %d, %d, %d)',
$path[1], $val, $val, time());
}
if (variable_get('pixel_pusher_debug', 0)) {
watchdog('Pixel Pusher', 'Updated node nid=@node by @amount', array('@node' => $path[1], '@amount' => $val));
}
}
}
}
}