MediaWiki:Common.js: Difference between revisions
Add Google Analytics 4 (anonymous visitors only) |
GA: skip missing pages; add site_check parameter to filter fake hits |
||
| (5 intermediate revisions by the same user not shown) | |||
| Line 41: | Line 41: | ||
if ( extraJS ) { | if ( extraJS ) { | ||
if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) { | if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) { | ||
mw.loader.load( '/index.php?title=' + extraJS + '&action=raw&ctype=text/javascript' ); | mw.loader.load( '/index.php?title=' + extraJS + '&action=raw&ctype=text/javascript&maxage=300&smaxage=300' ); | ||
} else { | } else { | ||
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } ); | mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } ); | ||
| Line 132: | Line 132: | ||
} ); | } ); | ||
/* Google Analytics 4 (property "Chabadpedia English - GA4") | /* Google Analytics 4 (property "Chabadpedia English - GA4") | ||
* Counted only: visitors who are not logged in, viewing a page for reading | |||
* (articles, portals, categories, project pages; not history, diffs, edits or special pages), | |||
* and only after a real action on the page (mouse, scroll, touch or key), which most bots never do. */ | |||
( function () { | ( function () { | ||
if ( mw.config.get( 'wgUserName' ) !== null ) { | if ( mw.config.get( 'wgUserName' ) !== null || mw.config.get( 'wgAction' ) !== 'view' ) { | ||
return; | |||
} | |||
var ns = mw.config.get( 'wgNamespaceNumber' ); | |||
if ( [ 0, 4, 14, 3000 ].indexOf( ns ) === -1 ) { | |||
return; | |||
} | |||
/* Pages that do not exist (404) are not counted; category pages without text are still counted */ | |||
if ( mw.config.get( 'wgArticleId' ) === 0 && ns !== 14 ) { | |||
return; | |||
} | |||
if ( /[?&](diff|oldid|curid|printable|redirect)=/.test( location.search ) ) { | |||
return; | return; | ||
} | } | ||
var id = 'G-V6K7LP5ZTY'; | var id = 'G-V6K7LP5ZTY'; | ||
var s = document.createElement( 'script' ); | var events = [ 'mousemove', 'scroll', 'touchstart', 'keydown', 'click' ]; | ||
var done = false; | |||
function start() { | |||
if ( done ) { | |||
return; | |||
} | |||
done = true; | |||
events.forEach( function ( ev ) { | |||
window.removeEventListener( ev, start, true ); | |||
} ); | |||
var s = document.createElement( 'script' ); | |||
s.async = true; | |||
s.src = 'https://www.googletagmanager.com/gtag/js?id=' + id; | |||
document.head.appendChild( s ); | |||
window.dataLayer = window.dataLayer || []; | |||
window.gtag = function () { window.dataLayer.push( arguments ); }; | |||
window.gtag( 'js', new Date() ); | |||
/* site_check marks hits sent by this code; reports can be filtered on it to drop fake hits sent straight to Google */ | |||
window.gtag( 'set', { site_check: 'cp1' } ); | |||
window.gtag( 'config', id, { site_check: 'cp1' } ); | |||
} | |||
events.forEach( function ( ev ) { | |||
window.addEventListener( ev, start, { capture: true, passive: true } ); | |||
} ); | |||
}() ); | }() ); | ||
/* Related Articles box at the bottom of articles: see [[MediaWiki:RelatedArticles.js]] */ | |||
mw.loader.load( mw.config.get( 'wgScript' ) + '?title=MediaWiki:RelatedArticles.js&action=raw&ctype=text/javascript&maxage=300&smaxage=300' ); | |||
/* "Read in Hebrew" button at the top of articles: see [[MediaWiki:ReadInHebrew.js]] */ | |||
mw.loader.load( mw.config.get( 'wgScript' ) + '?title=MediaWiki:ReadInHebrew.js&action=raw&ctype=text/javascript&maxage=300&smaxage=300' ); | |||
/* Short description under the article title: see [[MediaWiki:ShortDescription.js]] */ | |||
mw.loader.load( mw.config.get( 'wgScript' ) + '?title=MediaWiki:ShortDescription.js&action=raw&ctype=text/javascript&maxage=300&smaxage=300' ); | |||