-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathanalytics.php
163 lines (127 loc) · 4.66 KB
/
analytics.php
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
<?php
/*
Here is an example script written in PHP for handling the output from SimpleAnalytics on a
web service that hosts MySQL databases. Please consider this as a starting point, or an
inspiration for your own solution.
CAVEAT EMPTOR: This script was created by a relatively new PHP programmer, using documentation
and solutions found online. It may have serious unknown flaws as it has only been lightly
tested. But it has been found to work in at least light usage. As always, your mileage may vary.
===============
This script assumes a MySQL database with tables for "items" and "counters", where...
...the 'items' table's columns are:
description: VarChar
details: VarChar
device_id: VarChar
app_name: VarChar
app_version: VarChar
platform: VarChar
system_version: VarChar
timestamp: DateTime
id: Auto incrementing primary key
...the 'counters' table's columns are:
description: VarChar
count: Integer
device_id: VarChar
app_name: VarChar
app_version: VarChar
platform: VarChar
system_version: VarChar
timestamp: DateTime
id: Auto incrementing primary key
****************
Be sure to configure the following four properties in order to be able to connect to your database.
****************
*/
$database_servername = "";
$database_name = "";
$username = "";
$password = "";
$dbErrorCode = 503;
$body = @file_get_contents('php://input');
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
echo(generateErrorResponse($conn->connect_error, $dbErrorCode));
return;
}
parse_body($body, $conn);
function parse_body($body, $conn) {
$body_string = (string)$body;
$decoded = json_decode($body_string);
$items = $decoded->items;
$counters = $decoded->counters;
$device_id = (string)$decoded->device_id;
$application = (string)$decoded->app_name;
$app_version = (string)$decoded->app_version;
$platform = (string)$decoded->platform;
$system_version = (string)$decoded->system_version;
$currentCount = 0;
// add 'items' array contents to database
if (count($items) > 0 ) {
$description = "";
$details = "";
$timestamp = "";
// prepare and bind items insert statement
$prepared = $stmt = $conn->prepare("INSERT INTO items (description, details, device_id, app_name, app_version, platform, system_version, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?);");
if ($prepared === false) {
echo(generateErrorResponse("Error preparing 'items' insert statement", $dbErrorCode));
return;
}
$stmt->bind_param("ssssssss", $description, $details, $device_id, $application, $app_version, $platform, $system_version, $timestamp);
foreach ($items as $item) {
// set parameters and execute
$description = (string)$item->description;
$title = (string)$item->description;
$details = stdObject_array_toString($item->parameters);
$timestamp = (string)$item->timestamp;
if ($stmt->execute() === true) {
$currentCount += 1;
}
}
}
// add 'counters' array contents to database
if (count($counters) > 0) {
$name = "";
$itemCount = 0;
$timestamp = "";
// prepare and bind counters insert statement
$prepared = $stmt = $conn->prepare("INSERT INTO counters (description, count, device_id, app_name, app_version, platform, system_version, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?);");
if ($prepared === false) {
echo(generateErrorResponse("Error preparing 'counters' insert statement", $dbErrorCode));
return;
}
$stmt->bind_param("sdssssss", $name, $itemCount, $device_id, $application, $app_version, $platform, $system_version, $timestamp);
foreach ($counters as $item) {
// set parameters and execute
$name = (string)$item->name;
$itemCount = (int)$item->count;
$timestamp = (string)$item->timestamp;
if ($stmt->execute() === true) {
$currentCount += 1;
}
}
}
$conn->close();
$success['message'] = 'Successfully added ' . $currentCount . ' items to the Analytics database.';
$encoded = json_encode($success);
echo((string)$encoded);
}
function stdObject_array_toString($array) {
// convert the 'parameters' dictionary contents to a string for insertion in the database
// https://stackoverflow.com/questions/2537767/how-to-convert-a-php-object-to-a-string
if ($array === null) { return ""; }
$r = new ReflectionObject($array);
return '{' . implode(', ', array_map(
function($p) use ($array) {
$p->setAccessible(true);
return $p->getName() .': '. $p->getValue($array);
}, $r->getProperties())) .'}';
}
function generateErrorResponse($message, $code) {
header ($message, true, $code);
$response = array('message' => $message);
$encoded = json_encode($response);
return (string)$encoded;
}
?>