How to Fix WordPress Pixel Stat Tracking not Working in AMP Pages


If you recently updated your wordpress to 5.0 and updated the official AMP plugin by wordpress. You probably notice a drop of traffic from your website stats, these drops is actually the mobile users of your page who visits your AMP’s page. The wordpress pixel added to each AMP pages are now gone with the new updates and not working.

The developer didn’t yet give an update to this issue, but to temporarily fixed your problem. You may add this pieces of code to your templates/footer.php, this code can be found on the jetpack-helper.php.

<?php
/**
 * Footer template part.
 *
 * @package AMP
 */
function jetpack_amp_build_stats_pixel_url() {
	global $wp_the_query;
	if ( function_exists( 'stats_build_view_data' ) ) { // Added in <https://github.com/Automattic/jetpack/pull/3445>.
		$data = stats_build_view_data();
	} else {
		$blog     = Jetpack_Options::get_option( 'id' );
		$tz       = get_option( 'gmt_offset' );
		$v        = 'ext';
		$blog_url = wp_parse_url( site_url() );
		$srv      = $blog_url['host'];
		$j        = sprintf( '%s:%s', JETPACK__API_VERSION, JETPACK__VERSION );
		$post     = $wp_the_query->get_queried_object_id();
		$data     = compact( 'v', 'j', 'blog', 'post', 'tz', 'srv' );
	}


	$data['host'] = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : ''; // input var ok.
	$data['rand'] = 'RANDOM'; // AMP placeholder.
	$data['ref']  = 'DOCUMENT_REFERRER'; // AMP placeholder.
	$data         = array_map( 'rawurlencode', $data );
	return add_query_arg( $data, 'https://pixel.wp.com/g.gif' );
}
/**
 * Context.
 *
 * @var AMP_Post_Template $this
 */
?>
<footer class="amp-wp-footer">
	<div>
		<h2><?php echo esc_html( wptexturize( $this->get( 'blog_name' ) ) ); ?></h2>
		<p>
			<a href="<?php echo esc_url( esc_html__( 'https://wordpress.org/', 'amp' ) ); ?>">
				<?php
				// translators: %s is WordPress.
				//echo esc_html( sprintf( __( 'Powereds by %s', 'amp' ), 'WordPress' ) );
				?>
			</a>
		</p>
		<a href="#top" class="back-to-top"><?php esc_html_e( 'Back to top', 'amp' ); ?></a>
	</div>
</footer>
<amp-pixel src="<?php echo esc_url( jetpack_amp_build_stats_pixel_url() ); ?>"></amp-pixel>

This code will automatically add the wordpress pixel tracker to your amp pages including in the Google AMP cache.

Comments 0

Your email address will not be published. Required fields are marked *

How to Fix WordPress Pixel Stat Tracking not Working in AMP Pages

log in

Captcha!

reset password

Back to
log in
Choose A Format
Gif
GIF format