Skip to content

Commit 95435f3

Browse files
added counting sessions
1 parent 63ff0e8 commit 95435f3

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/Bolt.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ private function tempDirectoryInit(): void
5353

5454
private function track(): void
5555
{
56-
foreach (glob($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'queries.*.cnt') as $file) {
56+
foreach (glob($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'analytics.*.json') as $file) {
5757
$time = intval(explode('.', basename($file))[1]);
5858
if ($time < strtotime('today')) {
59-
$count = file_get_contents($file);
59+
$data = json_decode(file_get_contents($file), true);
60+
$distinctId = sha1(implode('', [php_uname(), disk_total_space('.'), filectime('/'), phpversion()]));
6061

6162
$curl = curl_init();
6263
curl_setopt_array($curl, [
@@ -72,11 +73,20 @@ private function track(): void
7273
[
7374
'properties' => [
7475
'$insert_id' => (string)$time,
75-
'distinct_id' => sha1(implode('', [php_uname(), disk_total_space('.'), filectime('/'), phpversion()])),
76-
'amount' => $count,
76+
'distinct_id' => $distinctId,
77+
'amount' => $data['queries'] ?? 0,
7778
'time' => $time
7879
],
7980
'event' => 'queries'
81+
],
82+
[
83+
'properties' => [
84+
'$insert_id' => (string)$time,
85+
'distinct_id' => $distinctId,
86+
'amount' => $data['sessions'] ?? 0,
87+
'time' => $time
88+
],
89+
'event' => 'sessions'
8090
]
8191
]),
8292
CURLOPT_HTTPHEADER => [

src/protocol/AProtocol.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,11 @@ public function getResponse(): Response
169169
public function __destruct()
170170
{
171171
if (!getenv('BOLT_ANALYTICS_OPTOUT') && is_writable($_ENV['TEMP_DIR']. DIRECTORY_SEPARATOR)) {
172-
$file = $_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'queries.' . strtotime('today') . '.cnt';
173-
$count = file_exists($file) ? intval(file_get_contents($file)) : 0;
174-
file_put_contents($file, $count + $this->writeCalls);
172+
$file = $_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'analytics.' . strtotime('today') . '.json';
173+
$data = file_exists($file) ? (array)json_decode((string)file_get_contents($file), true) : [];
174+
$data['queries'] = ($data['queries'] ?? 0) + $this->writeCalls;
175+
$data['sessions'] = ($data['sessions'] ?? 0) + 1;
176+
file_put_contents($file, json_encode($data));
175177
}
176178
}
177179
}

0 commit comments

Comments
 (0)