Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions admin/partials/dashboard-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
set_transient('webdecoy_dashboard_crawlers', $crawlers, 5 * MINUTE_IN_SECONDS);
}

// Page visits AI products (ChatGPT, Perplexity, Claude...) sent over 7 days.
// Counted locally on every install (WebDecoy_AI_Referrals).
$ai_referrals = get_transient('webdecoy_dashboard_ai_referrals');
if ($ai_referrals === false) {
$ai_referrals = class_exists('WebDecoy_AI_Referrals')
? WebDecoy_AI_Referrals::summary(7)
: ['total' => 0, 'platforms' => [], 'pages' => []];
set_transient('webdecoy_dashboard_ai_referrals', $ai_referrals, 5 * MINUTE_IN_SECONDS);
}

$request_counts_today = WebDecoy_Request_Counts::summary(1);

// Checkout bot signals in the last 24 hours: velocity, card-testing patterns,
Expand Down Expand Up @@ -126,6 +136,24 @@
</p>
</div>

<div class="webdecoy-widget-ai-referrals">
<h4><?php esc_html_e('Visits sent by AI assistants (7 days)', 'webdecoy'); ?></h4>
<?php if ((int) $ai_referrals['total'] > 0) : ?>
<ul class="webdecoy-widget-agents">
<?php foreach (array_slice($ai_referrals['platforms'], 0, 5) as $webdecoy_dw_ref) : ?>
<li>
<span class="webdecoy-agent-name"><strong><?php echo esc_html($webdecoy_dw_ref['platform']); ?></strong></span>
<span class="webdecoy-count"><?php echo esc_html(number_format((int) $webdecoy_dw_ref['referrals'])); ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php else : ?>
<p class="webdecoy-widget-verification">
<?php esc_html_e('None yet. When someone clicks a link to this site in ChatGPT, Perplexity, Claude, Gemini or Copilot, the visit is counted here.', 'webdecoy'); ?>
</p>
<?php endif; ?>
</div>

<div class="webdecoy-widget-request-counts">
<h4><?php esc_html_e('Detection rows and requests today (UTC)', 'webdecoy'); ?></h4>
<?php if ($request_counts_today['available']) : ?>
Expand Down
52 changes: 52 additions & 0 deletions admin/partials/statistics-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@
// Known crawlers, AI agents and bot tools over 7 days (counters only).
$crawler_summary = class_exists('WebDecoy_Crawler_Visits') ? WebDecoy_Crawler_Visits::summary(7 * 24) : null;

// Page visits AI products sent over 30 days (counters only).
$ai_referral_summary = class_exists('WebDecoy_AI_Referrals') ? WebDecoy_AI_Referrals::summary(WebDecoy_AI_Referrals::LOCAL_RETENTION_DAYS) : null;

$request_windows = [1 => WebDecoy_Request_Counts::summary(1), 7 => WebDecoy_Request_Counts::summary(7), 30 => WebDecoy_Request_Counts::summary(30)];
$options = get_option('webdecoy_options', []);
?>
Expand Down Expand Up @@ -245,6 +248,55 @@
<?php endif; ?>
</div>

<!-- Visits AI assistants sent -->
<?php if ($ai_referral_summary !== null) : ?>
<div class="webdecoy-chart-card">
<h3><?php esc_html_e('Visits Sent by AI Assistants (30 Days)', 'webdecoy'); ?></h3>
<p class="description">
<?php esc_html_e('Page visits that arrived from a link in ChatGPT, Perplexity, Claude, Gemini, Copilot and other AI products, counted on this server with no account. Only a count per AI product and landing page per day is kept: nothing about the visitor. Pages served from a page cache are not counted, so these are minimums.', 'webdecoy'); ?>
</p>
<?php if ((int) $ai_referral_summary['total'] === 0) : ?>
<p class="description"><?php esc_html_e('No visits from AI assistants yet.', 'webdecoy'); ?></p>
<?php else : ?>
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th><?php esc_html_e('AI product', 'webdecoy'); ?></th>
<th><?php esc_html_e('Visits', 'webdecoy'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($ai_referral_summary['platforms'] as $ref_row) : ?>
<tr>
<td><strong><?php echo esc_html($ref_row['platform']); ?></strong></td>
<td><?php echo esc_html(number_format((int) $ref_row['referrals'])); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php if (!empty($ai_referral_summary['pages'])) : ?>
<h4><?php esc_html_e('Pages AI assistants send people to', 'webdecoy'); ?></h4>
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th><?php esc_html_e('Landing page', 'webdecoy'); ?></th>
<th><?php esc_html_e('Visits', 'webdecoy'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach (array_slice($ai_referral_summary['pages'], 0, 20) as $ref_page) : ?>
<tr>
<td><a href="<?php echo esc_url(home_url($ref_page['path'])); ?>"><code><?php echo esc_html($ref_page['path']); ?></code></a></td>
<td><?php echo esc_html(number_format((int) $ref_page['referrals'])); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<?php endif; ?>
</div>
<?php endif; ?>

<!-- Known bots and AI crawlers -->
<?php if ($crawler_summary !== null) : ?>
<div class="webdecoy-chart-card">
Expand Down
22 changes: 21 additions & 1 deletion includes/class-webdecoy-activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class WebDecoy_Activator
/**
* Database version for migrations
*/
private const DB_VERSION = '2.11.1';
private const DB_VERSION = '2.13.0';

/**
* Plugin activation
Expand Down Expand Up @@ -165,6 +165,19 @@ private static function create_tables(): bool
PRIMARY KEY (bucket, outcome)
) $charset_collate;";

// Local AI referral counts (2.13.0): page visits AI products sent, per
// UTC day, platform and landing path, kept 30 days for the widget and
// Statistics page with no account. Counts only; nothing about a
// visitor. path_key is sha1(landing_path) for index limits.
$sql_ai_referral_days = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}webdecoy_ai_referral_days (
day DATE NOT NULL,
platform VARCHAR(50) NOT NULL,
path_key CHAR(40) NOT NULL,
landing_path VARCHAR(500) NOT NULL,
referrals BIGINT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (day, platform, path_key)
) $charset_collate;";

require_once ABSPATH . 'wp-admin/includes/upgrade.php';

dbDelta($sql_blocked);
Expand All @@ -174,6 +187,7 @@ private static function create_tables(): bool
dbDelta($sql_violation_queue);
dbDelta($sql_ai_referrals);
dbDelta($sql_crawler_visits);
dbDelta($sql_ai_referral_days);
dbDelta($sql_request_counts);
$table = $wpdb->prefix . 'webdecoy_request_counts';
$exists = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $wpdb->esc_like($table))) === $table;
Expand Down Expand Up @@ -303,6 +317,7 @@ public static function uninstall(): void
$wpdb->prefix . 'webdecoy_ai_referrals',
$wpdb->prefix . 'webdecoy_crawler_visits',
$wpdb->prefix . 'webdecoy_request_counts',
$wpdb->prefix . 'webdecoy_ai_referral_days',
];

foreach ($tables as $table) {
Expand Down Expand Up @@ -359,6 +374,11 @@ public static function uninstall(): void
WebDecoy_Crawler_Visits::prune();
}

// Local AI referral counts keep 30 days.
if (class_exists('WebDecoy_AI_Referrals')) {
WebDecoy_AI_Referrals::prune();
}

if (class_exists('WebDecoy_Request_Counts')) {
WebDecoy_Request_Counts::prune();
}
Expand Down
185 changes: 174 additions & 11 deletions includes/class-webdecoy-ai-referrals.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
* metadata says it is a document navigation. Pages served from a full-page
* cache never reach PHP and are not counted, so the figure is a floor.
*
* Counts accumulate in one small table and are sent every fifteen minutes when
* the site is connected to WebDecoy Cloud. Each send claims the open counts
* under a batch id; a failed send keeps that id and is retried with it, and
* WebDecoy counts a batch id once.
* Every install keeps its own daily counts (platform, landing path, number)
* for {@see self::LOCAL_RETENTION_DAYS} days, shown on the dashboard widget and
* the Statistics page with no account. When the site is also connected to
* WebDecoy Cloud, counts accumulate in a second small table and are sent every
* fifteen minutes. Each send claims the open counts under a batch id; a failed
* send keeps that id and is retried with it, and WebDecoy counts a batch id
* once.
*/
class WebDecoy_AI_Referrals
{
Expand All @@ -31,6 +34,21 @@ class WebDecoy_AI_Referrals
/** Distinct platform and path pairs kept before new ones are dropped. */
private const MAX_ROWS = 5000;

/** The schema version that created the local daily table. Recording waits for it. */
public const SINCE_DB_VERSION = '2.13.0';
/** Days of local daily counts kept. */
public const LOCAL_RETENTION_DAYS = 30;
/**
* Distinct landing paths kept per day. Past it a referral still counts for
* its platform, under an empty path, so platform totals stay exact.
*/
private const MAX_PATHS_PER_DAY = 1000;

/** True once register() has turned counting on. */
private static bool $counting = false;
/** True when counts are also queued for WebDecoy Cloud. */
private static bool $cloud = false;

/**
* The ai_referrals table name, prefixed and escaped for interpolation.
*/
Expand All @@ -40,22 +58,39 @@ public static function table(): string
return esc_sql($wpdb->prefix . 'webdecoy_ai_referrals');
}

/** The local daily counts table name, prefixed and escaped for interpolation. */
public static function days_table(): string
{
global $wpdb;
return esc_sql($wpdb->prefix . 'webdecoy_ai_referral_days');
}

/**
* Whether counting runs: connected to WebDecoy Cloud, and not turned off
* with the webdecoy_count_ai_referrals filter.
* Whether counting runs at all: on unless turned off with the
* webdecoy_count_ai_referrals filter. Needs no account.
*/
public static function enabled(string $apiKey): bool
public static function enabled(): bool
{
return $apiKey !== '' && (bool) apply_filters('webdecoy_count_ai_referrals', true);
return (bool) apply_filters('webdecoy_count_ai_referrals', true);
}

/** Wire the counter and its flush, scheduling the flush if it is not. */
/**
* Wire the counter. Counts are always kept locally; with an API key they
* are also sent to WebDecoy Cloud, and the flush is scheduled.
*/
public static function register(string $apiKey): void
{
if (!self::enabled($apiKey)) {
self::$counting = false;
self::$cloud = false;
if (!self::enabled()) {
return;
}
self::$counting = true;
add_action('template_redirect', [self::class, 'observe'], 1);
self::$cloud = $apiKey !== '';
if (!self::$cloud) {
return;
}
add_action(self::CRON_HOOK, static function () use ($apiKey): void {
self::flush($apiKey);
});
Expand All @@ -67,6 +102,9 @@ public static function register(string $apiKey): void
/** Count the current request if it is a page visit an AI product sent. */
public static function observe(): void
{
if (!self::$counting) {
return;
}
// phpcs:disable WordPress.Security.ValidatedSanitizedInput -- compared and classified, never stored or echoed
$method = strtoupper((string) ($_SERVER['REQUEST_METHOD'] ?? ''));
$mode = (string) ($_SERVER['HTTP_SEC_FETCH_MODE'] ?? '');
Expand All @@ -78,7 +116,132 @@ public static function observe(): void
if ($platform === '') {
return;
}
self::increment($platform, self::landingPath($uri));
$path = self::landingPath($uri);
self::recordDay($platform, $path);
if (self::$cloud) {
self::increment($platform, $path);
}
}

/** The UTC day a timestamp counts against. Pure, for tests. */
public static function day(int $timestamp): string
{
return gmdate('Y-m-d', $timestamp);
}

/**
* Count one referral in the local daily table, bounded to
* {@see self::MAX_PATHS_PER_DAY} landing paths a day.
*/
private static function recordDay(string $platform, string $path): void
{
if (!self::localReady()) {
return;
}
global $wpdb;
$table = self::days_table();
$day = self::day(time());
// phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix
$known = (int) $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$table} WHERE day = %s AND platform = %s AND path_key = %s",
$day,
$platform,
sha1($path)
));
if ($known === 0) {
$paths = (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$table} WHERE day = %s", $day));
if ($paths >= self::MAX_PATHS_PER_DAY) {
$path = '';
}
}
$wpdb->query($wpdb->prepare(
"INSERT INTO {$table} (day, platform, path_key, landing_path, referrals) VALUES (%s, %s, %s, %s, 1)
ON DUPLICATE KEY UPDATE referrals = referrals + 1",
$day,
$platform,
sha1($path),
$path
));
// phpcs:enable
}

/**
* Referrals over the last N days (today included), for the dashboard
* widget and the Statistics page.
*
* @return array{total:int,platforms:list<array{platform:string,referrals:int}>,pages:list<array{path:string,referrals:int}>}
*/
public static function summary(int $days): array
{
if (!self::localReady()) {
return self::summarize([]);
}
global $wpdb;
$table = self::days_table();
// phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix
$rows = $wpdb->get_results($wpdb->prepare(
"SELECT platform, landing_path, SUM(referrals) AS referrals FROM {$table} WHERE day >= %s GROUP BY platform, landing_path",
self::day(time() - (max(1, $days) - 1) * DAY_IN_SECONDS)
), ARRAY_A);
// phpcs:enable
return self::summarize(is_array($rows) ? $rows : []);
}

/**
* Fold daily rows into a total, per-platform counts and per-page counts,
* busiest first. Rows past the daily path bound have an empty path: they
* count for their platform but name no page. Pure, for tests.
*
* @param list<array{platform:string,landing_path:string,referrals:int|string}> $rows
* @return array{total:int,platforms:list<array{platform:string,referrals:int}>,pages:list<array{path:string,referrals:int}>}
*/
public static function summarize(array $rows): array
{
$total = 0;
$platforms = [];
$pages = [];
foreach ($rows as $row) {
$platform = (string) ($row['platform'] ?? '');
$path = (string) ($row['landing_path'] ?? '');
$n = (int) ($row['referrals'] ?? 0);
if ($platform === '' || $n <= 0) {
continue;
}
$total += $n;
$platforms[$platform] = ($platforms[$platform] ?? 0) + $n;
if ($path !== '') {
$pages[$path] = ($pages[$path] ?? 0) + $n;
}
}
$byCount = static function (array $counts, string $key): array {
$list = [];
foreach ($counts as $name => $n) {
$list[] = [$key => (string) $name, 'referrals' => $n];
}
usort($list, static function (array $a, array $b) use ($key): int {
return $b['referrals'] <=> $a['referrals'] ?: strcmp($a[$key], $b[$key]);
});
return $list;
};
return ['total' => $total, 'platforms' => $byCount($platforms, 'platform'), 'pages' => $byCount($pages, 'path')];
}

/** Delete local daily counts older than the retention window. */
public static function prune(): void
{
if (!self::localReady()) {
return;
}
global $wpdb;
$table = self::days_table();
// phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix
$wpdb->query($wpdb->prepare("DELETE FROM {$table} WHERE day < %s", self::day(time() - self::LOCAL_RETENTION_DAYS * DAY_IN_SECONDS)));
}

/** Whether the local daily table exists yet (the schema upgrade has run). */
private static function localReady(): bool
{
return version_compare((string) get_option('webdecoy_db_version', '0'), self::SINCE_DB_VERSION, '>=');
}

/**
Expand Down
Loading
Loading