Skip to content

Commit

Permalink
feat: add anonymous user tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
HardeepAsrani committed Oct 14, 2024
1 parent 618a1a2 commit b370b04
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 5 deletions.
72 changes: 72 additions & 0 deletions inc/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Logger Class.
*
* @package Codeinwp/HyveLite
*/

namespace ThemeIsle\HyveLite;

/**
* Class Logger
*/
class Logger {

/**
* Tracking URL.
*
* @var string
*/
const TRACK_URL = 'https://api.themeisle.com/track/';

/**
* Send data to the server if the user has opted in.
*
* @param array $events Data to track.
* @return void
*/
public static function track( $events ) {
if ( ! self::has_consent() ) {
return;
}

try {
$payload = [];

$license = apply_filters( 'product_hyve_license_key', 'free' );

if ( 'free' !== $license ) {
$license = wp_hash( $license );
}

foreach ( $events as $event ) {
$payload[] = [
'slug' => 'hyve',
'site' => get_site_url(),
'license' => $license,
'data' => $event,
];
}

$args = [
'headers' => [
'Content-Type' => 'application/json',
],
'body' => wp_json_encode( $payload ),
];

wp_remote_post( self::TRACK_URL, $args );
} finally {
return;
}
}

/**
* Check if the user has consented to tracking.
*
* @return bool
*/
public static function has_consent() {
return 'yes' === get_option( 'hyve_lite_logger_flag', false ) || defined( 'HYVE_BASEFILE' );
}
}
46 changes: 41 additions & 5 deletions inc/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use ThemeIsle\HyveLite\Threads;
use ThemeIsle\HyveLite\API;
use ThemeIsle\HyveLite\Qdrant_API;
use ThemeIsle\HyveLite\Logger;

/**
* Class Main
Expand Down Expand Up @@ -57,6 +58,11 @@ public function __construct() {
add_action( 'admin_menu', [ $this, 'register_menu_page' ] );
add_action( 'save_post', [ $this, 'update_meta' ] );
add_action( 'delete_post', [ $this, 'delete_post' ] );
add_action( 'hyve_weekly_stats', [ $this, 'log_stats' ] );

if ( Logger::has_consent() && ! wp_next_scheduled( 'hyve_weekly_stats' ) ) {
wp_schedule_event( time(), 'weekly', 'hyve_weekly_stats' );
}

$settings = self::get_settings();

Expand Down Expand Up @@ -156,11 +162,7 @@ public function enqueue_options_assets() {
'assets' => [
'images' => HYVE_LITE_URL . 'assets/images/',
],
'stats' => [
'threads' => Threads::get_thread_count(),
'messages' => Threads::get_messages_count(),
'totalChunks' => $this->table->get_count(),
],
'stats' => $this->get_stats(),
'docs' => 'https://docs.themeisle.com/article/2009-hyve-documentation',
'qdrant_docs' => 'https://docs.themeisle.com/article/2066-integrate-hyve-with-qdrant',
'pro' => 'https://themeisle.com/plugins/hyve/',
Expand Down Expand Up @@ -283,6 +285,40 @@ public function enqueue_assets() {
);
}

/**
* Get stats.
*
* @since 1.3.0
*
* @return array
*/
public function get_stats() {
return [
'threads' => Threads::get_thread_count(),
'messages' => Threads::get_messages_count(),
'totalChunks' => $this->table->get_count(),
];
}

/**
* Log stats.
*
* @since 1.3.0
*
* @return void
*/
public function log_stats() {
Logger::track(
[
[
'feature' => 'system',
'featureComponent' => 'stats',
'featureValue' => $this->get_stats(),
],
]
);
}

/**
* Update meta.
*
Expand Down

0 comments on commit b370b04

Please sign in to comment.