/** * Get the details about the page and add them to the Metrics Object * * @author Derrick Antaya */ function BN_Metrics() { this.api_data = { key: 'sDHRhYNcAY76jxfUf8TSMaMu8a10WkQaw6DJ4Vi3kboimcYAGX' }; this.page_data = { page_type: this.get_meta('bn:page_type'), post_id: this.get_meta('bn:post_id'), post_title: this.get_meta('bn:post_title'), post_name: this.get_meta('bn:post_name'), post_date: this.get_meta('bn:post_date'), post_modified: this.get_meta('bn:post_modified'), post_type: this.get_meta('bn:post_type'), post_category: this.get_meta('bn:post_category'), post_template: this.get_meta('bn:post_template'), page_url: window.location.href }; this.visitor_data = { referrer: this.get_referrer(), view_port: this.check_view_port(), user_data: this.user_data('bnUserInfo'), client_id: this.client_id('TPCI'), }; }; BN_Metrics.prototype = { construct: BN_Metrics, track: function (action, selector) { // track various types of behavior // set defaults var action = (action == null) ? null : action; var selector = (selector == null) ? null : selector; switch (action) { case 'view': var data = { type: 'view', api: this.api_data, page: this.page_data, visitor: this.visitor_data }; this.send(data); break; case 'click': document.getElementById(selector).onclick = function(e) { var data = { type: 'click' }; bn_metrics.send(data); }; break; default: return; } }, send: function (data) { var data = (data == null) ? null : data; var xapi = new XMLHttpRequest(); xapi.onreadystatechange = function() { if (xapi.readyState == XMLHttpRequest.DONE) { if (xapi.status == 400) { // @TODO: figure out what to do in error state } else { var response = JSON.parse(xapi.response); // handle the responses if (typeof response.pw != 'undefined') { // handle paywall bn_metrics.pw(response.pw); } } } }; xapi.open('POST', 'https://bnapi.buffalonews.com/metrics/api/v1/track/1y9WbPk5fHPLhStc2nrD10OT8kK7KWn0', true); xapi.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xapi.send('data=' + encodeURIComponent(JSON.stringify(data))); }, get_meta: function (name) { // get attribute from meta tag by name var $meta = document.querySelector("meta[name='" + name + "']"); // return empty string if the meta doesnt exist if ($meta == null) { return ''; } return $meta.getAttribute('content'); }, client_id: function (name) { // get or make a client id var client_id = this.get_cookie(name); if (!client_id) { client_id = this.generate_id(64); this.set_cookie(name, client_id); } return client_id; }, user_data: function (name) { // get user data var Base64 = {_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(e){var t="";var n,r,i,s,o,u,a;var f=0;e=Base64._utf8_encode(e);while(f>2;o=(n&3)<<4|r>>4;u=(r&15)<<2|i>>6;a=i&63;if(isNaN(r)){u=a=64}else if(isNaN(i)){a=64}t=t+this._keyStr.charAt(s)+this._keyStr.charAt(o)+this._keyStr.charAt(u)+this._keyStr.charAt(a)}return t},decode:function(e){var t="";var n,r,i;var s,o,u,a;var f=0;e=e.replace(/[^A-Za-z0-9+/=]/g,"");while(f>4;r=(o&15)<<4|u>>2;i=(u&3)<<6|a;t=t+String.fromCharCode(n);if(u!=64){t=t+String.fromCharCode(r)}if(a!=64){t=t+String.fromCharCode(i)}}t=Base64._utf8_decode(t);return t},_utf8_encode:function(e){e=e.replace(/rn/g,"n");var t="";for(var n=0;n127&&r<2048){t+=String.fromCharCode(r>>6|192);t+=String.fromCharCode(r&63|128)}else{t+=String.fromCharCode(r>>12|224);t+=String.fromCharCode(r>>6&63|128);t+=String.fromCharCode(r&63|128)}}return t},_utf8_decode:function(e){var t="";var n=0;var r=c1=c2=0;while(n191&&r<224){c2=e.charCodeAt(n+1);t+=String.fromCharCode((r&31)<<6|c2&63);n+=2}else{c2=e.charCodeAt(n+1);c3=e.charCodeAt(n+2);t+=String.fromCharCode((r&15)<<12|(c2&63)<<6|c3&63);n+=3}}return t}} var userInfo = this.get_cookie(name); if (!userInfo) { return null; } return Base64.decode(decodeURIComponent(userInfo)); }, get_referrer: function () { // get the pages referrer if (document.referrer) { var url = document.referrer; //var ref = url.match(/^http([s]?):\/\/([a-zA-Z0-9-_\.]+)(:[0-9]+)?/)[0]; var ref = url.match(/^(http([s]?)|android-app):\/\/([a-zA-Z0-9-_\.]+)(:[0-9]+)?/)[0]; return ref + '/'; } return ''; }, check_view_port: function () { // check screen size var width = window.innerWidth; if (width <= 767) { return 'mobile website'; } else if (width >= 768 && width < 992) { return 'tablet-v website'; } else if (width >= 992 && width < 1200) { return 'tablet-h website'; } else { return 'desktop website'; } }, generate_id: function (length) { // build a client id string var string = ''; var randomchar = function () { var n = Math.floor(Math.random() * 62); if (n < 10) { return n; //1-10 } if (n < 36) { return String.fromCharCode(n + 55); //A-Z } return String.fromCharCode(n + 61); //a-z } while (string.length < length) { string += randomchar(); } return string; }, set_cookie: function (name, value, domain) { // set cookies var date = new Date(); date.setTime(date.getTime() + (10000 * 24 * 60 * 60 * 1000)); var expires = "expires=" + date.toUTCString(); document.cookie = name + "=" + value + "; " + expires + ";domain=" + domain; }, get_cookie: function (name) { // get cookies var name = name + "="; var cookies = document.cookie.split(';'); for(var i = 0; i < cookies.length; i++) { var cookie = cookies[i]; while (cookie.charAt(0) == ' ') { cookie = cookie.substring(1); } if (cookie.indexOf(name) == 0) { return cookie.substring(name.length, cookie.length); } } return null; }, pw: function (pw) { // handle the setting and redirecting of the paywall // redirect if the criteria are met or just return if on the limit reached page or a non counted page if ( window.location.href !== pw.redirect_url && pw.count_page === 1 && pw.exception === 0 && pw.remaining_views < 0 ) { window.location.replace(pw.redirect_url); return true; } else if ( window.location.href === pw.redirect_url || pw.footer.html === '' ) { return true; } // add the paywall footer css to the page // avoid duplicates for(var i = 0; i < document.styleSheets.length; i++){ if(document.styleSheets[i].href == pw.footer.css){ return true; } } var head = document.getElementsByTagName('head')[0]; var link = document.createElement('link'); link.rel = 'stylesheet'; link.type = 'text/css'; link.href = pw.footer.css; head.appendChild(link); // add paywall footer to the page var body = document.getElementsByTagName('body')[0]; var div = document.createElement('div'); div.setAttribute('id', 'paywall-footer'); div.innerHTML = pw.footer.html; body.appendChild(div.firstChild); div = null; } } var bn_metrics = new BN_Metrics();