Skip to content

Commit

Permalink
Gracefully Handle Analytics Error.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeyer2115 committed Sep 2, 2022
1 parent 910faa2 commit d09dd49
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/core/analytics/analyticsreporter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/** @module AnalyticsReporter */

import AnalyticsEvent from './analyticsevent';
import { AnswersAnalyticsError } from '../errors/errors';
import { PRODUCTION } from '../constants';
import HttpRequester from '../http/httprequester';
import { getAnalyticsUrl } from '../utils/urlutils';
Expand Down Expand Up @@ -98,11 +97,13 @@ export default class AnalyticsReporter {
ytag('optin', true);
cookieData = ytag('yfpc', null);
} else if (this._conversionTrackingEnabled) {
throw new AnswersAnalyticsError('Tried to enable conversion tracking without including ytag');
console.error('Tried to enable conversion tracking without including ytag');
return false;
}

if (!(event instanceof AnalyticsEvent)) {
throw new AnswersAnalyticsError('Tried to send invalid analytics event', event);
console.error('Tried to send invalid analytics event', event);
return false;
}

if (includeQueryId) {
Expand Down
20 changes: 11 additions & 9 deletions tests/core/analytics/analyticsreporter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import AnalyticsReporter from '../../../src/core/analytics/analyticsreporter';
import HttpRequester from '../../../src/core/http/httprequester';
import { AnswersAnalyticsError } from '../../../src/core/errors/errors';
import AnalyticsEvent from '../../../src/core/analytics/analyticsevent';
import { getAnalyticsUrl } from '../../../src/core/utils/urlutils';
import { PRODUCTION } from '../../../src/core/constants';
Expand All @@ -21,10 +20,13 @@ describe('reporting events', () => {
analyticsReporter = new AnalyticsReporter('abc123', null, '213412', true);
});

it('throws an error if given a non-AnalyticsEvent', () => {
expect(() => {
analyticsReporter.report({ event_type: 'fake event' });
}).toThrow(AnswersAnalyticsError);
it('logs a console error if given a non-AnalyticsEvent', () => {
const consoleErrorSpy = jest.spyOn(console, 'error');
expect(analyticsReporter.report({ event_type: 'fake event' })).toBeFalsy();
expect(consoleErrorSpy).toHaveBeenLastCalledWith(
'Tried to send invalid analytics event',
{ event_type: 'fake event' }
);
});

it('sends the event via beacon in the "data" property', () => {
Expand Down Expand Up @@ -68,11 +70,11 @@ describe('reporting events', () => {
expect.anything());
});

it('throws error if opted in and ytag missing', () => {
it('logs a console error if opted in and ytag missing', () => {
analyticsReporter.setConversionTrackingEnabled(true);
expect(() => {
analyticsReporter.report(new AnalyticsEvent('thumbs_up'));
}).toThrow(AnswersAnalyticsError);
const consoleErrorSpy = jest.spyOn(console, 'error');
expect(analyticsReporter.report(new AnalyticsEvent('thumbs_up'))).toBeFalsy();
expect(consoleErrorSpy).toHaveBeenLastCalledWith('Tried to enable conversion tracking without including ytag');
});

it('includes cookies if opted in and ytag present', () => {
Expand Down

0 comments on commit d09dd49

Please sign in to comment.