Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt analytics utility code to log print, copy, paste #118

Open
khawkins98 opened this issue Nov 1, 2017 · 0 comments
Open

Adapt analytics utility code to log print, copy, paste #118

khawkins98 opened this issue Nov 1, 2017 · 0 comments
Milestone

Comments

@khawkins98
Copy link
Contributor

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.

Via https://stackoverflow.com/questions/2903991/how-to-detect-ctrlv-ctrlc-using-javascript/2904944#2904944

$(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
    }
  });
});
@khawkins98 khawkins98 added this to the 1.3 milestone Nov 1, 2017
@khawkins98 khawkins98 modified the milestones: 1.3, v1.4 Apr 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant