Skip to content

Commit

Permalink
Merge pull request #144 from neo4j-php/analytics-add-sessions
Browse files Browse the repository at this point in the history
added counting sessions
  • Loading branch information
stefanak-michal authored Aug 2, 2024
2 parents 63ff0e8 + 584d2a2 commit d2e25fa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
18 changes: 14 additions & 4 deletions src/Bolt.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ private function tempDirectoryInit(): void

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

$curl = curl_init();
curl_setopt_array($curl, [
Expand All @@ -72,11 +73,20 @@ private function track(): void
[
'properties' => [
'$insert_id' => (string)$time,
'distinct_id' => sha1(implode('', [php_uname(), disk_total_space('.'), filectime('/'), phpversion()])),
'amount' => $count,
'distinct_id' => $distinctId,
'amount' => $data['queries'] ?? 0,
'time' => $time
],
'event' => 'queries'
],
[
'properties' => [
'$insert_id' => (string)$time,
'distinct_id' => $distinctId,
'amount' => $data['sessions'] ?? 0,
'time' => $time
],
'event' => 'sessions'
]
]),
CURLOPT_HTTPHEADER => [
Expand Down
8 changes: 5 additions & 3 deletions src/protocol/AProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,11 @@ public function getResponse(): Response
public function __destruct()
{
if (!getenv('BOLT_ANALYTICS_OPTOUT') && is_writable($_ENV['TEMP_DIR']. DIRECTORY_SEPARATOR)) {
$file = $_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'queries.' . strtotime('today') . '.cnt';
$count = file_exists($file) ? intval(file_get_contents($file)) : 0;
file_put_contents($file, $count + $this->writeCalls);
$file = $_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'analytics.' . strtotime('today') . '.json';
$data = file_exists($file) ? (array)json_decode((string)file_get_contents($file), true) : [];
$data['queries'] = ($data['queries'] ?? 0) + $this->writeCalls;
$data['sessions'] = ($data['sessions'] ?? 0) + 1;
file_put_contents($file, json_encode($data));
}
}
}

0 comments on commit d2e25fa

Please sign in to comment.