Skip to content

Commit

Permalink
update(Metrics): Set Google Analytics userId if able. (#75)
Browse files Browse the repository at this point in the history
* update(Metrics): set Google Analytics user ID if able

trust the function

[Metrics] add google analytics types, update tests

[Metrics] rename to bootstrapGoogleAnalyticsUser

fix package-lock

clean up a couple things

* empty commit to bypass danger

* fix test name

* update types and tests

* fix typing of jest.spy

* downgrade npm version to remove option=true
  • Loading branch information
alecklandgraf authored Jun 5, 2019
1 parent 40f35db commit 4b883f6
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
__DEV__: true,
jsdom: true,
newrelic: true,
ga: true,
},

env: {
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"@babel/preset-react": "^7.0.0",
"@babel/preset-typescript": "^7.3.3",
"@types/enzyme": "^3.9.1",
"@types/google.analytics": "0.0.39",
"@types/jest": "^24.0.11",
"@types/storybook__addon-a11y": "^5.0.0",
"@types/storybook__addon-actions": "^3.4.2",
Expand Down
3 changes: 3 additions & 0 deletions packages/metrics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Provides integrated [NewRelic](https://newrelic.com/) and [Sentry](https://sentry.io/welcome/)
insights and metrics logging.

This will also set the Google Analytics `userId` if `ga` is global and `userID` is passed to
`Metrics.initialize`.

```bash static
npm install @airbnb/lunar-metrics --save
```
Expand Down
8 changes: 8 additions & 0 deletions packages/metrics/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Raven from 'raven-js';
import hasNewRelic from './utils/hasNewRelic';
import hasGoogleAnalytics from './utils/hasGoogleAnalytics';

export type IgnoreError = string | RegExp;

Expand Down Expand Up @@ -28,6 +29,7 @@ class Metrics {

this.bootstrapNewRelic();
this.bootstrapSentry();
this.bootstrapGoogleAnalyticsUser();
}

bootstrapNewRelic() {
Expand Down Expand Up @@ -82,6 +84,12 @@ class Metrics {
})
.install();
}

bootstrapGoogleAnalyticsUser() {
if (hasGoogleAnalytics() && this.settings.userID) {
ga('set', 'userId', this.settings.userID);
}
}
}

export default new Metrics();
3 changes: 3 additions & 0 deletions packages/metrics/src/utils/hasGoogleAnalytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function hasGoogleAnalytics() {
return !!ga && typeof ga === 'function';
}
31 changes: 31 additions & 0 deletions packages/metrics/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ describe('Metrics', () => {

global.newrelic.setCustomAttribute = jest.fn();
global.newrelic.setErrorHandler = jest.fn();

global.ga = (jest.fn() as unknown) as UniversalAnalytics.ga;
});

afterEach(() => {
Expand All @@ -56,14 +58,17 @@ describe('Metrics', () => {
it('calls boostrap functions', () => {
const nrBoot = jest.spyOn(Metrics, 'bootstrapNewRelic');
const sentryBoot = jest.spyOn(Metrics, 'bootstrapSentry');
const bootstrapGoogleAnalyticsUser = jest.spyOn(Metrics, 'bootstrapGoogleAnalyticsUser');

Metrics.initialize();

expect(nrBoot).toHaveBeenCalled();
expect(sentryBoot).toHaveBeenCalled();
expect(bootstrapGoogleAnalyticsUser).toHaveBeenCalled();

nrBoot.mockRestore();
sentryBoot.mockRestore();
bootstrapGoogleAnalyticsUser.mockRestore();
});
});

Expand Down Expand Up @@ -152,4 +157,30 @@ describe('Metrics', () => {
);
});
});

describe('bootstrapGoogleAnalyticsUser', () => {
it('sets the google analytics user if present', () => {
Metrics.settings.userID = 12355;
Metrics.bootstrapGoogleAnalyticsUser();

expect(global.ga).toBeCalledTimes(1);
expect(global.ga).toBeCalledWith('set', 'userId', 12355);
});

it('does not attempt to set the google analytics user if the user ID is not present', () => {
Metrics.settings.userID = null;
Metrics.bootstrapGoogleAnalyticsUser();

expect(global.ga).not.toHaveBeenCalled();
});

it('does not attempt to set the google analytics user if ga is not present', () => {
Metrics.settings.userID = 123;
global.ga = undefined;

expect(() => {
Metrics.bootstrapGoogleAnalyticsUser();
}).not.toThrow();
});
});
});
1 change: 1 addition & 0 deletions types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ interface KeyboardEvent {
declare namespace NodeJS {
interface Global {
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__: any;
ga?: UniversalAnalytics.ga;
location: Location;
navigator: Navigator;
newrelic: NewRelic.Browser;
Expand Down

0 comments on commit 4b883f6

Please sign in to comment.