Skip to content
代码片段 群组 项目
未验证 提交 3c0186a7 编辑于 作者: Niko Belokolodov's avatar Niko Belokolodov 提交者: GitLab
浏览文件

Trigger Sentry when tracking called with incorrect event name

上级 250fcbba
No related branches found
No related tags found
2 合并请求!3031Merge per-main-jh to main-jh by luzhiyuan,!3030Merge per-main-jh to main-jh
import * as Sentry from '~/sentry/sentry_browser_wrapper'; import * as Sentry from '~/sentry/sentry_browser_wrapper';
import getStandardContext from './get_standard_context'; import getStandardContext from './get_standard_context';
import { validateEvent } from './utils';
export function dispatchSnowplowEvent( export function dispatchSnowplowEvent(
category = document.body.dataset.page, category = document.body.dataset.page,
...@@ -11,6 +12,8 @@ export function dispatchSnowplowEvent( ...@@ -11,6 +12,8 @@ export function dispatchSnowplowEvent(
throw new Error('Tracking: no category provided for tracking.'); throw new Error('Tracking: no category provided for tracking.');
} }
validateEvent(action);
const { label, property, extra = {} } = data; const { label, property, extra = {} } = data;
let { value } = data; let { value } = data;
......
import { omitBy, isUndefined } from 'lodash'; import { omitBy, isUndefined } from 'lodash';
import { TRACKING_CONTEXT_SCHEMA } from '~/experimentation/constants'; import { TRACKING_CONTEXT_SCHEMA } from '~/experimentation/constants';
import { getExperimentData } from '~/experimentation/utils'; import { getExperimentData } from '~/experimentation/utils';
import * as Sentry from '~/sentry/sentry_browser_wrapper';
import { import {
ACTION_ATTR_SELECTOR, ACTION_ATTR_SELECTOR,
LOAD_ACTION_ATTR_SELECTOR, LOAD_ACTION_ATTR_SELECTOR,
...@@ -189,6 +190,12 @@ export const validateAdditionalProperties = (additionalProperties) => { ...@@ -189,6 +190,12 @@ export const validateAdditionalProperties = (additionalProperties) => {
}); });
}; };
export const validateEvent = (event) => {
if (event && /\s/.test(event)) {
Sentry.captureException(new Error(`Event name should not contain whitespace: ${event}`));
}
};
function filterProperties(additionalProperties, predicate) { function filterProperties(additionalProperties, predicate) {
return Object.keys(additionalProperties).reduce((acc, key) => { return Object.keys(additionalProperties).reduce((acc, key) => {
if (predicate(key)) { if (predicate(key)) {
......
...@@ -9,7 +9,9 @@ import { ...@@ -9,7 +9,9 @@ import {
validateAdditionalProperties, validateAdditionalProperties,
getCustomAdditionalProperties, getCustomAdditionalProperties,
getBaseAdditionalProperties, getBaseAdditionalProperties,
validateEvent,
} from '~/tracking/utils'; } from '~/tracking/utils';
import * as Sentry from '~/sentry/sentry_browser_wrapper';
import { TRACKING_CONTEXT_SCHEMA } from '~/experimentation/constants'; import { TRACKING_CONTEXT_SCHEMA } from '~/experimentation/constants';
import { REFERRER_TTL, URLS_CACHE_STORAGE_KEY } from '~/tracking/constants'; import { REFERRER_TTL, URLS_CACHE_STORAGE_KEY } from '~/tracking/constants';
import { TEST_HOST } from 'helpers/test_constants'; import { TEST_HOST } from 'helpers/test_constants';
...@@ -220,6 +222,36 @@ describe('~/tracking/utils', () => { ...@@ -220,6 +222,36 @@ describe('~/tracking/utils', () => {
}); });
}); });
describe('validateEvent', () => {
let sentrySpy;
beforeEach(() => {
sentrySpy = jest.spyOn(Sentry, 'captureException');
});
afterEach(() => {
sentrySpy.mockRestore();
});
it('calls Sentry for event names with whitespace', () => {
validateEvent('event name');
expect(sentrySpy).toHaveBeenCalled();
});
it('does not call Sentry for event names eqaual to nil', () => {
validateEvent(null);
expect(sentrySpy).not.toHaveBeenCalled();
});
it('does not call Sentry for event names without whitespace', () => {
validateEvent('event-name');
expect(sentrySpy).not.toHaveBeenCalled();
});
});
describe('getCustomAdditionalProperties', () => { describe('getCustomAdditionalProperties', () => {
it('returns only custom properties', () => { it('returns only custom properties', () => {
const additionalProperties = { const additionalProperties = {
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册