Config Firebase
First thing to do, it's include the firebase library on the site headers.
<script src="https://www.gstatic.com/firebasejs/4.2.0/firebase.js"></script>
Now, we need to initialize the SDK using the next code instructions:
var config = {
apiKey: "PUBLIC_API_KEY",
authDomain: "DOMAIN",
databaseURL: "DATABASE_URL",
storageBucket: "STORAGE_BUCKED",
messagingSenderId: "SENDER_ID"
};
firebase.initializeApp(config);
Config Realtime Analytics
RealTime Users Analytics
Test 1 Test 2
<script src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.6/Chart.bundle.min.js'></script>
<script src='/js/RealTimeAnalytics.js'></script>
<canvas id='realtimeAnalytics' width='500' height='300'></canvas>
function initRealtimeTracker(){
var body = $('body');
$('.tracked').each(function(i, item){
var $item = $(item),
events = $item.data('evts');
$item.on(events, trackEvent);
});
}
function trackEvent(evt) {
var $element = $(this),
eid = $element.attr('id');
if (eid){
var type = evt.type,
tracker = firebase.database().ref('/tracks/' + eid + '/' + type);
try {
tracker.transaction(function(track){
return track ? track + 1 : 1;
});
} catch (ex) {}
}
}
initRealtimeTracker();