Skip to content

Instantly share code, notes, and snippets.

@imkevinxu
Last active November 20, 2024 13:00
Show Gist options
  • Select an option

  • Save imkevinxu/4393523 to your computer and use it in GitHub Desktop.

Select an option

Save imkevinxu/4393523 to your computer and use it in GitHub Desktop.

Revisions

  1. imkevinxu revised this gist Dec 28, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -21,9 +21,9 @@

    <h1>Time Spent on Page</h1>
    <small>Change tabs, switch windows, or leave the page and the active amount of time on this page is logged</small>
    <a href="https://gist.github.com/4393523"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub"></a>
    <a href="https://gist.github.com/4393523" target="_blank"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub"></a>

    <script src="timeSpentOnPage.js"></script>

    </body>
    </html>
    </html>
  2. imkevinxu revised this gist Dec 28, 2012. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -9,11 +9,11 @@
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    width: 400px;
    margin: 50px auto;
    color: #444;
    }

    h1 { margin-bottom: 0.2em; }
    li { margin: 1em 0; }
    small { color: #666; }
    h1 { font-weight: 400; }
    li { margin: 1em 0; font-weight:300; }

    </style>
    </head>
    @@ -24,5 +24,6 @@ <h1>Time Spent on Page</h1>
    <a href="https://gist.github.com/4393523"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub"></a>

    <script src="timeSpentOnPage.js"></script>

    </body>
    </html>
  3. imkevinxu revised this gist Dec 28, 2012. 1 changed file with 0 additions and 0 deletions.
    Binary file added thumbnail.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  4. imkevinxu revised this gist Dec 28, 2012. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    Javascript code that calculates how much active time is spent on a tab and takes into account changing tabs, switching windows, or leaving the page.

    Demo here [http://bl.ocks.org/d/4393523/](http://bl.ocks.org/d/4393523/)
  5. imkevinxu revised this gist Dec 28, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -21,6 +21,7 @@

    <h1>Time Spent on Page</h1>
    <small>Change tabs, switch windows, or leave the page and the active amount of time on this page is logged</small>
    <a href="https://gist.github.com/4393523"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub"></a>

    <script src="timeSpentOnPage.js"></script>
    </body>
  6. imkevinxu revised this gist Dec 28, 2012. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    <title></title>
    <title>Time Spent on Page</title>
    <style type="text/css">

    body {
    @@ -11,13 +11,16 @@
    margin: 50px auto;
    }

    li { line-height: 2em; }
    h1 { margin-bottom: 0.2em; }
    li { margin: 1em 0; }
    small { color: #666; }

    </style>
    </head>
    <body>

    <h1 id="title">Change Tabs</h1>
    <h1>Time Spent on Page</h1>
    <small>Change tabs, switch windows, or leave the page and the active amount of time on this page is logged</small>

    <script src="timeSpentOnPage.js"></script>
    </body>
  7. imkevinxu revised this gist Dec 28, 2012. 2 changed files with 43 additions and 16 deletions.
    24 changes: 24 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    <title></title>
    <style type="text/css">

    body {
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    width: 400px;
    margin: 50px auto;
    }

    li { line-height: 2em; }

    </style>
    </head>
    <body>

    <h1 id="title">Change Tabs</h1>

    <script src="timeSpentOnPage.js"></script>
    </body>
    </html>
    35 changes: 19 additions & 16 deletions timeSpentOnPage.js
    Original file line number Diff line number Diff line change
    @@ -25,40 +25,43 @@ this.new_d = new Date();
    // Calculates Time Spent on page upon switching windows

    setInterval((function() {
    var time_spent;
    if (document.hasFocus() === false) {
    _this.new_d = new Date();
    time_spent = Math.round((_this.new_d - _this.d) / 1000);
    if (time_spent > 1) {
    console.log("[Switched Window] " + time_spent + " seconds");
    }
    var time_spent = Math.round((_this.new_d - _this.d) / 1000);
    doSomething("Switched Window", time_spent);
    _this.d = new Date();
    }
    }), 200);

    // Calculates Time Spent on page upon leaving/closing page

    window.onunload = function() {
    var time_spent;
    _this.new_d = new Date();
    time_spent = Math.round((_this.new_d - _this.d) / 1000);
    if (time_spent > 1) {
    console.log("[Left Page] " + time_spent + " seconds");
    }
    var time_spent = Math.round((_this.new_d - _this.d) / 1000);
    doSomething("Left Page", time_spent);
    };

    // Calculates Time Spent on page upon unfocusing tab
    // http://davidwalsh.name/page-visibility

    document.addEventListener(visibilityChange, (function(e) {
    var time_spent;
    if (document[state] === 'visible') {
    return _this.d = new Date();
    _this.d = new Date();
    } else if (document[hidden]) {
    _this.new_d = new Date();
    time_spent = Math.round((_this.new_d - _this.d) / 1000);
    if (time_spent > 1) {
    console.log("[Changed Tab] " + time_spent + " seconds");
    }
    var time_spent = Math.round((_this.new_d - _this.d) / 1000);
    doSomething("Changed Tab", time_spent);
    }
    }), false);

    // Function that does something

    var doSomething = function(message, time_spent) {
    if (time_spent >= 1) {
    var text = "["+message+"] "+time_spent+" seconds"
    var item = document.createElement("li");
    item.innerHTML = text;
    document.body.appendChild(item);
    console.log(text);
    }
    }
  8. imkevinxu created this gist Dec 28, 2012.
    64 changes: 64 additions & 0 deletions timeSpentOnPage.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    var hidden, state, visibilityChange,
    _this = this;

    if (document.hidden != null) {
    hidden = "hidden";
    visibilityChange = "visibilitychange";
    state = "visibilityState";
    } else if (document.mozHidden != null) {
    hidden = "mozHidden";
    visibilityChange = "mozvisibilitychange";
    state = "mozVisibilityState";
    } else if (document.msHidden != null) {
    hidden = "msHidden";
    visibilityChange = "msvisibilitychange";
    state = "msVisibilityState";
    } else if (document.webkitHidden != null) {
    hidden = "webkitHidden";
    visibilityChange = "webkitvisibilitychange";
    state = "webkitVisibilityState";
    }

    this.d = new Date();
    this.new_d = new Date();

    // Calculates Time Spent on page upon switching windows

    setInterval((function() {
    var time_spent;
    if (document.hasFocus() === false) {
    _this.new_d = new Date();
    time_spent = Math.round((_this.new_d - _this.d) / 1000);
    if (time_spent > 1) {
    console.log("[Switched Window] " + time_spent + " seconds");
    }
    _this.d = new Date();
    }
    }), 200);

    // Calculates Time Spent on page upon leaving/closing page

    window.onunload = function() {
    var time_spent;
    _this.new_d = new Date();
    time_spent = Math.round((_this.new_d - _this.d) / 1000);
    if (time_spent > 1) {
    console.log("[Left Page] " + time_spent + " seconds");
    }
    };

    // Calculates Time Spent on page upon unfocusing tab
    // http://davidwalsh.name/page-visibility

    document.addEventListener(visibilityChange, (function(e) {
    var time_spent;
    if (document[state] === 'visible') {
    return _this.d = new Date();
    } else if (document[hidden]) {
    _this.new_d = new Date();
    time_spent = Math.round((_this.new_d - _this.d) / 1000);
    if (time_spent > 1) {
    console.log("[Changed Tab] " + time_spent + " seconds");
    }
    }
    }), false);