Skip to content

Instantly share code, notes, and snippets.

@chrisblakley
Last active August 29, 2015 14:07
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 chrisblakley/5084dc14524cddbba130 to your computer and use it in GitHub Desktop.
Save chrisblakley/5084dc14524cddbba130 to your computer and use it in GitHub Desktop.
Detect copied text using Google Analytics
jQuery(document).on('cut copy', function(){
var currentPage = jQuery(document).attr('title');
var words = [];
var selection = window.getSelection() + '';
words = selection.split(' ');
wordsLength = words.length;
if (words.length > 8) {
words = words.slice(0, 8).join(' ');
ga('send', 'event', 'Copied Text', currentPage, words + '... [' + wordsLength + ' words]');
} else {
if ( selection == '' || selection == ' ' ) {
ga('send', 'event', 'Copied Text', currentPage, '[0 words]');
} else {
ga('send', 'event', 'Copied Text', currentPage, selection);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment