Skip to content

Instantly share code, notes, and snippets.

@gabrielhpugliese
Created November 8, 2013 14:21
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save gabrielhpugliese/7371688 to your computer and use it in GitHub Desktop.
Save gabrielhpugliese/7371688 to your computer and use it in GitHub Desktop.
Iron-router for http://coderstv.com with tracking - something may be missing because it's coderstv internals
Router.map(function () {
this.route('index', {
controller: 'BasicController',
layoutTemplate: 'indexLayout',
path: '/',
waitOn: function () {
return Meteor.subscribe('Channels');
}
});
this.route('coder', {
controller: 'ChannelController',
template: 'channel',
path: '/coder/:coderId'
});
this.route('codersList', {
controller: 'ChannelController',
template: 'channels',
path: '/coders'
});
this.route('video', {
controller: 'ChannelController',
template: 'channel',
path: '/video/:coderId/:videoId'
});
this.route('blankSearch', {
controller: 'SearchController',
path: '/search'
});
this.route('search', {
controller: 'SearchController',
path: '/search/:keyword'
});
this.route('language', {
controller: 'LanguageController',
path: '/language/:language'
});
this.route('languagesList', {
controller: 'LanguageController',
template: 'languages',
path: '/languages'
});
this.route('dashboard', {
controller: 'LoggedUserController',
before: function () {
if (! Roles.userIsInRole(Meteor.user(), 'beta') &&
! Roles.userIsInRole(Meteor.user(), 'admin')) {
this.render('not_allowed');
this.stop();
return;
}
},
waitOn: function () {
return [
Meteor.subscribe('Languages'),
Meteor.subscribe('Users'),
Meteor.subscribe('Channels')
];
}
});
this.route('preferences', {
controller: 'LoggedUserController',
waitOn: function () {
return Meteor.subscribe('Followers');
}
});
this.route('login', {
controller: 'BasicController',
before: function () {
if (Meteor.user()) {
this.redirect(Router.routes.index.path());
this.stop();
return;
}
}
});
this.route('privacy', {
controller: 'BasicController'
});
this.route('notFound', {
controller: 'BasicController',
path: '*'
});
});
BasicController = RouteController.extend({
layoutTemplate: 'layout',
notFoundTemplate: 'notFound',
loadingTemplate: 'loading',
waitOn: function () {
return Meteor.subscribe('Users');
},
before: function () {
Path.set(Router.current().path);
}
});
ChannelController = BasicController.extend({
waitOn: function () {
return [
Meteor.subscribe('Channels', Session.get('channelSearchQuery')),
Meteor.subscribe('Users'),
Meteor.subscribe('Followers')
];
},
before: function () {
var currentCoder = getCurrentCoder(this.params.coderId);
Session.set('currentCoder', currentCoder.userId);
Session.set('currentUsername', currentCoder.username);
Session.set('currentVideo', this.params.videoId);
}
});
SearchController = ChannelController.extend({
template: 'channels',
before: function () {
Session.set('channelSearchQuery', this.params.keyword);
}
});
LanguageController = BasicController.extend({
waitOn: function () {
return [
Meteor.subscribe('Languages'),
Meteor.subscribe('Channels'),
Meteor.subscribe('Users')
];
},
before: function () {
Session.set('currentLanguage', this.params.language);
}
});
LoggedUserController = BasicController.extend({
before: function () {
if (! Meteor.user()) {
this.redirect(Router.routes.login.path());
this.stop();
return;
}
}
});
function getCurrentCoder (coderId) {
var user = Meteor.users.findOne({'profile.username': coderId}),
currentCoder = {coderId: coderId};
if (user) {
return {
userId: user._id,
username: coderId
};
}
return {
userId: coderId
};
}
function setPresence (whereAt) {
Meteor.Presence.state = function() {
return {
online : true,
whereAt: whereAt
}
}
}
function trackPageview (path) {
var settings = Meteor.settings,
UA = null;
if (preventTracking()) {
return;
}
if (settings && settings.public) {
UA = Meteor.settings.public.ga;
} else {
// production
UA = 'UA-XXXXXXXX-1';
}
// ga
window._gaq = window._gaq || [];
window._gaq.push(['_setAccount', UA]);
window._gaq.push(['_trackPageview', path]);
// woopra
trackWoopra();
}
Deps.autorun(function () {
var current = Router.current();
if (! current) {
return;
}
var path = current.path;
trackPageview(path);
setPresence(path);
Deps.afterFlush(function () {
$(window).scrollTop(0);
});
});
trackWoopra = function (name, options) {
// woopra
var user = Meteor.user(),
identification = {};
if (Meteor.user()) {
identification = {
name: user.profile.name,
email: user._id
};
if (user.profile && user.profile.username) {
identification.username = user.profile.username;
}
}
woopra.identify(identification);
if (! name) {
return woopra.track();
}
return woopra.track(name, options);
};
trackGAEvent = function (category, action, label) {
_gaq.push(['_trackEvent', category, action, label]);
};
trackEvent = function (name, options) {
if (preventTracking()) {
return;
}
if (typeof options === 'object') {
trackWoopra(name, options);
}
var label = _.values(options).join('_'),
action = 'click';
return trackGAEvent(name, action, label);
};
preventTracking = function () {
if (_.isEmpty(Meteor.absoluteUrl().match('http://coderstv.com/'))) {
console.debug('In localhost not tracking GA');
return true;
}
return false;
};
@wbashir
Copy link

wbashir commented Nov 12, 2013

This is really helpful, is there a way to check roles for a set of routes: [route1, route2] and not others. I know i can copy and paste as well as extend the LoginController for each route but that gets messy?

@dillongreen
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment