WordPress
A must-use plugin survives theme changes and cannot be deactivated by accident.
Install
<?php
/**
* Plugin Name: 3x Analytics
* Description: Cookieless first-party analytics (3x).
*/
const X3_KEY = '{{PUBLISHABLE_KEY}}';
const X3_HOST = '{{API_ORIGIN}}';
add_action('wp_enqueue_scripts', function () {
wp_enqueue_script('x3', X3_HOST . '/v1/s/x3.full.js?k=' . rawurlencode(X3_KEY), [], null, ['strategy' => 'async', 'in_footer' => false]);
wp_add_inline_script('x3', 'window.x3=window.x3||function(){(window.x3q=window.x3q||[]).push(arguments)}', 'before');
});
add_filter('script_loader_tag', function ($tag, $handle) {
if ($handle !== 'x3') return $tag;
return str_replace(' src=', ' data-key="' . esc_attr(X3_KEY) . '" src=', $tag);
}, 10, 2);Logged-in editors are measured too; add if (is_user_logged_in()) return; at the top of the
wp_enqueue_scripts callback to skip them, or open the site once with ?x3_notrack=1.
Verify
- Open the site in a private window and click around.
- The network tab shows
POST {{API_ORIGIN}}/v1/e→ 204. - Caching plugins (WP Rocket, LiteSpeed): purge the page cache, and exclude the 3x script from JavaScript combining/delaying if the request does not appear.
Proxy (optional)
A REST route on your own domain forwards events with the visitor's IP. Store the proxy key in
wp-config.php, never in the database:
define('X3_PROXY_KEY', 'sk_3x_YOUR_PROXY_KEY');Then follow WordPress proxy and change the script source to the REST route:
const X3_HOST = '/wp-json/n3';Consent
Consent plugins: set the CMP in the project's consent settings (Complianz and Borlabs have built-in adapters). Any other banner can call 3x from its accept/decline hooks:
document.addEventListener("cookie_consent_accepted", () => window.x3("consent", "granted"));
document.addEventListener("cookie_consent_declined", () => window.x3("consent", "denied"));add_action('wp_enqueue_scripts', function () {
wp_enqueue_script('x3-consent', content_url('mu-plugins/x3-consent.js'), ['x3'], null, ['in_footer' => true]);
});Replace the event names with your banner's.
Events
Block editor: add data-x3-* attributes via Advanced → HTML anchor / Additional CSS plugins, or
in custom HTML blocks:
<a class="wp-block-button__link" href="/contact" data-x3-cta data-x3-section="hero">Get a quote</a>Contact Form 7, Gravity Forms and WPForms are measured by the forms plugin (start, submit, abandon) without extra code.