From 4b70301e2ad0eed9d9200a80d0b8633f8478e30f Mon Sep 17 00:00:00 2001 From: Elliott Stocks Date: Thu, 7 Apr 2016 22:57:18 +0100 Subject: [PATCH] First pass at tracking individual image hits when full-screen, in Carousel --- modules/carousel/jetpack-carousel.js | 8 ++++++++ modules/carousel/jetpack-carousel.php | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/modules/carousel/jetpack-carousel.js b/modules/carousel/jetpack-carousel.js index dd5868662d6d5..5576995779f48 100644 --- a/modules/carousel/jetpack-carousel.js +++ b/modules/carousel/jetpack-carousel.js @@ -692,6 +692,14 @@ jQuery(document).ready(function($) { caption.fadeOut( 'fast' ).empty(); } + // Record pageview in WP Stats, for each new image loaded full-screen. + if ( jetpackCarouselStrings.stats ) { + new Image().src = document.location.protocol + + '//pixel.wp.com/g.gif?' + + jetpackCarouselStrings.stats + + '&post=' + encodeURIComponent( attachmentId ) + + '&rand=' + Math.random(); + } // Load the images for the next and previous slides. $( next ).add( previous ).each( function() { diff --git a/modules/carousel/jetpack-carousel.php b/modules/carousel/jetpack-carousel.php index b3d5302dd455d..6118905b8dece 100644 --- a/modules/carousel/jetpack-carousel.php +++ b/modules/carousel/jetpack-carousel.php @@ -211,6 +211,24 @@ function enqueue_assets( $output ) { } } + /** + * Handle WP stats for images in full-screen. + * Build string with tracking info. + */ + if ( ! in_array( 'stats', Jetpack::get_active_modules() ) ) { + $localize_strings['stats'] = 'blog=' . Jetpack_Options::get_option( 'id' ) . '&host=' . parse_url( get_option( 'home' ), PHP_URL_HOST ) . '&v=ext&j=' . JETPACK__API_VERSION . ':' . JETPACK__VERSION; + + // Set the stats as empty if user is logged in but logged-in users shouldn't be tracked. + if ( is_user_logged_in() && function_exists( 'stats_get_options' ) ) { + $stats_options = stats_get_options(); + $track_loggedin_users = isset( $stats_options['reg_users'] ) ? (bool) $stats_options['reg_users'] : false; + + if ( ! $track_loggedin_users ) { + $localize_strings['stats'] = ''; + } + } + } + /** * Filter the strings passed to the Carousel's js file. *