Skip to content

Instantly share code, notes, and snippets.

@renoirb
Last active December 25, 2015 05:59
Show Gist options
  • Save renoirb/6929061 to your computer and use it in GitHub Desktop.
Save renoirb/6929061 to your computer and use it in GitHub Desktop.
Piwik tracking code
// Refactor note, use mw.loader.using('mw.user',setupTracking);
/**
* Read address GET parameters and return value assigned to the key
*
* Credits: http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values#answer-901144
**/
function getQueryParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
/**
* Piwik tracking within MediaWiki
*
* Will help tracking Edit and Save of a page
**/
function setupTracking() {
var _paq = window._paq || [],
campaign = getQueryParameterByName('pk_campaign');
if (campaign.length > 1 && campaign.match(/DocSprint/)) {
_paq.push(['setCustomVariable', 2, 'event', campaign, 'visit']);
}
if (typeof mw == 'object' && mw.user.isAnon() === false) {
_paq.push(['setCustomVariable', 1, 'username', mw.user.getName(), 'visit']);
}
jQuery('body').on('click', '#main-content #wpSave', function(){
var title = jQuery(this).attr('title') || null;
if (typeof title === 'string') {
_paq.push(['setCustomVariable', 1, 'label', title, 'page']);
}
_paq.push(['trackGoal', 2]); // idGoal=2 > Saves a page
});
jQuery('body').on('click', '.tool-area .toolbar .dropdown:first-child a', function(){
var title = jQuery(this).attr('title') || null;
if (typeof title === 'string') {
_paq.push(['setCustomVariable', 1, 'label', title, 'page']);
}
_paq.push(['trackGoal', 1]); // idGoal=1 > Clicks edit page
});
if (typeof window._paq !== 'object') {
window._paq = _paq;
}
if (typeof window.console === 'object') {
console.log('Tracking started');
}
}
var _paq = _paq || [];
/** ******************** all this will be moved elsewhere ******************** **/
function hasCookie(name){
var a = document.cookie.split('; '),
out = false;
for (var i = 0 ; i < a.length ; i += 1) {
if (a[i].match(new RegExp(name))) {
out = true;
}
}
return out;
}
// Thanks: http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values#answer-901144
function getQueryParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
// Should be moved somewhere else, but will do for now
if (getQueryParameterByName('pk_campaign').length > 1 && getQueryParameterByName('pk_campaign').match(/DocSprint/)) {
_paq.push(['setCustomVariable', 2, 'event', getQueryParameterByName('pk_campaign'), 'visit']);
}
// Note from renoirb: Should be moved somewhere else soon
jQuery(document).ready(function(){
jQuery('body').on('click', '.tool-area .toolbar .dropdown:first-child a', function(){
var title = jQuery(this).attr('title') || null;
if (typeof title === 'string') {
_paq.push(['setCustomVariable', 1, 'label', title, 'page']);
}
_paq.push(['trackGoal', 1]);
});
});
/** ********************** /all this will be moved elsewhere ********************** **/
_paq.push(["setCustomVariable",1,"username", mw.user.getName(), "visit"]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u=(("https:" == document.location.protocol) ? "https" : "http") + "://tracking.webplatform.org/"
_paq.push(['setTrackerUrl', u+'js/']);
_paq.push(['setSiteId', 1]);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript';
g.defer=true; g.async=true; g.src=u+'js/'; s.parentNode.insertBefore(g,s);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment