MaxSem has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/228997

Change subject: Track Wikivoyage maps usage
......................................................................

Track Wikivoyage maps usage

Bug: T107324
Change-Id: Ifd88b3d1bbbfe8e626906ca17778fa36b3870baa
(cherry picked from commit d0d3cc54cb05b9abfd3f771d0a71ce974c1942d2)
---
M WikimediaEvents.php
M modules/ext.wikimediaEvents.geoFeatures.js
2 files changed, 73 insertions(+), 2 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikimediaEvents 
refs/changes/97/228997/1

diff --git a/WikimediaEvents.php b/WikimediaEvents.php
index 6771582..92675dd 100644
--- a/WikimediaEvents.php
+++ b/WikimediaEvents.php
@@ -121,7 +121,7 @@
        'schema.GeoFeatures' => array(
                'class'    => 'ResourceLoaderSchemaModule',
                'schema'   => 'GeoFeatures',
-               'revision' => 12518424,
+               'revision' => 12914994,
        ),
        'ext.wikimediaEvents.geoFeatures' => array(
                'scripts'       => array(
diff --git a/modules/ext.wikimediaEvents.geoFeatures.js 
b/modules/ext.wikimediaEvents.geoFeatures.js
index 557bf61..3be2f76 100644
--- a/modules/ext.wikimediaEvents.geoFeatures.js
+++ b/modules/ext.wikimediaEvents.geoFeatures.js
@@ -4,8 +4,11 @@
  * @see https://meta.wikimedia.org/wiki/Schema:GeoFeatures
  */
 ( function( $, mw ) {
+       var oldHide = $.fn.hide,
+               // Which iframes have already been logged
+               tracked = {};
+
        // Override hide() to track it
-       var oldHide = $.fn.hide;
        $.fn.hide = function() {
                $( this ).trigger( 'hide' );
                return oldHide.apply( this, arguments );
@@ -71,6 +74,45 @@
                }
        }
 
+       /**
+        * Returns whether at least part of a given element is scrolled into 
view
+        * @param {jQuery} $el
+        * @returns {bool}
+        */
+       function isVisible( $el ) {
+               var $window = $( window ),
+                       top = $window.scrollTop(),
+                       bottom = top + $window.height(),
+                       elTop = $el.offset().top,
+                       elBottom = elTop + $el.height();
+
+               return ( elTop >= top && elTop <= bottom ) || ( elBottom >= top 
&& elBottom <= bottom );
+       }
+
+       /**
+        * Logs focus switches to a given iframe as interactions with a given 
feature
+        *
+        * @param {string} selector Selector of iframe to track
+        * @param {string} feature Feature name to log
+        */
+       function trackIframe( selector, feature ) {
+               $( window ).on( 'blur', function() {
+                       // Wait for event loop to process updates to be sure
+                       setTimeout( function() {
+                               var $el;
+
+                               // Fastest checks first
+                               if ( !tracked[selector]
+                                       && document.activeElement instanceof 
HTMLIFrameElement
+                                       && ( $el = $( document.activeElement ) 
).is( selector )
+                               ) {
+                                       tracked[selector] = true;
+                                       track( feature, 'interaction', 
$el.data( 'fromPrimaryCoordinate' ) === 'yes' );
+                               }
+                       }, 0 );
+               } );
+       }
+
        // Track GeoHack usage
        $( 'a[href^=\'//tools.wmflabs.org/geohack/geohack.php\']' ).on( 
'click', function( event ) {
                var $this = $( this );
@@ -97,4 +139,33 @@
                var mapShown = $( 'iframe#openstreetmap' ).is( ':visible' );
                track( 'WIWOSM', mapShown ? 'open' : 'close', true );
        } );
+
+       // Track Wikivoyage maps
+       ( function() {
+               function onScroll() {
+                       if ( isVisible( $map ) ) {
+                               track( 'Wikivoyage', 'view', false );
+                               $( window ).off( 'scroll', onScroll );
+                       }
+               }
+
+               var $map = $( '#mapwrap #mapdiv' );
+
+               if ( !$map.length ) {
+                       return;
+               }
+
+               trackIframe( '#mapwrap #mapdiv iframe', 'Wikivoyage' );
+
+               // Log only 1 of 100 views to prevent a flood
+               if ( Math.random() * 100 > 1 ) {
+                       return;
+               }
+
+               if ( isVisible( $map ) ) {
+                       track( 'Wikivoyage', 'view', false );
+               } else {
+                       $( window ).on( 'scroll', onScroll );
+               }
+       } ) ();
 }( jQuery, mediaWiki ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/228997
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifd88b3d1bbbfe8e626906ca17778fa36b3870baa
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikimediaEvents
Gerrit-Branch: wmf/1.26wmf16
Gerrit-Owner: MaxSem <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to