I came up with a neat idea the other night. Using localStorage and sessionStorage you could theoretically monitor the number of tabs or windows a visitor has opened for your site. As far as I know, this capability has never before been possible. Well, now it is.
Check out this crude example.
Once you open the monitor, leave that tab/window put. Use the “Spawn Tab” link to create new tabs and windows. The monitor will be notified and display some simple debug. There are 10 second updates per tab/window so that when they close the monitor can detect it. The monitor will detect a close within 15 seconds of the tab/window closing and will display the total time the tab/window plus or minus 10 seconds. Correct values are maintained as the tab/windows browse across pages as long as they stay on the domain! Just about everything you’d want or need.
Again, I mentioned this is rather crude. The fact that the monitor tab remains open is only due to the fact that I wanted to prototype the idea. The majority of the state is stored in localStorage, and each tab/window maintains a single identifier in its sessionStorage to remind the tab/window what id it was while it navigates to multiple pages. Because everything is stored in the storage this system has the capability to become completely distributed. Meaning no “monitor” tab is necessary, and the scripts can determine, and monitor, on their own the existence of all other tabs. Thus, this would be a viable option for the next level of web analytics.
As cool as this is, I don’t think it will provide too much value to the analytics. For the first time webmasters will be able to know how many windows or tabs a visitor opens (and to what pages they open). The webmaster will know more about how its user’s use the website, but I don’t think this statistic will be a game changer. Who knows!
So, how does it work? Very simple. Each tab includes the client.js code to handle updating the localStorage and maintaining its own “tab_id” in sessionStorage. Data it maintains can be whatever you want, I went with some simple information such as its start time, current url, and latest keepalive:
The upkeep for a Tab Client is to restore their session information when you navigate to any new page:
// Create or Restore tab_id var myTabId = sessionStorage tab; if myTabId === undefined var tabs = localStorage tabs; if tabs === undefined myTabId = 0; localStorage tabs = "0"; else var largest = parseInt tabssplit/,/pop 10 ; myTabId = largest+1; localStorage tabs += "," + myTabId; sessionStorage tab = myTabId;
And to perform its keepalives:
// Update the Latest Timestamp var key = 'tab'+myTabId+'_latest'; localStorage setItem key +; // Update Status every 10 seconds windowsetIntervalsetLatest10000; setLatest;
I put a few more convenience functions in there to help it update these localStorage keys, and communicate with the monitor which was crudely done through localStorage. That is explained next.
The Tab Monitor as it stands right now receives messages through localStorage’s “storage” event. It also checks all the tab’s “lastest” keepalives to make sure they didn’t pass their 10 second limit. In the case of a tab being closed, it will remove references to that tab and output an approximation of the time the tab was open:
// Listener - receive messages from tabs windowaddEventListener'storage' if e storageArea === localStorage if e key == "tab_msg" console.log e newValue ; addMsg e newValue ; // Appends to the page ; // Purger - clean out tabs that died for 15 seconds windowsetInterval console.log"purging"; var now = + tabs = localStorage tabs; if tabs !== undefined var toRemove = toKeep = ; tabs = tabssplit/,/; for var i=0 len=tabslength; i<len; i++ var tabId = tabs i tabLatest = parseInt localStorage getItem"tab"+tabId+"_latest" ; now-tabLatest>=15000 ? toRemove : toKeep pushtabId; localStorage tabs = toKeepjoin','; for var i=0 len=toRemovelength; i<len; i++ var tabId = toRemove i tabLatest = parseInt localStorage getItem"tab"+tabId+"_latest" tabStart = parseInt localStorage getItem"tab"+tabId+"_start" time = tabLatest-tabStart /1000; addMsg "Tab " + tabId + " Closed after " + time + " seconds!" ; 5000;
This took a little under an hour to get working. There are still minor issues that I didn’t attempt to resolve. However, if there is interest this could be developed into a completely distributed peer-to-peer communication between tabs/windows on a single domain. However, a little warning. Web Storage is not set in stone. Not all browsers have implemented it and the specification is subject to change at any minute. There has been some rather heated debate on the subject of Web Storage recently, with good reason. All I know is that when its settled, this functionality will continue to exist!
Let me know what you think.