-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from AmberEngine/analytics
Add analytics
- Loading branch information
Showing
7 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import test from 'ava'; | ||
import AnalyticsProvider from '../analytics'; | ||
|
||
class FakeAnalyticsSource { | ||
calls = [] | ||
enabled = true | ||
initialized = false | ||
initialize() { | ||
this.initialized = true; | ||
} | ||
pageView(page) { | ||
this.calls.push({ call: 'pageView', page }); | ||
} | ||
error(error) { | ||
this.calls.push({ call: 'error', error }); | ||
} | ||
} | ||
|
||
test('analyticsProvider is created with specified sources', t => { | ||
const fakeSource = new FakeAnalyticsSource(); | ||
const analytics = new AnalyticsProvider([fakeSource]); | ||
t.is(analytics.analyticSources.length, 1); | ||
t.is(analytics.analyticSources[0] instanceof FakeAnalyticsSource, true); | ||
}); | ||
|
||
test('analyticsProvider initializes specified sources', t => { | ||
const fakeSource = new FakeAnalyticsSource(); | ||
t.is(fakeSource.initialized, false); | ||
const analytics = new AnalyticsProvider([fakeSource]); | ||
t.is(fakeSource.initialized, true); | ||
}); | ||
|
||
test('analyticsProvider doesn\'t initialize disabled sources', t => { | ||
const fakeSource = new FakeAnalyticsSource(); | ||
fakeSource.enabled = false; | ||
t.is(fakeSource.initialized, false); | ||
const analytics = new AnalyticsProvider([fakeSource]); | ||
t.is(fakeSource.initialized, false); | ||
}); | ||
|
||
test('analyticsProvider calls pageView of enabled sources', t => { | ||
const fakeSource = new FakeAnalyticsSource(); | ||
const analytics = new AnalyticsProvider([fakeSource]); | ||
|
||
analytics.pageView(); | ||
t.is(fakeSource.calls.length, 1); | ||
t.is(fakeSource.calls[0].call, 'pageView'); | ||
t.is(fakeSource.calls[0].page, 'blank'); | ||
}); | ||
|
||
test('analyticsProvider calls error of enabled sources', t => { | ||
const fakeSource = new FakeAnalyticsSource(); | ||
const analytics = new AnalyticsProvider([fakeSource]); | ||
|
||
analytics.error('Some bad error!'); | ||
t.is(fakeSource.calls.length, 1); | ||
t.is(fakeSource.calls[0].call, 'error'); | ||
t.is(fakeSource.calls[0].error, 'Some bad error!'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import ReactGA from 'react-ga'; | ||
|
||
const googleAnalyticsSource = { | ||
enabled: !!process.env.APP_GA_ACCOUNT_ID, | ||
initialize() { | ||
ReactGA.initialize(process.env.APP_GA_ACCOUNT_ID); | ||
}, | ||
pageView: (page) => { | ||
ReactGA.set({ page }); | ||
ReactGA.pageview(page); | ||
}, | ||
error: (error) => { | ||
// Do nothing! | ||
}, | ||
}; | ||
|
||
export default googleAnalyticsSource; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import GoogleAnalytics from './googleAnalyticsSource'; | ||
import SentryAnalytics from './sentryAnalyticsSource'; | ||
|
||
// Export the analytic sources | ||
export { | ||
GoogleAnalytics, | ||
SentryAnalytics, | ||
}; | ||
|
||
export default class AnalyticsProvider { | ||
analyticSources: [] | ||
|
||
constructor(sources = []) { | ||
this.analyticSources = sources; | ||
this.initialize(); | ||
} | ||
|
||
initialize() { | ||
this.getAnalyticSources() | ||
.forEach(source => { | ||
source.initialize(); | ||
}); | ||
} | ||
|
||
getAnalyticSources() { | ||
return this.analyticSources | ||
.filter(source => source.enabled); | ||
} | ||
|
||
registerSource = (source) => { | ||
this.analyticSources = this.analyticSources.concat([source]); | ||
} | ||
|
||
pageView = () => { | ||
const page = window.location.pathname + window.location.search; | ||
this.getAnalyticSources() | ||
.forEach(source => { | ||
source.pageView(page); | ||
}); | ||
} | ||
|
||
error = (error) => { | ||
this.getAnalyticSources() | ||
.forEach(source => { | ||
source.error(error); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import Raven from 'raven-js'; | ||
|
||
const sentryAnalyticsSource = { | ||
enabled: !!process.env.APP_SENTRY_ACCOUNT_ID, | ||
initialize() { | ||
if (this.enabled) { | ||
const { APP_SENTRY_ACCOUNT_ID, APP_SENTRY_PROJECT_NAME } = process.env; | ||
Raven | ||
.config(`https://${APP_SENTRY_ACCOUNT_ID}@sentry.io/${APP_SENTRY_PROJECT_NAME}`) | ||
.install(); | ||
} | ||
}, | ||
pageView: (page) => { | ||
// Do nothing! | ||
}, | ||
error: (error) => { | ||
Raven.captureMessage(error); | ||
}, | ||
}; | ||
|
||
export default sentryAnalyticsSource; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters