Skip to content

feat(feedback): Allow passing tags field to any feedback config param #12197

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

Merged
merged 11 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ sentryTest('should capture feedback', async ({ getLocalTestUrl, page }) => {
},
},
level: 'info',
tags: {},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add some tests showing that the tags are flowing through?

timestamp: expect.any(Number),
event_id: expect.stringMatching(/\w{32}/),
environment: 'production',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ sentryTest('should capture feedback', async ({ forceFlushReplay, getLocalTestUrl
},
},
level: 'info',
tags: {},
timestamp: expect.any(Number),
event_id: expect.stringMatching(/\w{32}/),
environment: 'production',
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/feedback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { getClient, getCurrentScope } from './currentScopes';
* Send user feedback to Sentry.
*/
export function captureFeedback(
feedbackParams: SendFeedbackParams,
params: SendFeedbackParams,
hint: EventHint & { includeReplay?: boolean } = {},
scope = getCurrentScope(),
): string {
const { message, name, email, url, source, associatedEventId } = feedbackParams;
const { message, name, email, url, source, associatedEventId, tags } = params;

const feedbackEvent: FeedbackEvent = {
contexts: {
Expand All @@ -25,6 +25,7 @@ export function captureFeedback(
},
type: 'feedback',
level: 'info',
tags,
};

const client = (scope && scope.getClient()) || getClient();
Expand Down
8 changes: 5 additions & 3 deletions packages/feedback/src/core/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,18 @@ export const buildFeedbackIntegration = ({
const feedbackIntegration = (({
// FeedbackGeneralConfiguration
id = 'sentry-feedback',
showBranding = true,
autoInject = true,
showBranding = true,
isEmailRequired = false,
isNameRequired = false,
showEmail = true,
showName = true,
enableScreenshot = true,
useSentryUser = {
email: 'email',
name: 'username',
},
isNameRequired = false,
isEmailRequired = false,
tags,

// FeedbackThemeConfiguration
colorScheme = 'system',
Expand Down Expand Up @@ -115,6 +116,7 @@ export const buildFeedbackIntegration = ({
showName,
enableScreenshot,
useSentryUser,
tags,

colorScheme,
themeDark,
Expand Down
13 changes: 8 additions & 5 deletions packages/feedback/src/core/sendFeedback.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { captureFeedback } from '@sentry/core';
import { getClient } from '@sentry/core';
import type { EventHint, SendFeedback, SendFeedbackParams, TransportMakeRequestResponse } from '@sentry/types';
import type { Event } from '@sentry/types';
import { getCurrentScope } from '@sentry/core';
import type { Event, EventHint, SendFeedback, SendFeedbackParams, TransportMakeRequestResponse } from '@sentry/types';
import { getLocationHref } from '@sentry/utils';
import { FEEDBACK_API_SOURCE } from '../constants';

/**
* Public API to send a Feedback item to Sentry
*/
export const sendFeedback: SendFeedback = (
options: SendFeedbackParams,
params: SendFeedbackParams,
hint: EventHint & { includeReplay?: boolean } = { includeReplay: true },
): Promise<string> => {
if (!options.message) {
if (!params.message) {
throw new Error('Unable to submit feedback with empty message');
}

Expand All @@ -23,11 +23,14 @@ export const sendFeedback: SendFeedback = (
throw new Error('No client setup, cannot send feedback.');
}

if (params.tags && Object.keys(params.tags).length) {
getCurrentScope().setTags(params.tags);
}
const eventId = captureFeedback(
{
source: FEEDBACK_API_SOURCE,
url: getLocationHref(),
...options,
...params,
},
hint,
);
Expand Down
2 changes: 2 additions & 0 deletions packages/feedback/src/modal/components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export function Form({
screenshotInput,
}: Props): VNode {
const {
tags,
addScreenshotButtonLabel,
removeScreenshotButtonLabel,
cancelButtonLabel,
Expand Down Expand Up @@ -122,6 +123,7 @@ export function Form({
email: data.email,
message: data.message,
source: FEEDBACK_WIDGET_SOURCE,
tags,
},
{ attachments: data.attachments },
);
Expand Down
4 changes: 4 additions & 0 deletions packages/feedback/src/util/mergeOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export function mergeOptions(
return {
...defaultOptions,
...optionOverrides,
tags: {
...defaultOptions.tags,
...optionOverrides.tags,
},
onFormOpen: () => {
optionOverrides.onFormOpen && optionOverrides.onFormOpen();
defaultOptions.onFormOpen && defaultOptions.onFormOpen();
Expand Down
6 changes: 6 additions & 0 deletions packages/types/src/feedback/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Primitive } from '../misc';
import type { FeedbackFormData } from './form';
import type { FeedbackTheme } from './theme';

Expand Down Expand Up @@ -55,6 +56,11 @@ export interface FeedbackGeneralConfiguration {
email: string;
name: string;
};

/**
* Set an object that will be merged sent as tags data with the event.
*/
tags?: { [key: string]: Primitive };
}

/**
Expand Down
12 changes: 7 additions & 5 deletions packages/types/src/feedback/sendFeedback.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Event, EventHint } from '../event';
import type { Primitive } from '../misc';
import type { User } from '../user';

/**
Expand Down Expand Up @@ -38,13 +39,14 @@ export interface SendFeedbackParams {
url?: string;
source?: string;
associatedEventId?: string;
}

interface SendFeedbackOptions extends EventHint {
/**
* Should include replay with the feedback?
* Set an object that will be merged sent as tags data with the event.
*/
includeReplay?: boolean;
tags?: { [key: string]: Primitive };
}

export type SendFeedback = (params: SendFeedbackParams, options?: SendFeedbackOptions) => Promise<string>;
export type SendFeedback = (
params: SendFeedbackParams,
hint?: EventHint & { includeReplay?: boolean },
) => Promise<string>;
Loading