Adapt analytics utility code to log print, copy, paste
Created by: khawkins98
It would be useful to know how often users print, copy, paste on a page.
Print and paste information would only work for desktop keyboard commands (not mobile or when a menu has been used), even so some indication would still be helpful.
$(document).ready(function() {
var ctrlDown = false,
ctrlKey = 17,
cmdKey = 91,
vKey = 86,
cKey = 67;
$(document).keydown(function(e) {
if (e.keyCode == ctrlKey || e.keyCode == cmdKey) ctrlDown = true;
}).keyup(function(e) {
if (e.keyCode == ctrlKey || e.keyCode == cmdKey) ctrlDown = false;
});
$(".trackable-area").keydown(function(e) {
if (ctrlDown && (e.keyCode == vKey || e.keyCode == cKey)) {
// doLogging();
}
});
});
and/or
$(document).bind('copy', function(e) {
// doLogging();
});
$(document).bind('paste', function() {
// doLogging();
});
$(document).bind('cut', function() {
// doLogging();
});
$(document).bind('contextmenu', function(e) {
// doLogging();
});
For copy we can get a broader idea by tracking text selection:
Via https://www.savio.no/analytics/track-if-visitors-copy-your-text-in-google-analytics
function getSelected() {
if (window.getSelection) {
return window.getSelection();
} else if (document.getSelection) {
return document.getSelection();
} else {
var selection = document.selection && document.selection.createRange();
if (selection.text) {
return selection.text;
}
return false;
}
return false;
}
$(document).ready(function() {
$('body').on('copy cut paste', function(ccp) { // Track cut, copy or paste with jQuery.
var selection = getSelected();
var maxLength = 150; // Up to 150 Characters from your text will be tracked. Change this number if you want to track more or less text.
if (selection && (selection = new String(selection).replace(/^\s+|\s+$/g, ''))) {
var textLength = selection.length; // How many characters was copied/cutted/pasted.
if (selection.length > maxLength) {
selection = selection.substr(0, maxLength) + "..."
} // If the text is longer than maxLength, add ... to the end of the text
else {
selection = selection; // If the text is shorter than maxLength, just track the text as it is.
}
_gaq.push(['_trackEvent', 'Clipboard', ccp.type, selection, textLength]);
// Track copied/cutted/pasted data in Google Analytics as Events
}
});
});