rcmdnk's blog

Japanese ver.: ブログがコピーされた事をGoogle Analyticsでチェックする
Sams Teach Yourself Google Analytics in 10 Minutes

In the post Notice a copy event on your blog by email only by JavaScript, the copy events are sent to Mandrill to make an email notice.

In this post, I would introduce how to analyze such events in Google Analytics.

Sponsored Links

JavaScript Code

Here is a snippet to send a copy event to Google Analytics:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<head>
...
<script>
window.onload = function() {
  document.oncopy = function () {
    var selected = "";
    if (typeof window.getSelection != "undefined") {
        var sel = window.getSelection();
        if (sel.rangeCount) {
            var container = document.createElement("div");
            for (var i = 0, len = sel.rangeCount; i < len; ++i) {
                container.appendChild(sel.getRangeAt(i).cloneContents());
            }
            selected = container.innerHTML;
        }
    } else if (typeof document.selection != "undefined") {
        if (document.selection.type == "Text") {
            selected = document.selection.createRange().htmlText;
        }
    }
    if (!selected) return;
    ga('send', 'event', 'copy', location.href + ':' + document.title, selected);
  }
}
</script>
...
</head>

Place this code in your <head>...</head> as above.

In this code, it assumes Universal Analytics version (analytics.js).

The key point is usage of Analytics’ event tracking:

ga('send', 'event', 'copy', location.href + ':' + document.title, selected);

In this function, arguments mean:

  • 1st arg: Command. send to send information to Google Analytics.
  • 2nd arg: hitType. event to set Event Tracking.
  • 3rd arg: eventCategory. Category of the event (<150bytes).
  • 4th arg: eventAction. Action of the event (<150bytes).
  • 5th arg: eventLabel. Label of the event (<150bytes).
  • 6th arg: eventValue. Value of the event (>=0). It is omitted in this time.

If you are using old version (ga.js), replace:

ga('send', 'event', 'copy', location.href + ':' + document.title, selected);

with

_gaq.push(['_trackEvent', 'copy', location.href + ':' + document.title, selected]);

Check copy events in Google Analytics

You can find the results in BehaviorEvents.

behavior

In Overview, copy category is there.

eventcategory

Choose copy and click Event Action, there is a page list.

eventaction

If go to Event Label, you can find what were copied in your blog. If you choose any page in Event Action first, then go to Event Label, only copies of the selected page are shown.

eventlabel

In above snippet, copied region is sent as HTML. Therefore, if the large region is copied, you will see sentences with HTML tags.

eventlabel2

If you wish to have only text, manage select variable in above snippet like:

1
2
-selected = container.innerHTML;
+selected = container.innerText;

and

1
2
-selected = document.selection.createRange().htmlText;
+selected = document.selection.createRange().text;

At Event Category page, you can use Secondary dimension to divide events into each page or title, too.

secondarydimension

If you want to add more marks to divide into, you can use Event Action instead of the page url+title.

Sponsored Links
Sponsored Links

« Access to Mac from other machines Enable an event tracking of Google Website Translator with Universal Analytics »

}