Skip to content

Commit

Permalink
Merge d6377f4 into 4652e07
Browse files Browse the repository at this point in the history
  • Loading branch information
milafrerichs committed Aug 26, 2015
2 parents 4652e07 + d6377f4 commit 8a68cc0
Show file tree
Hide file tree
Showing 8 changed files with 4,415 additions and 100 deletions.
3 changes: 3 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ module.exports = function(config) {
'public/javascripts/vendor/xhr.min.js',
'public/javascripts/vendor/time.min.js',
'public/javascripts/vendor/timeFormat.min.js',
'public/javascripts/vendor/handlebars-v3.0.3.js',
'public/javascripts/vendor/raphael-min.js',
'public/javascripts/vendor/morris.min.js',
'public/javascripts/helpers/helper.js',
'public/javascripts/helper/templateHelper.js',
'public/javascripts/landing-pages.js',
'public/javascripts/traffic.js',
'tests/**/*Spec.js',
'tests/fixtures/**/*'
Expand Down
3 changes: 3 additions & 0 deletions karma.saucelabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ module.exports = function(config) {
'public/javascripts/vendor/xhr.min.js',
'public/javascripts/vendor/time.min.js',
'public/javascripts/vendor/timeFormat.min.js',
'public/javascripts/vendor/handlebars-v3.0.3.js',
'public/javascripts/vendor/raphael-min.js',
'public/javascripts/vendor/morris.min.js',
'public/javascripts/helpers/helper.js',
'public/javascripts/helper/templateHelper.js',
'public/javascripts/landing-pages.js',
'public/javascripts/traffic.js',
'tests/**/*Spec.js',
'tests/fixtures/**/*'
Expand Down
8 changes: 5 additions & 3 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ <h2 class="brick-heading">Most popular pages</h2>
<script src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.4/raphael-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>
<script src="javascripts/vendor/mustache.js"></script>
<script src="javascripts/vendor/handlebars-v3.0.3.js"></script>
<script src="javascripts/vendor/pretty.js"></script>
<script src="javascripts/vendor/bezier.js"></script>

<script src="javascripts/helper/templateHelper.js"></script>
<script src="javascripts/helpers/helper.js"></script>
<script src="javascripts/landing-pages.js"></script>
<script src="javascripts/search.js"></script>
Expand Down Expand Up @@ -118,19 +120,19 @@ <h2 class="brick-heading">Most popular pages</h2>
</li>
</script>

<script type="template" id="landing-pages-item">
<script type="template" id="landing-pages-items">
{{#each pages}}
<li>
<div>
{{#has_url}}<a href="http://{{ url }}" target="_blank">{{/has_url}}
{{ term }}
{{#has_url}}</a>{{/has_url}}

{{#has_source}}
<em>via: {{ source }}</em>
{{/has_source}}
</div>
</li>
{{/each}}
</script>

</body>
</html>
22 changes: 22 additions & 0 deletions public/javascripts/helper/templateHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
window.templateHelper = {
templates: [],
compileTemplate: function(sourceHtml) {
return Handlebars.compile(sourceHtml);
},
getTemplate: function(id) {
var templ = this.templates[id];
if(!templ) {
this.templates[id] = templ = this.compileTemplate(document.getElementById(id).innerHTML);
}
return templ;
},
renderTemplate: function(element, id, context) {
element.innerHTML = this.getTemplate(id)(context);
},
prependTemplate: function(element, id, context) {
element.innerHTML = this.getTemplate(id)(context) + element.innerHTML;
},
appendTemplate: function(element, id, context) {
element.innerHTML = element.innerHTML + this.getTemplate(id)(context);
}
}
137 changes: 40 additions & 97 deletions public/javascripts/landing-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,14 @@

var landing = {
terms: [],
urls: [],
newTerms: [],
newURLs: [],
newSources: [],
el: false,
nextRefresh: 0,
limit: 10,

endpoint: function(profileId){
endpoint: function(){
return "/realtime?"
+ "ids=ga:"+matrix.settings.profileId+"&"
+ "metrics=ga:activeVisitors&"
+ "dimensions=ga:pageTitle,ga:pagePath,rt:source&"
/*+ "filters="+ encodeURIComponent("ga:pagePath==/search") +"&"*/
+ "sort=-ga:activeVisitors&"
+ "metrics=rt:pageViews&"
+ "dimensions=ga:pageTitle,ga:pagePath,rt:source,rt:minutesAgo,rt:deviceCategory&"
+ "sort=rt:minutesAgo&"
+ "max-results=10000";
},
safeTerm: function(term){
Expand All @@ -42,107 +35,57 @@
return true;
},
addTerm: function(term, count, url, source){

var i, _i;
for(i=0, _i=landing.terms.length; i<_i; i++){
if(landing.terms[i].term === term){
landing.terms[i].nextTick = count;
return true;
}
}
landing.terms.push({
term: term,
total: 0,
nextTick: count,
currentTick: 0,
url: url,
source: source
});
},
zeroNextTicks: function(){
var i, _i, newTerms = [];
for(i=0, _i=landing.terms.length; i<_i; i++){
landing.terms[i].nextTick = 0;
var i;
for(i=0;i<count;i++) {
landing.terms.push({
term: term,
total: count,
url: url,
source: source,
has_url: !!url,
has_source: !!source && (source != "(not set)")
});
}
},
addNextTickValues: function(data){

var i, _i, term, url, source;
parseData: function(data) {
var i, _i, term, url, source, minutesAgo,
termColumn = 0, urlColumn = 1, sourceColumn = 2,
minutesAgoColumn = 3, countColumn = 5, maxMinutes = 2;
for(i=0,_i=data.rows.length; i<_i; i++){
term = data.rows[i][0].split(' - ');
url = data.rows[i][1];
source = data.rows[i][2];
if(term[0] !== 'Search' && landing.safeTerm(term[0])){
landing.addTerm(term[0], root.parseInt(data.rows[i][3], 10), url, source);
}

}
},
addTimeIndexValues: function(){
var i, _i, j, _j, term, time, newPeople, nonZeroTerms = [];
for(i=0, _i=landing.terms.length; i<_i; i++){
term = landing.terms[i];
newPeople = term.currentTick < term.nextTick ? term.nextTick - term.currentTick : 0;
term.total = term.total + newPeople;
term.currentTick = term.nextTick;
if(newPeople > 0){
for(j=0,_j=newPeople; j<_j; j++){
landing.newTerms.push(term.term);
landing.newURLs.push(term.url);
landing.newSources.push(term.source);
term = data.rows[i][termColumn];
url = data.rows[i][urlColumn];
source = data.rows[i][sourceColumn];
minutesAgo = root.parseInt(data.rows[i][minutesAgoColumn]);
if(minutesAgo < maxMinutes) {
if(term !== 'Search' && landing.safeTerm(term)){
landing.addTerm(term, root.parseInt(data.rows[i][countColumn], 10), url, source);
}
}
if(term.currentTick > 0){
nonZeroTerms.push(term);
}else {
break;
}
}
//landing.newTerms.sort(function(){ return Math.floor((Math.random() * 3) - 1) });
landing.terms = nonZeroTerms;
},
parseResponse: function(data){
var term, i, _i;

landing.zeroNextTicks();
landing.addNextTickValues(data);
landing.addTimeIndexValues();
},
displayResults: function(){
var term = landing.newTerms.pop();
var url = landing.newURLs.pop();
var source = landing.newSources.pop();
var el = landing.el;
if(term){
var context = {
term: term,
has_url: !!url,
url: url,
has_source: !!source && (source != "(not set)"),
source: source
}

var tempList = el.ol().template("landing-pages-item", context);
root.matrix.manager.animateInto(tempList.firstElementChild, el,
landing.limit);
setTimeout(landing.displayResults,
(landing.nextRefresh - Date.now())/landing.newTerms.length);
} else {
setTimeout(landing.displayResults, 5000);
parseResponse: function(error, data){
if(error) { return -1; }
if(data.hasOwnProperty('rows')) {
landing.terms = [];
landing.parseData(data);
landing.refreshResults();
}else {
return -1;
}
},
refreshResults: function() {
templateHelper.prependTemplate(landing.el, "landing-pages-items", {pages: landing.terms });
},
init: function(){
var source;
landing.el = document.getElementById('search');
landing.reload();
landing.displayResults();
window.setInterval(landing.reload, 60e3);
document.addEventListener(
"visibilitychange",
function() { root.matrix.manager.positionFixups(landing); },
false);
},
reload: function(){
var endpoint = landing.endpoint(root.matrix.settings.profileId);
landing.nextRefresh = Date.now() + 60e3;
xhr.json(endpoint, landing.parseResponse);
xhr.json(landing.endpoint(), landing.parseResponse);
}
};

Expand Down

0 comments on commit 8a68cc0

Please sign in to comment.