1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 09:23:13 +01:00

Add third party modules to handle tracking without polluting the code

This commit is contained in:
vrde 2015-07-27 18:06:02 +02:00
parent 861f633476
commit 4d93e076df
10 changed files with 169 additions and 26 deletions

View File

@ -51,8 +51,6 @@
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-60614729-2', 'auto');
</script>
<!-- Intercom library -->

View File

@ -0,0 +1,19 @@
'use strict';
import alt from '../alt';
class EventActions {
constructor() {
this.generateActions(
'applicationWillBoot',
'applicationDidBoot',
'profileDidLoad',
'userDidLogin',
'userDidLogout',
'routeDidChange'
);
}
}
export default alt.createActions(EventActions);

View File

@ -16,6 +16,12 @@ import requests from './utils/requests';
import { getSubdomainSettings } from './utils/constants_utils';
import { initLogging } from './utils/error_utils';
import EventActions from './actions/event_actions';
// require('./third_party/debug');
require('./third_party/ga');
require('./third_party/raven');
require('./third_party/intercom');
initLogging();
let headers = {
@ -43,25 +49,30 @@ class AppGateway {
settings = getSubdomainSettings(subdomain);
appConstants.whitelabel = settings;
updateApiUrls(settings.type, subdomain);
this.load(settings.type);
this.load(settings);
} catch(err) {
// if there are no matching subdomains, we're routing
// to the default frontend
console.logGlobal(err);
this.load('default');
this.load();
}
}
load(type) {
load(settings) {
let type = 'default';
if (settings) {
type = settings.type;
}
EventActions.applicationWillBoot(settings);
Router.run(getRoutes(type), Router.HistoryLocation, (App) => {
if (window.ga) {
window.ga('send', 'pageview');
}
React.render(
<App />,
document.getElementById('main')
);
EventActions.routeDidChange();
});
EventActions.applicationDidBoot(settings);
}
}

View File

@ -2,13 +2,13 @@
import React from 'react';
import Router from 'react-router';
import Raven from 'raven-js';
import UserActions from '../actions/user_actions';
import UserStore from '../stores/user_store';
import WhitelabelActions from '../actions/whitelabel_actions';
import WhitelabelStore from '../stores/whitelabel_store';
import EventActions from '../actions/event_actions';
import Nav from 'react-bootstrap/lib/Nav';
import Navbar from 'react-bootstrap/lib/Navbar';
@ -84,19 +84,7 @@ let Header = React.createClass({
this.setState(state);
if(this.state.currentUser && this.state.currentUser.email) {
// bootup intercom if the user is logged in
window.Intercom('boot', {
app_id: 'oboxh5w1',
email: this.state.currentUser.email,
subdomain: window.location.host.split('.')[0],
widget: {
activator: '#IntercomDefaultWidget'
}
});
Raven.setUserContext({
email: this.state.currentUser.email
});
EventActions.profileDidLoad.defer(this.state.currentUser);
}
},

View File

@ -22,7 +22,8 @@ let constants = {
'name': 'Creative Commons France',
'logo': 'https://s3-us-west-2.amazonaws.com/ascribe0/public/creativecommons/cc.logo.sm.png',
'permissions': ['register', 'edit', 'share', 'del_from_collection'],
'type': 'wallet'
'type': 'wallet',
'ga': 'UA-60614729-4'
},
{
'subdomain': 'cc-staging',
@ -36,7 +37,8 @@ let constants = {
'name': 'Sluice Art Fair',
'logo': 'http://sluice.info/images/logo.gif',
'permissions': ['register', 'edit', 'share', 'del_from_collection'],
'type': 'prize'
'type': 'prize',
'ga': 'UA-60614729-5'
},
{
'subdomain': 'sluice-staging',
@ -46,6 +48,10 @@ let constants = {
'type': 'prize'
}
],
'defaultDomain': {
'type': 'default',
'ga': 'UA-60614729-2'
},
// in case of whitelabel customization, we store stuff here
'whitelabel': {},

30
js/third_party/debug.js vendored Normal file
View File

@ -0,0 +1,30 @@
'use strict';
import alt from '../alt';
import EventActions from '../actions/event_actions';
class DebugHandler {
constructor() {
let symbols = [];
for (let k in EventActions) {
if (typeof EventActions[k] === 'symbol') {
symbols.push(EventActions[k]);
}
}
this.bindListeners({
onWhateverEvent: symbols
});
}
onWhateverEvent() {
let args = arguments[0];
let symbol = arguments[1];
console.debug(symbol, args);
}
}
export default alt.createStore(DebugHandler, 'DebugHandler');

28
js/third_party/ga.js vendored Normal file
View File

@ -0,0 +1,28 @@
'use strict';
import alt from '../alt';
import EventActions from '../actions/event_actions';
class GoogleAnalyticsHandler {
constructor() {
this.bindActions(EventActions);
}
onRouteDidChange() {
console.log(window.location.href);
window.ga('send', 'pageview');
}
onApplicationWillBoot(settings) {
if (settings.ga) {
window.ga('create', settings.ga, 'auto');
console.log('Google Analytics loaded');
} else {
console.log('Cannot load Google Analytics: no tracking code provided');
}
}
}
export default alt.createStore(GoogleAnalyticsHandler, 'GoogleAnalyticsHandler');

34
js/third_party/intercom.js vendored Normal file
View File

@ -0,0 +1,34 @@
'use strict';
import alt from '../alt';
import EventActions from '../actions/event_actions';
class IntercomHandler {
constructor() {
this.bindActions(EventActions);
this.loaded = false;
}
onProfileDidLoad(profile) {
if (this.loaded) {
return;
}
/* eslint-disable */
window.Intercom('boot', {
/* eslint-enable */
app_id: 'oboxh5w1',
email: profile.email,
subdomain: window.location.host.split('.')[0],
widget: {
activator: '#IntercomDefaultWidget'
}
});
console.log('Intercom loaded');
this.loaded = true;
}
}
export default alt.createStore(IntercomHandler, 'IntercomHandler');

28
js/third_party/raven.js vendored Normal file
View File

@ -0,0 +1,28 @@
'use strict';
import alt from '../alt';
import EventActions from '../actions/event_actions';
import Raven from 'raven-js';
class RavenHandler {
constructor() {
this.bindActions(EventActions);
this.loaded = false;
}
onProfileDidLoad(profile) {
if (this.loaded) {
return;
}
Raven.setUserContext({
email: profile.email
});
console.log('Raven loaded');
this.loaded = true;
}
}
export default alt.createStore(RavenHandler, 'RavenHandler');

View File

@ -8,8 +8,9 @@ export function getSubdomainSettings(subdomain) {
if(settings.length === 1) {
return settings[0];
} else if(settings.length === 0) {
throw new Error('There are no subdomain settings for the subdomain: ' + subdomain);
return appConstants.defaultDomain;
// throw new Error('There are no subdomain settings for the subdomain: ' + subdomain);
} else {
throw new Error('Matched multiple subdomains. Adjust constants file.');
}
}
}