Skip to content

Instantly share code, notes, and snippets.

@yuvipanda
Forked from anonymous/gist:3361818
Created August 15, 2012 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuvipanda/3361873 to your computer and use it in GitHub Desktop.
Save yuvipanda/3361873 to your computer and use it in GitHub Desktop.
/*jslint white:true, vars:true, browser: true */
/*global mw, $ */
//
// Helper Functions
//
mw.activeCampaigns = mw.activeCampaigns || {};
mw.activeCampaigns.PEF = {
name: 'PEF',
version : 1,
preferences: {
setBuckets: false
},
rates: {
control : 0,
experimental_1 : 0,
experimental_2 : 0
}
};
mw.loader.using( [ 'jquery.cookie', 'jquery.clickTracking', 'ext.UserBuckets' ], function () {
'use strict';
var DEBUG = !!$.cookie( 'clicktrackingDebug' );
var VERSION = 1;
var config = mw.config;
var log = function () {};
if ( DEBUG ) {
if ( typeof mw.domLog === 'function' ) {
log = mw.domLog;
} else if ( window.console && typeof console.log === 'function' ) {
log = $.proxy( console.log, console );
}
}
log( '*********************************' );
log( '*** Experiment Framework Init ***' );
log( '*********************************' );
log( window.location.href );
function getRevSignature() {
var articleId = config.get( 'wgArticleId' );
var revisionId = config.get( 'wgCurRevisionId' );
var userName = config.get( 'wgUserName' );
var userHash;
if ( !( articleId && revisionId && userName ) ) {
return null;
}
userHash = mw.experiments.murmur3( userName );
return [ articleId, revisionId, userHash ].join( ':' );
}
var revSignature = getRevSignature();
if ( revSignature === null ) {
return false;
}
function logWrap( func, name ) {
return function () {
var retval = func.apply( this, arguments );
log( name + '(): ' + retval );
return retval;
};
}
function didUserOptOut() {
return !!mw.user.options.get( 'vector-noexperiments' );
}
function isUserAnonymous() {
return config.get( 'wgUserName' ) === null;
}
if ( didUserOptOut() ) {
log( 'User opted out of experiments; aborting' );
return false;
}
if ( isUserAnonymous() ) {
log( 'Not logged in; aborting' );
return false;
}
function isPostEdit() {
return ( document.referrer.indexOf( 'action=edit' ) !== -1 && mw.util.getParamValue( 'pe' ) === '1' );
}
function getBuckets() {
// Hack for Ironholds
return ['anonymous'];
var buckets = {};
var cookie = $.cookie( 'userbuckets' );
if ( cookie === null ) {
return buckets;
}
try {
buckets = $.parseJSON( cookie );
} catch ( e ) {
if ( e instanceof SyntaxError ) {
$.cookie( 'userbuckets', null );
return buckets;
}
throw e;
}
return buckets;
}
function parseDbDate( dbDate ) {
if ( !dbDate ) {
return null;
}
dbDate = dbDate.toString();
if ( dbDate.length !== 14 ) {
return null;
}
var year = parseInt( dbDate.slice( 0, 4 ), 10 );
var month = parseInt( dbDate.slice( 4, 6 ), 10 ) - 1;
var day = parseInt( dbDate.slice( 6, 8 ), 10 );
var hours = parseInt( dbDate.slice( 8, 10 ), 10 );
var minutes = parseInt( dbDate.slice( 10, 12 ), 10 );
var seconds = parseInt( dbDate.slice( 12, 14 ), 10 );
return Date.UTC( year, month, day, hours, minutes, seconds );
}
function hoursToMs( hours ) {
return hours * 36e5;
}
function daysToMs( days ) {
return days * 864e5;
}
function getEventId( eventType, bucketName ) {
var id = [ 'ext.postEditFeedback', VERSION ].join( '@' );
return [ id, bucketName, eventType ].join( '-' );
}
function trackEvent( eventType, bucketName ) {
bucketName = bucketName || 'unbucketed';
return $.trackActionWithOptions( {
id : getEventId( bucketName, eventType ),
info : revSignature
} );
}
//
// Constants
//
var WIKI_EPOCH = 1008460800000; // Jan 15, 2001 at midnight, UTC
// Deployment time, UTC
// Mon Jul 30 2012 12:00:00 GMT-0700 (PDT)
var deploymentTime = 1343674800000;
// Current time, UTC
var currentTime = new Date().getTime();
// Date at which the experiment becomes active
var activationStart = deploymentTime + hoursToMs( 3 );
// Duration of experiment being active for anyone at all
var activationPeriod = daysToMs( 7 );
var eligibilityStart = activationStart;
var eligibilityPeriod = daysToMs( 7 );
var eligibilityStop = eligibilityStart + eligibilityPeriod;
var activationStop = eligibilityStop + activationPeriod;
var wgRegistration = window._reg;
if ( typeof wgRegistration !== 'string' ) {
log( 'Registration date not available; aborting' );
return false;
}
var whenRegistered = parseDbDate( wgRegistration );
if ( whenRegistered < WIKI_EPOCH ) {
log( 'Registration date failed sanity check; aborting' );
return false;
}
// Determine whether user is in the experiment *at all* (in *any* bucket).
function isUserInExperiment() {
// Ironholds modification
return true;
}
function getAssignedBucketOrNull( experimentName ) {
var bucket = getBuckets()[ experimentName ];
if ( !bucket || !bucket.length || !bucket[ 0 ] ) {
log( 'Not assigned to a bucket for experiment ' + experimentName );
return null;
}
return bucket[ 0 ];
}
function bucketUser() {
var userName = config.get( 'wgUserName' );
// Modification for Ironholds: serve only anon users
// Kill bucketing, do not accept for logged in users
if ( userName ) {
return false;
}
var bucketName = 'anonymous';
log( 'Assigning to bucket ' + bucketName );
$.setBucket( 'PEF', bucketName, 1 );
var beacon = [ bucketName, window.location.href ].join( '__' );
trackEvent( 'assignment', bucketName );
return bucketName;
}
function pageIsAppropriate() {
return ( config.get( 'wgIsArticle' )
&& config.get( 'wgAction' ).match( /view|purge/ )
&& mw.util.getParamValue( 'redirect' ) !== 'no'
&& mw.util.getParamValue( 'printable' ) !== 'yes'
&& mw.util.getParamValue( 'diff' ) === null
&& mw.util.getParamValue( 'oldid' ) === null
);
}
function isExperimentActive() {
if ( mw.util.getParamValue( 'pef_active' ) === "1" ) {
return true;
}
return ( currentTime >= activationStart ) && ( currentTime <= activationStop );
}
function isExperimentOn() {
if ( mw.util.getParamValue( 'pef_on' ) === "1" ) {
return true;
}
if ( !isExperimentActive() || !isUserInExperiment() ) {
return false;
}
return currentTime <= ( whenRegistered + activationPeriod );
}
function showFeedback( message ) {
var $pef = $( '.pef-notification-container' );
$( 'span', $pef ).text( message );
$pef.show();
window.setTimeout( function () {
$pef.fadeOut();
}, 1500 );
}
function performExperiment( bucketName ) {
var message = mw.msg( 'experiments-pef-' + bucketName );
trackEvent( 'postEdit', bucketName );
switch ( bucketName ) {
case 'anonymous':
showFeedback( message );
break;
case 'experimental_1':
showFeedback( message );
break;
case 'experimental_2':
showFeedback( message );
break;
case 'control':
break;
}
return true;
}
function getSetBucket() {
var bucket = getAssignedBucketOrNull( 'PEF' );
return bucket !== null ? bucket : bucketUser();
}
function isViewFresh() {
return $.cookie( revSignature ) === null;
}
function markViewStale() {
$.cookie( revSignature, '1' );
}
//
// Main
//
var bucketName = null;
if ( DEBUG ) {
log( 'Experiment active? ' + isExperimentActive() );
log( 'User eligible? ' + isUserInExperiment() );
log( 'Experiment visible? ' + isExperimentOn() );
log( 'Post edit? ' + isPostEdit() );
log( 'Current time: ' + new Date( currentTime ) );
log( 'Deployment time: ' + new Date( deploymentTime ) );
log( 'Registration time: ' + new Date( whenRegistered ) );
log( 'Treatment period (in hours): ' + activationPeriod / 36e5 );
log( 'Eligibility period (in hours): ' + eligibilityPeriod / 36e5 );
}
if ( isExperimentActive() ) {
if ( isUserInExperiment() ) {
log( 'User is eligible' );
bucketName = getSetBucket();
log( 'User in bucket ' + bucketName );
if ( isPostEdit() && pageIsAppropriate() && isExperimentOn() ) {
log( 'Page is appropriate; Experiment is on' );
if ( isViewFresh() ) {
markViewStale();
performExperiment( bucketName );
}
}
}
}
mw.experiments = mw.experiments || {};
$.extend( mw.experiments, {
bucketUser : bucketUser,
daysToMs : daysToMs,
didUserOptOut : didUserOptOut,
getAssignedBucketOrNull : getAssignedBucketOrNull,
getBuckets : getBuckets,
getEventId : getEventId,
getRevSignature : getRevSignature,
getSetBucket : getSetBucket,
hoursToMs : hoursToMs,
isExperimentActive : isExperimentActive,
isExperimentOn : isExperimentOn,
isPostEdit : isPostEdit,
isUserAnonymous : isUserAnonymous,
isUserInExperiment : isUserInExperiment,
isViewFresh : isViewFresh,
logWrap : logWrap,
markViewStale : markViewStale,
pageIsAppropriate : pageIsAppropriate,
parseDbDate : parseDbDate,
performExperiment : performExperiment,
showFeedback : showFeedback,
trackEvent : trackEvent
} );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment