' . esc_html__( 'Only needed if AI visitor tracking is on under Outercite, Settings, AI visitors.', 'outercite' ) . '

' . '

' . esc_html__( 'Suggested text:', 'outercite' ) . ' ' . esc_html__( 'This site uses Outercite to count visits that arrive from AI answers, such as ChatGPT or Perplexity. When you visit a page, your browser sends Outercite the page address, the address you came from, your browser type, your window size and your IP address. Outercite sets one cookie, ct_sid, that lasts 30 minutes and links the pages you view in one visit.', 'outercite' ) . ' ' . sprintf( /* translators: %s: Outercite privacy policy address */ esc_html__( 'Outercite\'s privacy policy: %s', 'outercite' ), 'https://outercite.com/privacy' ) . '

'; wp_add_privacy_policy_content( 'Outercite', wp_kses_post( $text ) ); } /** * The stored switch: on/off, the key and the addresses. * * @return array */ public static function settings(): array { $saved = get_option( self::OPTION, array() ); return is_array( $saved ) ? $saved : array(); } /** * Whether to print the tags on this request. */ private static function active(): bool { $s = self::settings(); return ! empty( $s['on'] ) && ! empty( $s['key'] ) && ! empty( $s['script'] ) && ! is_admin() && ! Site::is_staging() && Connection::is_linked(); } /** * The script tags in the page head. */ public static function head(): void { if ( ! self::active() ) { return; } $s = self::settings(); // The queue stub lets events be recorded before the script has loaded. wp_print_inline_script_tag( 'window.citetrace = window.citetrace || { q: [], event: function () { (this.q = this.q || []).push(arguments); } };' ); wp_print_script_tag( array( 'src' => esc_url( $s['script'] ), 'async' => true, 'data-key' => esc_attr( $s['key'] ), ) ); } /** * The image tag, for AI fetchers that read the page but run no script. */ public static function footer(): void { if ( ! self::active() || empty( self::settings()['pixel'] ) ) { return; } printf( '', esc_url( self::settings()['pixel'] ) ); } /** * Turn tracking on or off from the settings screen. */ public static function handle_switch(): void { check_admin_referer( 'outercite_tracking' ); if ( ! current_user_can( Roles::CONNECT ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'outercite' ) ); } $on = isset( $_POST['on'] ) && '1' === $_POST['on']; $back = Admin::settings_url( array( 'tab' => 'visitors' ) ); if ( ! $on ) { self::stop(); Activity::add( 'tracking', __( 'Turned AI visitor tracking off', 'outercite' ) ); Admin::flash( 'success', __( 'AI visitor tracking is off. The tags are removed from your pages, and the page cache was cleared. If your host keeps its own cached copies, clear them too.', 'outercite' ) ); wp_safe_redirect( $back ); exit; } if ( Site::is_staging() ) { Admin::flash( 'error', __( 'Turn tracking on from the live site. A copy of the site never tracks visits.', 'outercite' ) ); wp_safe_redirect( $back ); exit; } $res = Ledger::enable_tracking(); if ( ! $res['ok'] || empty( $res['data']['publicKey'] ) ) { Admin::flash( 'error', $res['ok'] ? __( 'Outercite did not return a tracking key.', 'outercite' ) : $res['error'] ); wp_safe_redirect( $back ); exit; } update_option( self::OPTION, array( 'on' => '1', 'key' => (string) $res['data']['publicKey'], 'script' => (string) $res['data']['scriptUrl'], 'pixel' => (string) ( $res['data']['pixelUrl'] ?? '' ), ), false ); self::purge_page_cache(); Activity::add( 'tracking', __( 'Turned AI visitor tracking on', 'outercite' ) ); Admin::flash( 'success', __( 'AI visitor tracking is on. Visits from AI answers show in Outercite within a day of the first one.', 'outercite' ) ); wp_safe_redirect( $back ); exit; } /** * Turn tracking off and forget its key, then clear cached pages so no * stored copy keeps the tags. Used by the switch, by disconnecting and * by deactivating the plugin. */ public static function stop(): void { $was_on = ! empty( self::settings()['on'] ); delete_option( self::OPTION ); if ( $was_on ) { self::purge_page_cache(); } } /** * Clear the page cache so cached pages gain or lose the tags at once: * WordPress's object cache, the common cache plugins and hosts, and any * other cache that listens for outercite_purge_page_cache. */ public static function purge_page_cache(): void { // phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- other plugins' own hooks. if ( class_exists( '\WpeCommon' ) && method_exists( '\WpeCommon', 'purge_varnish_cache' ) ) { \WpeCommon::purge_varnish_cache(); // WP Engine. } if ( function_exists( 'rocket_clean_domain' ) ) { rocket_clean_domain(); // WP Rocket. } if ( function_exists( 'w3tc_flush_all' ) ) { w3tc_flush_all(); // W3 Total Cache. } if ( function_exists( 'wp_cache_clear_cache' ) ) { wp_cache_clear_cache(); // WP Super Cache. } if ( function_exists( 'sg_cachepress_purge_cache' ) ) { sg_cachepress_purge_cache(); // SiteGround. } do_action( 'litespeed_purge_all' ); // LiteSpeed Cache. do_action( 'wpfc_clear_all_cache' ); // WP Fastest Cache. do_action( 'cache_enabler_clear_complete_cache' ); // Cache Enabler. do_action( 'breeze_clear_all_cache' ); // Breeze (Cloudways). do_action( 'rt_nginx_helper_purge_all' ); // Nginx Helper. // phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound do_action( 'outercite_purge_page_cache' ); } /** * The AI visitors tab on the settings screen. */ public static function render(): void { $s = self::settings(); $on = ! empty( $s['on'] ); $form = '
' . wp_nonce_field( 'outercite_tracking', '_wpnonce', true, false ) . '' . '
'; echo '

' . esc_html__( 'AI visitors', 'outercite' ) . '

' . wp_kses_post( $on ? Admin::pill( __( 'On', 'outercite' ), 'green' ) : Admin::pill( __( 'Off', 'outercite' ) ) ) . $form . '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- the form is built from escaped parts above. echo '

' . esc_html__( 'See the visits that arrive from AI answers such as ChatGPT and Perplexity, and which pages they land on. Outercite adds a small script and image tag to every page; they run in the visitor\'s browser, so cached pages are tracked too.', 'outercite' ) . '

'; if ( $on ) { $status = Connection::is_linked() ? Ledger::tracking() : null; if ( $status && $status['ok'] && ! empty( $status['data']['lastSeenAt'] ) ) { echo '

' . esc_html( sprintf( /* translators: %s: date */ __( 'Last visit recorded %s.', 'outercite' ), wp_date( get_option( 'date_format' ), (int) strtotime( (string) $status['data']['lastSeenAt'] ) ) ) ) . '

'; } else { echo '

' . esc_html__( 'No visits from AI answers recorded yet.', 'outercite' ) . '

'; } } if ( Site::is_staging() ) { echo '

' . esc_html__( 'This is a copy of the site, so it never prints the tags.', 'outercite' ) . '

'; } echo '

' . esc_html__( 'What leaves the site', 'outercite' ) . '

' . esc_html__( 'From the visitor\'s browser: the page address, the referring address, the browser and the window size. The script sets one session cookie (ct_sid) that expires after 30 minutes without a visit. It sends no names or email addresses. Mention it in your privacy policy.', 'outercite' ) . '

'; } }