Skip to content

Commit

Permalink
Build stats pixel for Jetpack sites
Browse files Browse the repository at this point in the history
  • Loading branch information
mjangda committed Feb 24, 2016
1 parent 93367aa commit 84ee0c6
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions jetpack-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*
**/
function amp_jetpack_mods() {
if ( Jetpack::is_module_active( 'stats' ) ) {
add_action( 'amp_post_template_footer', 'jetpack_amp_add_stats_pixel' );
}
amp_jetpack_disable_sharing();
amp_jetpack_disable_related_posts();
}
Expand All @@ -29,3 +32,38 @@ function amp_jetpack_disable_related_posts() {
remove_filter( 'the_content', array( $jprp, 'filter_add_target_to_dom' ), 40 );
}
}

function jetpack_amp_add_stats_pixel( $amp_template ) {
if ( ! has_action( 'wp_footer', 'stats_footer' ) ) {
return;
}
?>
<amp-pixel src="<?php echo esc_url( jetpack_amp_build_stats_pixel_url() ); ?>"></amp-pixel>
<?php
}

/**
* Generate the stats pixel.
*
* Looks something like:
* https://pixel.wp.com/g.gif?v=ext&j=1%3A3.9.1&blog=1234&post=5678&tz=-4&srv=example.com&host=example.com&ref=&rand=0.4107963021218808
*/
function jetpack_amp_build_stats_pixel_url() {
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 = 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['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' );
}

0 comments on commit 84ee0c6

Please sign in to comment.