Skip to content

Commit ef256db

Browse files
committed
feat: add anonymous user tracking
1 parent 77d9924 commit ef256db

File tree

2 files changed

+113
-5
lines changed

2 files changed

+113
-5
lines changed

inc/Logger.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Logger Class.
4+
*
5+
* @package Codeinwp/HyveLite
6+
*/
7+
8+
namespace ThemeIsle\HyveLite;
9+
10+
/**
11+
* Class Logger
12+
*/
13+
class Logger {
14+
15+
/**
16+
* Tracking URL.
17+
*
18+
* @var string
19+
*/
20+
const TRACK_URL = 'https://api.themeisle.com/track/';
21+
22+
/**
23+
* Send data to the server if the user has opted in.
24+
*
25+
* @param array $events Data to track.
26+
* @return void
27+
*/
28+
public static function track( $events ) {
29+
if ( ! self::has_consent() ) {
30+
return;
31+
}
32+
33+
try {
34+
$payload = [];
35+
36+
$license = apply_filters( 'product_hyve_license_key', 'free' );
37+
38+
if ( 'free' !== $license ) {
39+
$license = wp_hash( $license );
40+
}
41+
42+
foreach ( $events as $event ) {
43+
$payload[] = [
44+
'slug' => 'hyve',
45+
'site' => get_site_url(),
46+
'license' => $license,
47+
'data' => $event,
48+
];
49+
}
50+
51+
$args = [
52+
'headers' => [
53+
'Content-Type' => 'application/json',
54+
],
55+
'body' => wp_json_encode( $payload ),
56+
];
57+
58+
wp_remote_post( self::TRACK_URL, $args );
59+
} finally {
60+
return;
61+
}
62+
}
63+
64+
/**
65+
* Check if the user has consented to tracking.
66+
*
67+
* @return bool
68+
*/
69+
public static function has_consent() {
70+
return 'yes' === get_option( 'hyve_lite_logger_flag', false ) || defined( 'HYVE_BASEFILE' );
71+
}
72+
}

inc/Main.php

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use ThemeIsle\HyveLite\Threads;
1414
use ThemeIsle\HyveLite\API;
1515
use ThemeIsle\HyveLite\Qdrant_API;
16+
use ThemeIsle\HyveLite\Logger;
1617

1718
/**
1819
* Class Main
@@ -57,6 +58,11 @@ public function __construct() {
5758
add_action( 'admin_menu', [ $this, 'register_menu_page' ] );
5859
add_action( 'save_post', [ $this, 'update_meta' ] );
5960
add_action( 'delete_post', [ $this, 'delete_post' ] );
61+
add_action( 'hyve_weekly_stats', [ $this, 'log_stats' ] );
62+
63+
if ( Logger::has_consent() && ! wp_next_scheduled( 'hyve_weekly_stats' ) ) {
64+
wp_schedule_event( time(), 'weekly', 'hyve_weekly_stats' );
65+
}
6066

6167
$settings = self::get_settings();
6268

@@ -156,11 +162,7 @@ public function enqueue_options_assets() {
156162
'assets' => [
157163
'images' => HYVE_LITE_URL . 'assets/images/',
158164
],
159-
'stats' => [
160-
'threads' => Threads::get_thread_count(),
161-
'messages' => Threads::get_messages_count(),
162-
'totalChunks' => $this->table->get_count(),
163-
],
165+
'stats' => $this->get_stats(),
164166
'docs' => 'https://docs.themeisle.com/article/2009-hyve-documentation',
165167
'qdrant_docs' => 'https://docs.themeisle.com/article/2066-integrate-hyve-with-qdrant',
166168
'pro' => 'https://themeisle.com/plugins/hyve/',
@@ -283,6 +285,40 @@ public function enqueue_assets() {
283285
);
284286
}
285287

288+
/**
289+
* Get stats.
290+
*
291+
* @since 1.3.0
292+
*
293+
* @return array
294+
*/
295+
public function get_stats() {
296+
return [
297+
'threads' => Threads::get_thread_count(),
298+
'messages' => Threads::get_messages_count(),
299+
'totalChunks' => $this->table->get_count(),
300+
];
301+
}
302+
303+
/**
304+
* Log stats.
305+
*
306+
* @since 1.3.0
307+
*
308+
* @return void
309+
*/
310+
public function log_stats() {
311+
Logger::track(
312+
[
313+
[
314+
'feature' => 'system',
315+
'featureComponent' => 'stats',
316+
'featureValue' => $this->get_stats(),
317+
],
318+
]
319+
);
320+
}
321+
286322
/**
287323
* Update meta.
288324
*

0 commit comments

Comments
 (0)