var startTime = getZoneBasedDateTime('+5.5'); var isOnIOS = navigator.userAgent.match(/iPad/i)|| navigator.userAgent.match(/iPhone/i); var eventName = isOnIOS ? "pagehide" : "beforeunload"; var userBrowserId = getUserBrowserId(); var trackingUrl = 'http://172.104.180.251/inddms/index.php/Tracking/trackPageView'; //When the user leaves the page(closes the window/tab, clicks a link)... window.addEventListener(eventName, function (event) { trackPageView(startTime); }); function trackPageView(startTime){ var xmlhttp; //Make a variable for a new ajax request. //If it's a decent browser... if (window.XMLHttpRequest){ // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest();//Open a new ajax request. } else { //If it's a bad browser... // code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //Open a different type of ajax call. } var endTime = getZoneBasedDateTime('+5.5'); //Send the time on the page to a php script of your choosing. var url = trackingUrl+"?actionStartTime="+startTime+"&actionEndTime="+endTime+"&pageUrl="+window.location.href+"&resolution="+screen.width+"x"+screen.height+"&refererUrl="+document.referrer+"&userBrowserId="+userBrowserId; //The false at the end tells ajax to use a synchronous call which wont be severed by the user leaving. xmlhttp.open("GET",url,false); xmlhttp.send(null); //Send the request and don't wait for a response. } function getUserBrowserId(){ var userBrowserId = readCookie('userBrowserId'); if(userBrowserId == null || userBrowserId == ''){ var rString = randomString(32, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); setCookie('userBrowserId',rString,90,'','digicliff.com'); userBrowserId = readCookie('userBrowserId'); } jQuery('.userBrowserId').val(userBrowserId); return userBrowserId; } function setCookie( name, value, expires, path, domain, secure ){ // set time, it's in milliseconds var today = new Date(); today.setTime( today.getTime() ); if(expires){ expires = expires * 1000 * 60 * 60 * 24; } var expires_date = new Date( today.getTime() + (expires) ); document.cookie = name + "=" +escape( value ) + ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + ( ( path ) ? ";path=" + path : "" ) + ( ( domain ) ? ";domain=" + domain : "" ) + ( ( secure ) ? ";secure" : "" ); } function readCookie(name){ var cookiename = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++){ var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(cookiename) == 0){ return c.substring(cookiename.length,c.length); } } return null; } function randomString(length, chars) { var result = ''; for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)]; return result; } function getZoneBasedDateTime(offset) { d = new Date(); utc = d.getTime() + (d.getTimezoneOffset() * 60000); myDate = new Date(utc + (3600000*offset)); var year = myDate.getFullYear(); var month = myDate.getMonth() + 1; month = month < 10 ? '0'+month : month; var date = myDate.getDate(); date = date < 10 ? '0'+date : date; var hour = myDate.getHours(); hour = hour < 10 ? '0'+hour : hour; var mins = myDate.getMinutes(); mins = mins < 10 ? '0'+mins : mins; var secs = myDate.getSeconds(); secs = secs < 10 ? '0'+secs : secs; return year + '-' +month + '-' + date + ' '+hour+ ':'+mins+ ':'+secs; }