<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Saleem</title>
    <description>The latest articles on DEV Community by Saleem (@saleemkce).</description>
    <link>https://dev.to/saleemkce</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F858931%2Fa5d31a76-2c26-4f18-816e-c8c6fdca4bd9.png</url>
      <title>DEV Community: Saleem</title>
      <link>https://dev.to/saleemkce</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/saleemkce"/>
    <language>en</language>
    <item>
      <title>Let's visualize time-on-site captured with timeonsite.js using free "Visual tool" -part 2</title>
      <dc:creator>Saleem</dc:creator>
      <pubDate>Thu, 08 Sep 2022 15:59:02 +0000</pubDate>
      <link>https://dev.to/saleemkce/lets-visualize-time-on-site-captured-with-timeonsitejs-using-free-visual-tool-part-2-511f</link>
      <guid>https://dev.to/saleemkce/lets-visualize-time-on-site-captured-with-timeonsitejs-using-free-visual-tool-part-2-511f</guid>
      <description>&lt;p&gt;It is the continuation of the &lt;a href="https://dev.to/saleemkce/lets-build-a-modern-time-on-site-counter-for-your-website-with-timeonsitejs-and-js-beacons-2hag"&gt;Let's build time-on-site counter for your website - Part 1&lt;/a&gt; we wrote few weeks back. In the last article, we learnt about how to build a time-on-site counter to show in webpage and utilize &lt;strong&gt;&lt;em&gt;timeonsite.js&lt;/em&gt;&lt;/strong&gt; tracker to capture it in a modern and hassle-free approach. We also used state-of-the-art &lt;strong&gt;&lt;em&gt;sendBeacon()&lt;/em&gt;&lt;/strong&gt; JS API to save the critical user engagement data on page close. This is good! For each pageview, we get following data dynamically captured by &lt;em&gt;timeonsite.js&lt;/em&gt; during page close&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
 TOSId: 1129620185532,
 TOSSessionKey: "14802525481391382263",
 TOSUserId: "anonymous",
 title: "Blog application — Nature &amp;amp; Wildlife",
 URL: "http://tos-localdata.london/home.php"
 entryTime: "2021–11–27 13:15:48.663",
 exitTime: "2021–11–27 13:17:31.663",
 timeOnPage: 103,
 timeOnSite: 103,
 timeOnPageTrackedBy: "second",
 timeOnPageByDuration: "0d 00h 01m 43s",
 timeOnSiteByDuration: "0d 00h 01m 43s",
 trackingType: "tos",
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This record is great. But, how do we visualize all these data with large number of users spanned across millions of records? Let's say we have 1000 pageviews captured yesterday and we have an overall time-on-site of &lt;strong&gt;&lt;em&gt;185,300&lt;/em&gt;&lt;/strong&gt; seconds or &lt;strong&gt;3088&lt;/strong&gt; minutes approximately as seen from "&lt;strong&gt;&lt;em&gt;tos&lt;/em&gt;&lt;/strong&gt;" table in the database. &lt;/p&gt;

&lt;h2&gt;
  
  
  "tos" table as in MariaDB/MySql DB
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--P8YnSuTZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4kqmnzt25mupjo9vvpiv.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P8YnSuTZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4kqmnzt25mupjo9vvpiv.PNG" alt="Tos table column structure" width="880" height="448"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OWbHqm7q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5pfbyj1sit9wowrnw1xl.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OWbHqm7q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5pfbyj1sit9wowrnw1xl.PNG" alt="Holistic view of tos session" width="880" height="216"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How many sessions occurred yesterday?&lt;/li&gt;
&lt;li&gt;Which is the largest session that was captured yesterday?&lt;/li&gt;
&lt;li&gt;Which is the smallest session or bounce-rate captured yesterday?&lt;/li&gt;
&lt;li&gt;Which page (home, about or contact) got more attention in terms of user stay time?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One way is to query the data like this from our popular MariaDB or MySql DB as follows,&lt;/p&gt;

&lt;h2&gt;
  
  
  Total page views occurred on 2022-09-01
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select count(tos_id) as Pageviews from tos WHERE entry_data like '%2022-09-01%'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Pageviews: 123&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;or to find a large session&lt;/p&gt;

&lt;h2&gt;
  
  
  Large session occurred in the website recently
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT 
  * 
FROM 
  tos 
WHERE 
  tos_session_key = (
    SELECT 
      tos_session_key 
    FROM 
      (
        SELECT 
          MAX(timeonsite) AS max_timeonsite, 
          tos_session_key 
        FROM 
          tos 
        GROUP BY 
          tos_session_key 
        ORDER BY 
          max_timeonsite DESC 
        LIMIT 
          1
      ) app_large_session
  ) 
ORDER BY 
  timeonsite DESC;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6B9IBhRr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0vhuzla25yvz80e33bms.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6B9IBhRr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0vhuzla25yvz80e33bms.PNG" alt="Single large session in tos table" width="880" height="38"&gt;&lt;/a&gt;&lt;br&gt;
As you can see, 197 seconds or 3 mins 17 seconds is the large session occurred in &lt;em&gt;tos&lt;/em&gt; table.&lt;/p&gt;

&lt;p&gt;But, this is all custom querying to find our desired reports. Also, this is part of &lt;a href="https://github.com/saleemkce/timeonsite_analytics"&gt;Timeonsite Analytics&lt;/a&gt;, an advanced querying technique to find insights and stats. We'll skip that for now. And let's take a look at the free tool "Visual" provided by timonsite.js for quick reporting and analytics view of the user engagement data.&lt;/p&gt;

&lt;p&gt;Let's open the free &lt;a href="https://github.com/saleemkce/visual"&gt;Visual&lt;/a&gt; backend server and download it to your computer. If you just scroll down there, there are instructions for PHP/NodeJs. Let's take PHP since it's part of most popular LAMP stack along with MariaDB.  I recommend you use &lt;strong&gt;&lt;em&gt;XAMPP/WAMP/MAMP&lt;/em&gt;&lt;/strong&gt; server if you are trying out this locally which provides you &lt;strong&gt;&lt;em&gt;/www&lt;/em&gt;&lt;/strong&gt; folder for quickly accessing PHP scripts. In our case, just download the visual into &lt;strong&gt;&lt;em&gt;/www&lt;/em&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;em&gt;/htdocs&lt;/em&gt;&lt;/strong&gt; folder and follow the 5-minute installation given in Visual's repository page.&lt;/p&gt;

&lt;p&gt;Now, you'll be able to access the Visual application at &lt;a href="http://localhost/visual/index.html"&gt;http://localhost/visual/index.html&lt;/a&gt;  If you are trying out in cloud server, replace the &lt;strong&gt;&lt;em&gt;localhost&lt;/em&gt;&lt;/strong&gt; with &lt;strong&gt;&lt;em&gt;your-website-com&lt;/em&gt;&lt;/strong&gt; in this URL.&lt;/p&gt;

&lt;p&gt;If you are going to use PHP, you can see that the &lt;em&gt;Visual&lt;/em&gt; app is using PHP as default in &lt;a href="https://github.com/saleemkce/visual/blob/master/config/config.js"&gt;this file&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, after uploading visual folder and making configurations properly like DB host, username &amp;amp; password, you will be able to see the reporting application. It'll be like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3bTwj0O9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zohuk77f7su7mlqogjcf.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3bTwj0O9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zohuk77f7su7mlqogjcf.PNG" alt="Reporting dashboard, timeonsite.js" width="880" height="411"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Key features to note (Data view)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It's built on usual JQuery DataTable&lt;/li&gt;
&lt;li&gt;There is a search bar at the bottom of each column&lt;/li&gt;
&lt;li&gt;You can copy paste any &lt;strong&gt;&lt;em&gt;tos_session_key&lt;/em&gt;&lt;/strong&gt; and put at the bottom of the page to understand how user came from page 1 to page 2 to page x to achieve "x" time-on-site data&lt;/li&gt;
&lt;li&gt;"&lt;strong&gt;&lt;em&gt;tos&lt;/em&gt;&lt;/strong&gt;" column should be in ascending order. In case it's not in ascending order. Click on "up/down symbol" in column header to sort.&lt;/li&gt;
&lt;li&gt;You can search by page titles alone&lt;/li&gt;
&lt;li&gt;You can apply date filter at the top to view data for specific date or month. Apply "from" and "to" dates properly&lt;/li&gt;
&lt;li&gt;Total records/pageviews can be seen at the bottom of the DataTable (Each record is a single pageview in website).&lt;/li&gt;
&lt;li&gt;"Sync data" button can be clicked to fetch recent or real-time data occurring in your website.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is a "Chart view" link at the top right side. Just click that to view analytics dashboard. It's high-level view of important stats of your website like time-spent duration, large sessions, recent sessions time duration etc. It's limited to recent 10k records. It is a configuration parameter in &lt;strong&gt;&lt;em&gt;Visual&lt;/em&gt;&lt;/strong&gt; app; you can customize it to large value if your browser and application support big load. Let's take a look at the analytics view of time-on-site data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zvbMuOPf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qgg1oz6lgbwcniderv25.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zvbMuOPf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qgg1oz6lgbwcniderv25.PNG" alt="Analytics dashboard, timeonsite.js" width="880" height="697"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's take a look at key stats from this chart view:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Recent 75 sesssions by time-on-site&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HhuNqmzw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rr1dr9yon72if9i5qfqg.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HhuNqmzw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rr1dr9yon72if9i5qfqg.PNG" alt="Recent 75 sesssions" width="880" height="191"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Page popularity table&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2T5C_2L2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aahqeecct74e3sg7tmf2.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2T5C_2L2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aahqeecct74e3sg7tmf2.PNG" alt="Large and short session for each page" width="570" height="137"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, we'll be able to see how much engaged users are in our web application with recent sessions by duration and popular pages in our site.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key features to note (Chart view)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It's built on popular charting library dc.js&lt;/li&gt;
&lt;li&gt;Total occurrences is nothing but page views&lt;/li&gt;
&lt;li&gt;You can also view anonymous vs authenticates users accessing the site.&lt;/li&gt;
&lt;li&gt;There is a date-picker at the top to control the pageview/user engagement data by recent week or month, quarter or year.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Play around with this "visual" tool to learn more about it. You can customize anything in it and as much as you can since it's free reporting tool written in PHP/NodeJs. So, this tool is great for small, medium or large websites, personal blogging sites, beginners and site administrators to understand user engagement.&lt;/p&gt;

&lt;p&gt;If you are looking for advanced reporting, try &lt;strong&gt;&lt;em&gt;Timeonsite Analytics&lt;/em&gt;&lt;/strong&gt;. Since you have user engagement data in table, you are not limited to SQL querying or Visual app reporting alone, the data can be visualized in any popular analytics tools like Tableau, Power BI, Qlik, Alteryx etc. You have got much more data projections, insights and stats there. I have a plan to write an article about time-on-site reporting on Tableau Public, free BI tool for you, later in the series.&lt;/p&gt;

&lt;p&gt;Key points to note:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This reporting looks similar to other analytics tool on the surface but it's fetching not only pageviews from tracked data but also user's exact stay time on each page thanks to timeonsite.js' data accuracy consistency!&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;pageviews + stay time =&amp;gt; "accurate user engagement"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;If you have integrated &lt;strong&gt;&lt;em&gt;timeonsite.js&lt;/em&gt;&lt;/strong&gt; in your website/app with "callback" or "real-time" option, then you get "user engagement" for bounce rate pages as well. This is highly exclusive here since you won't get "user engagement" for bounce-rated pages in any other analytic providers except with difficult ping/poll techniques that they employ. This is the first time we have captured like this with the help of timeonsite.js and underlying beacon APIs.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;timeonsite.js + callback option =&amp;gt; "accurate user engagement for bounce-rated pages as well"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Try it out and let us know if there are any integration issues. Post it in comments. There are a good deal of posts online to support you. If you liked this article, go ahead and give a &lt;a href="https://github.com/saleemkce/timeonsite"&gt;star on github&lt;/a&gt; repository.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>analytics</category>
      <category>webdev</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Let's build a modern time-on-site counter for your website with timeonsite.js and JS Beacons</title>
      <dc:creator>Saleem</dc:creator>
      <pubDate>Mon, 15 Aug 2022 12:45:49 +0000</pubDate>
      <link>https://dev.to/saleemkce/lets-build-a-modern-time-on-site-counter-for-your-website-with-timeonsitejs-and-js-beacons-2hag</link>
      <guid>https://dev.to/saleemkce/lets-build-a-modern-time-on-site-counter-for-your-website-with-timeonsitejs-and-js-beacons-2hag</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;Timeonsite.js&lt;/em&gt;&lt;/strong&gt; can be used to measure most important metric for you website &lt;em&gt;time-on-page&lt;/em&gt; and &lt;em&gt;time-on-site&lt;/em&gt;. Though it can be calculated simply by finding the difference between page entry and page exit with Javascript's Date object, here we are going to use a powerful web implementation of this with the help of widely popular timeonsite.js library along with the help of &lt;strong&gt;&lt;em&gt;sendBeacon()&lt;/em&gt;&lt;/strong&gt; API for safe data persistence to DB table.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic HTML block
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;!-- 
HTML page with "counter" div container 
--&amp;gt;
&amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&amp;lt;/head&amp;gt;
    &amp;lt;div id="counter"&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/html&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Javascript code snippet for building timer
&lt;/h2&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

// On page Load...
let entryTime = new Date();

//....
//User navigates the website
// some time later....

During Page exit....
let exitTime = new Date();

let timeSpent = exitTime - entryTime;
timeSpent = (timeSpent/1000); //convert to seconds

//Display the time spent with Jquery's API in page:
$(document).ready(function(){
    $('#counter').html('You have spent ' + timeSpent + 'seconds.');
});


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;That's it. We have measured user's time-on-page parameter nicely.  But what about its accuracy, cross-browser compatibility and data persistence when user refreshes the page many times or opens N number of browser tabs of same website? What if user has gone to visit another shopping or social media website leaving the former webpage in open state?&lt;/p&gt;

&lt;p&gt;Since &lt;strong&gt;&lt;em&gt;timeSpent&lt;/em&gt;&lt;/strong&gt; is a variable, it cannot control the states occurring in the webpage like inactive tab or multiple tabs opened concurrently for the same website. In this case, the accuracy of the &lt;strong&gt;&lt;em&gt;timeSpent&lt;/em&gt;&lt;/strong&gt; is lost and cannot be reliably measured.  Let's take a look at the APIs exposed by timeonsite.js tracker.&lt;/p&gt;

&lt;p&gt;But timeonsite tracker's &lt;em&gt;&lt;code&gt;Tos.getTimeOnPage()&lt;/code&gt;&lt;/em&gt; API exposes &lt;em&gt;&lt;code&gt;timeonpage data&lt;/code&gt;&lt;/em&gt; every time you call the API. And we are going to use this API to show and update our counter with timer data.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

{
 TOSId: 1129620185532,
 TOSSessionKey: "14802525481391382263",
 TOSUserId: "anonymous",
 title: "Blog application — Nature &amp;amp; Wildlife",
 URL: "http://tos-localdata.london/home.php"
 entryTime: "2021–11–27 13:15:48.663",
 exitTime: "2021–11–27 13:17:31.663",
 timeOnPage: 103,
 timeOnSite: 103,
 timeOnPageTrackedBy: "second",
 timeOnPageByDuration: "0d 00h 01m 43s",
 timeOnSiteByDuration: "0d 00h 01m 43s",
 trackingType: "tos",
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 1
&lt;/h2&gt;

&lt;p&gt;Add the tracker snippet in your HTML page header&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;head&amp;gt;
     &amp;lt;script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/timeonsite/1.2.1/timeonsitetracker.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 2
&lt;/h2&gt;

&lt;p&gt;Add the Init code of the tracker&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;script&amp;gt;
var config = {
    // track page by seconds. Default tracking is by milliseconds
    trackBy: 'seconds',
    callback: function(data) { /* callback denotes your data tracking is real-time */
        console.log(data);
        var endPointUrl = 'http://example.com' //Replace with your actual backend API URL http://localhost/tos
        if (data &amp;amp;&amp;amp; data.trackingType) {
            if (data.trackingType == 'tos') {
                if (Tos.verifyData(data) != 'valid') {
                    console.log('Data abolished!');
                    return; 
                }
            }

            // make use of sendBeacon if this API is supported by your browser.
            if (navigator &amp;amp;&amp;amp; typeof navigator.sendBeacon === 'function') {
                data.trasferredWith = 'sendBeacon';
                var blob = new Blob([JSON.stringify(data)], {type : 'application/json'});
                navigator.sendBeacon(endPointUrl, blob);
            }
        }
    }
};
var Tos;
if (TimeOnSiteTracker) {
    Tos = new TimeOnSiteTracker(config);
}
&amp;lt;/script&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 3
&lt;/h2&gt;

&lt;p&gt;Show the status of your time-on-page data in webpage as well&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

$(document).ready(function(){
    setInterval(function () {
        $('#counter').html('You have spent ' + Tos.getTimeOnPage().timeOnPage + 'seconds.');
    }, 1000); // Update the counter every second for real-time experience.
}); 


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;That's it. &lt;em&gt;Watch for tracker imprint in browser terminal!&lt;/em&gt; We are done with accurate tracking of user's time-on-site in our website. There is &lt;a href="https://saleemkce.github.io/timeonsite" rel="noopener noreferrer"&gt;a demo here &lt;/a&gt;if you want to see.&lt;/p&gt;

&lt;p&gt;You will see counter like this. Customize the look.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzumupm9amkriss03kjyr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzumupm9amkriss03kjyr.png" alt="TimeOnSite sample counter in page"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Timeonsite.js will take care of everything like,&lt;/p&gt;

&lt;p&gt;1, Inactive tabs&lt;br&gt;
2, Data persistence on page refresh and reloads&lt;br&gt;
3, Multiple tabs of same website (because in normal JS Date object, you will end up duplicate and wrong time-on-site data)&lt;br&gt;
4, Saving data to your favorite DB table on page close. (You may use already backend &lt;a href="https://github.com/saleemkce/visual" rel="noopener noreferrer"&gt;visual, free backend server&lt;/a&gt; for saving this data in PHP/NodeJS)&lt;br&gt;
5, Most importantly, unprecedented accuracy of the metrics ever captured by an analytics provider!&lt;/p&gt;

&lt;p&gt;So, you can show how much time your users spent on your site and also you can show how much time user spent in last session as log taken from DB table.&lt;/p&gt;

&lt;p&gt;In step 2, we used following lines,&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

navigator.sendBeacon(endPointUrl, blob);


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This is one of the most powerful &lt;strong&gt;&lt;em&gt;Javascript API&lt;/em&gt;&lt;/strong&gt; for making HTTP post requests like &lt;strong&gt;&lt;em&gt;XMLHTTPRequest&lt;/em&gt;&lt;/strong&gt; but it works especially for retaining and saving log data during page close.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F09ymi7o832smgdsv789y.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F09ymi7o832smgdsv789y.PNG" alt="Sample SendBeacon() Request on RequestBin site"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You'll see a ping in Network console unlike XHR/HTTP requests. I'm using a sample bin that causes CORS error. In normal app, it should send back success response.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus point
&lt;/h2&gt;

&lt;p&gt;Timeonsite.js also exposes a &lt;code&gt;Tos.startActivity()&lt;/code&gt; API along with &lt;code&gt;Tos.endActivity()&lt;/code&gt;. The advantage of this API is that it can track any specific activity in a site and it's bundled with same capabilities like time-on-page parameter like inactive tab, minimize tab or multiple tabs in measuring timing accuracy. Each pageview includes a &lt;code&gt;TOSSessionKey&lt;/code&gt; to uniquely identify each activity &lt;code&gt;ActivityId(TOSId)&lt;/code&gt; occurring in the site. Let's take an example,&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

// After initializing the TimeOnSiteTracker on page load, call startActivity() as
Tos.startActivity({applicantName: 'Nazir Khan'}); // this will start tracking the activity

// .
// .
// .
// after a while,

Tos.endActivity({applicantCountry: 'Romania'});
// calling .endActivity() will give back response

Gives response as,
{
    TOSId: 585872449448,
    TOSSessionKey: "14802525481391382263",
    TOSUserId: "anonymous",
    title: "Test application - TimeOnSiteTracker",
    URL: "http://tos-localdata.london/home.php",
    activityStart: "2021-11-27 13:20:46.707",
    activityEnd: "2021-11-27 13:20:50.213",
    timeTaken:4,
    timeTakenByDuration: "0d 00h 00m 04s"
    timeTakenTrackedBy: "second",
    trackingType: "activity",
    applicantName: "Nazir Khan",
    applicantCountry: "Romania"
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you liked this article, go ahead and give a &lt;a href="https://github.com/saleemkce/timeonsite" rel="noopener noreferrer"&gt;star on github&lt;/a&gt; repository. And share your comments on how it's like to build your own time-on-site counter for your webpage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Notes&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
This timeonsite.js is commercial library but it's also free for non-production sites and personal websites. So, let's use the free version hosted from CDN.js hosting website.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
