Just out of curiosity, I wondered how often visitors of my blog copy text (especially code fragments) to their clipboard. Detecting copy/paste/cut events in jQuery turns out to be easy And thanks to Google Analytics event tracking API, tracking these events with GA is also easy. This resulted in the following mini jQuery plugin:

/*
 * jQuery Clipboard tracking plugin
 *
 */

(function($) {    
  $.fn.ga_track_clipboard = function() {    
      return this.each(function() {
          var text = $(this).text().substr(0, 10) + "...";
          jQuery(this).bind('cut copy paste', function(e) {  
          // The specification for the _trackEvent() method is:
          //_trackEvent(category, action, opt_label, opt_value)
          _gaq.push(['_trackEvent', 'Clipboard', e.type, text]);
        });
      });
  };  
})(jQuery);

(I will notice it if you copy the code above) 😉

Usage example:

jQuery(document).ready(function() {
  jQuery("p").ga_track_clipboard();
});

Note that this code uses the first 10 characters of copied text as the event label. I don’t think that this an ideal value for the label, but I haven’t come up with a better and generic idea yet.

See also:

5 Responses to “Tracking copy/paste events with jQuery and Google Analytics”

  1. Shaun says:

    Hi, JQuery is excellent. I assume you can modify this code to insert content into the copied data. For example insert the page URL at the bottom so that when the content is shared e.g. via email there would be a link at the foot of the content for them to click. The neat extra to that is that people copy and pasting into facebook would automatically get a link plus picture if markup is correctly set up on the target page.
    Shaun.

  2. Rob Kingston says:

    This is really cool, Gerwert – I like this idea.

    Recently, I’ve found jQuery holds a lot of potential for easily tracking visitor behaviours within the page.

    I would potentially make the following adjustments – capture more than just 10 chars (GA accepts much more through events – in fact I believe it’s only really limited by the length of the POST for __utm.gif).

    Also, I’d capture the text length in the form of the event value.

    Can’t wait to try this out though…

  3. Would be even better if you could show what the report looks like in GA.

    What does the output look like?

  1. […] copy/paste events with jQuery and Google Analytics jQuery Clipboard tracking plugin… [full post] Gerwert Onderweg Blog javascript / jquerysoftware developmentgoogle analyticsjquery […]

  2. […] annoyed me, so earlier this year I did some Googling and found an excellent jQuery solution on Onderweg. This script had everything I needed but it was missing a few […]

Leave a Reply





Human Verification