-
Notifications
You must be signed in to change notification settings - Fork 503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generic web analytics tracking implementation #681
Merged
yurishkuro
merged 6 commits into
jaegertracing:master
from
th3M1ke:Generic-analytics-tracking-impl
Feb 16, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
605f980
Generic web analytics tracking implementation
th3M1ke 3f71ca3
Update due to comments, refactor Google Analytic tracker
th3M1ke 6e147f7
Add unit tests
th3M1ke 85fec84
Update tests
th3M1ke 171ca8f
Update tests
th3M1ke 186e6c8
Increase test coverage
th3M1ke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,35 @@ | ||
// Copyright (c) 2021 The Jaeger Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import { RavenStatic } from 'raven-js'; | ||
import { TNil } from '.'; | ||
import { Config } from './config'; | ||
|
||
export interface IWebAnalyticsFunc { | ||
(config: Config, versionShort: string, versionLong: string): IWebAnalytics; | ||
} | ||
|
||
export default interface IWebAnalytics { | ||
init: () => void; | ||
context: boolean | RavenStatic | null; | ||
isEnabled: () => boolean; | ||
trackPageView: (pathname: string, search: string | TNil) => void; | ||
trackError: (description: string) => void; | ||
trackEvent: ( | ||
category: string, | ||
action: string, | ||
labelOrValue?: string | number | TNil, | ||
value?: number | TNil | ||
) => void; | ||
} |
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,218 @@ | ||
// Copyright (c) 2017 Uber Technologies, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/* eslint-disable import/first */ | ||
jest.mock('./conv-raven-to-ga', () => () => ({ | ||
category: 'jaeger/a', | ||
action: 'some-action', | ||
message: 'jaeger/a', | ||
})); | ||
|
||
jest.mock('./index', () => { | ||
global.process.env.REACT_APP_VSN_STATE = '{}'; | ||
return require.requireActual('./index'); | ||
}); | ||
|
||
import ReactGA from 'react-ga'; | ||
import * as GA from './ga'; | ||
import * as utils from './utils'; | ||
|
||
let longStr = '---'; | ||
function getStr(len) { | ||
while (longStr.length < len) { | ||
longStr += longStr.slice(0, len - longStr.length); | ||
} | ||
return longStr.slice(0, len); | ||
} | ||
|
||
describe('google analytics tracking', () => { | ||
let calls; | ||
let tracking; | ||
|
||
beforeAll(() => { | ||
tracking = GA.default( | ||
{ | ||
tracking: { | ||
gaID: 'UA-123456', | ||
trackErrors: true, | ||
cookiesToDimensions: [{ cookie: 'page', dimension: 'dimension1' }], | ||
}, | ||
}, | ||
'c0mm1ts', | ||
'c0mm1tL' | ||
); | ||
}); | ||
|
||
beforeEach(() => { | ||
calls = ReactGA.testModeAPI.calls; | ||
calls.length = 0; | ||
}); | ||
|
||
describe('init', () => { | ||
it('check init function (no cookies)', () => { | ||
tracking.init(); | ||
expect(calls).toEqual([ | ||
['create', 'UA-123456', 'auto'], | ||
[ | ||
'set', | ||
{ | ||
appId: 'github.com/jaegertracing/jaeger-ui', | ||
appName: 'Jaeger UI', | ||
appVersion: 'c0mm1tL', | ||
}, | ||
], | ||
]); | ||
}); | ||
|
||
it('check init function (no cookies)', () => { | ||
document.cookie = 'page=1;'; | ||
tracking.init(); | ||
expect(calls).toEqual([ | ||
['create', 'UA-123456', 'auto'], | ||
[ | ||
'set', | ||
{ | ||
appId: 'github.com/jaegertracing/jaeger-ui', | ||
appName: 'Jaeger UI', | ||
appVersion: 'c0mm1tL', | ||
}, | ||
], | ||
['set', { dimension1: '1' }], | ||
]); | ||
}); | ||
}); | ||
|
||
describe('trackPageView', () => { | ||
it('tracks a page view', () => { | ||
tracking.trackPageView('a', 'b'); | ||
expect(calls).toEqual([['send', { hitType: 'pageview', page: 'ab' }]]); | ||
}); | ||
|
||
it('ignores search when it is falsy', () => { | ||
tracking.trackPageView('a'); | ||
expect(calls).toEqual([['send', { hitType: 'pageview', page: 'a' }]]); | ||
}); | ||
}); | ||
|
||
describe('trackError', () => { | ||
it('tracks an error', () => { | ||
tracking.trackError('a'); | ||
expect(calls).toEqual([ | ||
['send', { hitType: 'exception', exDescription: expect.any(String), exFatal: false }], | ||
]); | ||
}); | ||
|
||
it('ensures "jaeger" is prepended', () => { | ||
tracking.trackError('a'); | ||
expect(calls).toEqual([['send', { hitType: 'exception', exDescription: 'jaeger/a', exFatal: false }]]); | ||
}); | ||
|
||
it('truncates if needed', () => { | ||
const str = `jaeger/${getStr(200)}`; | ||
tracking.trackError(str); | ||
expect(calls).toEqual([ | ||
['send', { hitType: 'exception', exDescription: str.slice(0, 149), exFatal: false }], | ||
]); | ||
}); | ||
}); | ||
|
||
describe('trackEvent', () => { | ||
it('tracks an event', () => { | ||
const category = 'jaeger/some-category'; | ||
const action = 'some-action'; | ||
tracking.trackEvent(category, action); | ||
expect(calls).toEqual([ | ||
[ | ||
'send', | ||
{ | ||
hitType: 'event', | ||
eventCategory: category, | ||
eventAction: action, | ||
}, | ||
], | ||
]); | ||
}); | ||
|
||
it('prepends "jaeger/" to the category, if needed', () => { | ||
const category = 'some-category'; | ||
const action = 'some-action'; | ||
tracking.trackEvent(category, action); | ||
expect(calls).toEqual([ | ||
['send', { hitType: 'event', eventCategory: `jaeger/${category}`, eventAction: action }], | ||
]); | ||
}); | ||
|
||
it('truncates values, if needed', () => { | ||
const str = `jaeger/${getStr(600)}`; | ||
tracking.trackEvent(str, str, str); | ||
expect(calls).toEqual([ | ||
[ | ||
'send', | ||
{ | ||
hitType: 'event', | ||
eventCategory: str.slice(0, 149), | ||
eventAction: str.slice(0, 499), | ||
eventLabel: str.slice(0, 499), | ||
}, | ||
], | ||
]); | ||
}); | ||
}); | ||
|
||
it('converting raven-js errors', () => { | ||
window.onunhandledrejection({ reason: new Error('abc') }); | ||
expect(calls).toEqual([ | ||
['send', { hitType: 'exception', exDescription: expect.any(String), exFatal: false }], | ||
['send', { hitType: 'event', eventCategory: expect.any(String), eventAction: expect.any(String) }], | ||
]); | ||
}); | ||
|
||
describe('Debug mode', () => { | ||
let trackingDebug; | ||
|
||
beforeAll(() => { | ||
const originalWindow = { ...window }; | ||
const windowSpy = jest.spyOn(global, 'window', 'get'); | ||
windowSpy.mockImplementation(() => ({ | ||
...originalWindow, | ||
location: { | ||
...originalWindow.location, | ||
href: 'http://my.test/page', | ||
search: 'ga-debug=true', | ||
}, | ||
})); | ||
|
||
trackingDebug = GA.default( | ||
{ | ||
tracking: { | ||
gaID: 'UA-123456', | ||
trackErrors: true, | ||
cookiesToDimensions: [{ cookie: 'page', dimension: 'dimension1' }], | ||
}, | ||
}, | ||
'c0mm1ts', | ||
'c0mm1tL' | ||
); | ||
}); | ||
|
||
it('isDebugMode = true', () => { | ||
utils.logTrackingCalls = jest.fn(); | ||
trackingDebug.init(); | ||
trackingDebug.trackError(); | ||
trackingDebug.trackEvent('jaeger/some-category', 'some-action'); | ||
trackingDebug.trackPageView('a', 'b'); | ||
expect(utils.logTrackingCalls).toHaveBeenCalledTimes(4); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a place where the type of this function is defined (it seems to take config as arg)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand the question correct, my answer is yes it will look like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant whether we have a type definition for Config somewhere. Maybe not.