Mattflaschen has uploaded a new change for review.

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

Change subject: Add experiment for testing sendBeacon reliability
......................................................................

Add experiment for testing sendBeacon reliability

The goals of the experiment are to get emperical data on
what browsers support sendBeacon, what proportion of our users
have these browsers, and the reliability of sendBeacon.

For browsers that do support it, we log the same event twice
(except noting which event is which) with the two methods
to allow correlating which events are not delivered.

For example, it will show what proportion of img events
come from a sendBeacon-supporting browser, but have no accompanying
sendBeacon event (or vice-versa).

For browsers without sendBeacon, there is nothing to correlate,
so if an img event is not delivered, we have no way to know.

As an optimization, only 1/10,000 views do the logging.

To make future data-gathering like this roll on and off faster,
set up a convention of a 'ext.wikimediaEvents' module that is always
present for this purpose (with or without code).  This solves
ResourceLoader issues (mainly related to cached anon HTML).

Bug: T44815
Change-Id: I07ecbde39b5f1ce640f40430b198f8c160a89d13
(cherry picked from commit 18a0e621b2ab24ddbef977a85aa63ccda5b101b5)
---
M WikimediaEvents.php
M WikimediaEventsHooks.php
A modules/ext.wikimediaEvents.sendBeacon.js
3 files changed, 51 insertions(+), 0 deletions(-)


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

diff --git a/WikimediaEvents.php b/WikimediaEvents.php
index fd0f50c..3e4cfd5 100644
--- a/WikimediaEvents.php
+++ b/WikimediaEvents.php
@@ -53,6 +53,11 @@
                'schema' => 'Edit',
                'revision' => 10676603,
        ),
+       'schema.SendBeaconReliability' => array(
+               'class' => 'ResourceLoaderSchemaModule',
+               'schema' => 'SendBeaconReliability',
+               'revision' => '10676430',
+       ),
        'ext.wikimediaEvents.ve' => array(
                'scripts'       => 'ext.wikimediaEvents.ve.js',
                'dependencies'  => 'ext.visualEditor.base',
@@ -66,6 +71,18 @@
                'remoteExtPath' => 'WikimediaEvents/modules',
                'targets' => array( 'desktop', 'mobile' ),
        ),
+       // This is for analytics code that is meant to load on all page views 
for both
+       // logged in and anonymous users.  It is intended that this module 
remain
+       // permanently (even if empty, to avoid errors on cached pages), and 
future code
+       // meeting this criteria be added to it.
+       'ext.wikimediaEvents' => array(
+               'scripts' => array(
+                       'ext.wikimediaEvents.sendBeacon.js',
+               ),
+               'localBasePath' => __DIR__ . '/modules',
+               'remoteExtPath' => 'WikimediaEvents/modules',
+               'targets' => array( 'desktop', 'mobile' ),
+       ),
 );
 
 $wgVisualEditorPluginModules[] = 'ext.wikimediaEvents.ve';
diff --git a/WikimediaEventsHooks.php b/WikimediaEventsHooks.php
index c0bb539..731dd85 100644
--- a/WikimediaEventsHooks.php
+++ b/WikimediaEventsHooks.php
@@ -25,11 +25,15 @@
 
        /**
         * Adds 'ext.wikimediaEvents.deprecate' logging module for logged-in 
users.
+        *
+        * Adds 'ext.wikimediaEvents' for all users.
         * @see https://meta.wikimedia.org/wiki/Schema:DeprecatedUsage
         * @param OutputPage &$out
         * @param Skin &$skin
         */
        public static function onBeforePageDisplay( &$out, &$skin ) {
+               $out->addModules( 'ext.wikimediaEvents' );
+
                $user = $out->getUser();
 
                if ( $user->isAnon() ) {
diff --git a/modules/ext.wikimediaEvents.sendBeacon.js 
b/modules/ext.wikimediaEvents.sendBeacon.js
new file mode 100644
index 0000000..57c9724
--- /dev/null
+++ b/modules/ext.wikimediaEvents.sendBeacon.js
@@ -0,0 +1,30 @@
+( function ( mw, $ ) {
+       var odds, isBeaconAvailable, baseEvent, imgEvent, beaconEvent;
+
+       odds = 0.0001; // 1 in 10,000 chance
+
+       if ( Math.random() < odds ) {
+               mw.loader.using( [ 'mediawiki.user', 
'schema.SendBeaconReliability' ] ).done( function () {
+                       isBeaconAvailable = !!navigator.sendBeacon;
+
+                       baseEvent = {
+                               browserSupportsSendBeacon: isBeaconAvailable,
+                               logId: mw.user.generateRandomSessionId()
+                       };
+
+                       imgEvent = $.extend( { method: 'logEvent' }, baseEvent 
);
+
+                       // We always log via logEvent (to at least get data on 
user agent and whether
+                       // it supports sendBeacon).  If sendBeacon is 
available, we also log via
+                       // logPersistentEvent.  Since logId is the same for 
both events, this allows
+                       // us to determine how common it is to have logEvent 
without
+                       // logPersistentEvent, or vice-versa.
+                       mw.eventLog.logEvent( 'SendBeaconReliability', imgEvent 
);
+
+                       if ( isBeaconAvailable ) {
+                               beaconEvent = $.extend( { method: 
'logPersistentEvent' }, baseEvent );
+                               mw.eventLog.logPersistentEvent( 
'SendBeaconReliability', beaconEvent );
+                       }
+               } );
+       }
+}( mediaWiki, jQuery ) );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I07ecbde39b5f1ce640f40430b198f8c160a89d13
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikimediaEvents
Gerrit-Branch: wmf/1.25wmf11
Gerrit-Owner: Mattflaschen <[email protected]>

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

Reply via email to