Skip to content

Instantly share code, notes, and snippets.

@acoyfellow
Created April 21, 2015 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acoyfellow/b7fc4672b0c4cefed338 to your computer and use it in GitHub Desktop.
Save acoyfellow/b7fc4672b0c4cefed338 to your computer and use it in GitHub Desktop.
SendGrowth's Realtime Counter
'use strict';
(function() {
/*
1. Depending on how this file is called, it does a different thing.
2. "auth"=url
3. all/single page option
4. total/active option
<script page="all" cat="total" label="You are visitor number:" href="" src="//ex.io/js.js"></script>
API:
- 'page' : required
- 'cat' : required
- 'label' : optional
- 'href' : optional
*/
//include firebase+react
var Firebase = require('firebase'),
Database = new Firebase('https://sendgrowth-counter.firebaseio.com/'),
pageURL = document.URL.split('.').join('_'),
pageURL = pageURL.split('/').join('*'),
domain = window.location.host,
urlRef = Database.child(pageURL),
rootRef = Database.child(domain.split('.').join('_'));
// pagePref, catPref, textPref
var pagePref = null,
catPref = null,
labelPref = null,
hrefPref = null,
scripts = document.getElementsByTagName('script');
for(var i = 0, l = scripts.length; i < l; i++){
if(scripts[i].src === 'https://www.sendgrowth.com/assets/js/counter.js'){
pagePref = scripts[i].getAttribute('page');
catPref = scripts[i].getAttribute('cat');
labelPref = scripts[i].getAttribute('label');
hrefPref = scripts[i].getAttribute('href');
break;
};
};
console.log(pagePref, catPref, labelPref);
if(pagePref===null||catPref===null||labelPref===null){
console.log('SETTINGS NOT FOUND!');
return false;
};
// console.log(URL.split('.').join('_'));
rootRef.transaction(function(currentData) {
if (currentData === null) {
var obj = { active: 1, total: 1 };
obj[pageURL] = {active: 1, total: 1};
return obj;
} else {
console.log('Root already exists.',currentData, currentData[pageURL], currentData[pageURL].active);
if (currentData[pageURL] === null){
var obj = { active: currentData.active+1, total: currentData.total+1 };
obj[pageURL] = {active: 1, total: 1};
return obj
}else{
var obj = { active: currentData.active+1, total: currentData.total+1 };
obj[pageURL] = {active: currentData[pageURL].active+1, total: currentData[pageURL].total+1};
return obj
};
}
}, function(error, committed, snapshot) {
if (error) {
console.log('Transaction failed abnormally!', error);
} else if (!committed) {
console.log('We aborted the transaction (because wilma already exists).');
} else {
console.log('User wilma added!');
}
console.log("Data: ", snapshot.val());
//
//4 choices: if page=all&&cat=total, page=all&&cat=live, page=all&&cat=total, page=all&&cat=live,
var choice = null;
if(pagePref==='all'&&catPref==='total'){ choice = snapshot.val().total };
if(pagePref==='all'&&catPref==='active'){ choice = snapshot.val().active };
if(pagePref==='one'&&catPref==='total'){ choice = snapshot.val()[pageURL].total };
if(pagePref==='one'&&catPref==='active'){ choice = snapshot.val()[pageURL].active };
var element = document.createElement('div');
element.style.cssText = 'position: fixed;bottom: 0;left: 0;width: 200px;min-height: 40px;margin: 0;padding: 10px;z-index: 99999;background: lightblue; text-align:center;';
element.innerHTML = labelPref+'<br><b>'+choice+'</b>';
document.body.appendChild(element);
rootRef.on('value', function(dataSnapshot) {
if(pagePref==='all'&&catPref==='total'){ choice = dataSnapshot.val().total };
if(pagePref==='all'&&catPref==='active'){ choice = dataSnapshot.val().active };
if(pagePref==='one'&&catPref==='total'){ choice = dataSnapshot.val()[pageURL].total };
if(pagePref==='one'&&catPref==='active'){ choice = dataSnapshot.val()[pageURL].active };
element.innerHTML = labelPref+'<br><b>'+choice+'</b>';
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment