summaryrefslogtreecommitdiff
blob: 63b2a81481d48b7ab490fcce2eca87c009d8a275 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
/**
 * Cloudflare Analytics
 * Let WPCOM users automatically insert a Cloudflare analytics JS snippet into their site header.
 *
 * @package automattic/jetpack
 */

namespace Automattic\Jetpack\Cloudflare_Analytics;

use Automattic\Jetpack\Status\Host;

/**
 * Add Cloudflare Analytics tracking code to the head.
 * This is currently only available to Atomic and WordPress.com Simple sites.
 *
 * @since 9.5.0
 */
function insert_tracking_id() {
	$option = get_option( 'jetpack_cloudflare_analytics' );

	if (
		! empty( $option['code'] )
		&& ! is_admin()
		&& ( class_exists( 'Jetpack_AMP_Support' ) && ! \Jetpack_AMP_Support::is_amp_request() )
		&& ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || ( new Host() )->is_woa_site() )
	) {
		printf(
			"<!-- Jetpack Cloudflare Web Analytics -->
<script defer
	src='https://static.cloudflareinsights.com/beacon.min.js'
	data-cf-beacon='{\"token\": \"%s\"}'>
</script>
<!-- End Jetpack Cloudflare Web Analytics -->\r\n",
			esc_html( $option['code'] )
		);
	}
}
add_action( 'wp_footer', __NAMESPACE__ . '\insert_tracking_id', 999 );