Cloudflare Web Analytics Beacon
Adds Cloudflare's free, cookieless Web Analytics beacon to your public pages, for sites not proxied through Cloudflare or that want manual setup.
Opens WPIDE on
Code
add_action( 'wp_footer', function () {
$token = 'YOUR_TOKEN'; // Web Analytics site token.
if ( 'YOUR_TOKEN' === $token || current_user_can( 'edit_posts' ) ) {
return;
}
printf( "<script defer src=\"https://static.cloudflareinsights.com/beacon.min.js\" data-cf-beacon=\"%s\"></script>\n", esc_attr( wp_json_encode( array( 'token' => $token ) ) ) );
} );
Notes
Replace YOUR_TOKEN with the token from Cloudflare dashboard > Analytics & Logs > Web Analytics. Nothing is output while the placeholder is in place.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.