Skip to content

Commit e0037f1

Browse files
committed
feat: add referer option when calling attachTo/createWidget
1 parent 45e66e9 commit e0037f1

File tree

6 files changed

+37
-16
lines changed

6 files changed

+37
-16
lines changed

packages/feedback/src/integration.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
SUBMIT_BUTTON_LABEL,
1616
SUCCESS_MESSAGE_TEXT,
1717
} from './constants';
18-
import type { FeedbackConfigurationWithDefaults, Widget } from './types';
18+
import type { CreateWidgetOptionOverrides, FeedbackConfigurationWithDefaults, Widget } from './types';
1919
import { createActorStyles } from './widget/Actor.css';
2020
import { createShadowHost } from './widget/createShadowHost';
2121
import { createWidget } from './widget/createWidget';
@@ -84,7 +84,6 @@ export class Feedback implements Integration {
8484

8585
public constructor({
8686
id = 'sentry-feedback',
87-
// attachTo = null,
8887
autoInject = true,
8988
showEmail = true,
9089
showName = true,
@@ -130,7 +129,6 @@ export class Feedback implements Integration {
130129

131130
this.options = {
132131
id,
133-
// attachTo,
134132
autoInject,
135133
isAnonymous,
136134
isEmailRequired,
@@ -200,7 +198,7 @@ export class Feedback implements Integration {
200198
/**
201199
* Adds click listener to attached element to open a feedback dialog
202200
*/
203-
public attachTo(el: Node | string, optionOverrides: Partial<FeedbackConfigurationWithDefaults>): Widget | null {
201+
public attachTo(el: Node | string, optionOverrides: CreateWidgetOptionOverrides): Widget | null {
204202
try {
205203
const options = Object.assign({}, this.options, optionOverrides);
206204

@@ -227,7 +225,7 @@ export class Feedback implements Integration {
227225
/**
228226
* Creates a new widget. Accepts partial options to override any options passed to constructor.
229227
*/
230-
public createWidget(optionOverrides: Partial<FeedbackConfigurationWithDefaults>): Widget | null {
228+
public createWidget(optionOverrides: CreateWidgetOptionOverrides): Widget | null {
231229
try {
232230
return this._createWidget(Object.assign({}, this.options, optionOverrides));
233231
} catch (err) {

packages/feedback/src/sendFeedback.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getCurrentHub } from '@sentry/core';
33
import { getLocationHref } from '@sentry/utils';
44

55
import { sendFeedbackRequest } from './util/sendFeedbackRequest';
6+
import { SendFeedbackOptions } from './types';
67

78
interface SendFeedbackParams {
89
message: string;
@@ -11,16 +12,12 @@ interface SendFeedbackParams {
1112
url?: string;
1213
}
1314

14-
interface SendFeedbackOptions {
15-
includeReplay?: boolean;
16-
}
17-
1815
/**
1916
* Public API to send a Feedback item to Sentry
2017
*/
2118
export function sendFeedback(
2219
{ name, email, message, url = getLocationHref() }: SendFeedbackParams,
23-
{ includeReplay = true }: SendFeedbackOptions = {},
20+
{ referrer, includeReplay = true }: SendFeedbackOptions = {},
2421
): ReturnType<typeof sendFeedbackRequest> {
2522
const hub = getCurrentHub();
2623
const client = hub && hub.getClient<BrowserClient>();
@@ -38,5 +35,6 @@ export function sendFeedback(
3835
url,
3936
replay_id: replayId,
4037
},
38+
referrer,
4139
});
4240
}

packages/feedback/src/types/index.ts

+20
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,24 @@ export interface SendFeedbackData {
2929
replay_id?: string;
3030
name?: string;
3131
};
32+
referrer?: string;
3233
}
3334

35+
export interface SendFeedbackOptions {
36+
/**
37+
* Should include replay with the feedback?
38+
*/
39+
includeReplay?: boolean;
40+
41+
/**
42+
* Allows user to set a referrer for feedback, to act as a category for the feedback
43+
*/
44+
referrer?: string;
45+
}
46+
47+
/**
48+
* Feedback data expected from UI/form
49+
*/
3450
export interface FeedbackFormData {
3551
message: string;
3652
email?: string;
@@ -213,6 +229,10 @@ export interface FeedbackTheme {
213229
error: string;
214230
}
215231

232+
export interface CreateWidgetOptionOverrides extends Partial<FeedbackConfigurationWithDefaults> {
233+
referrer?: string;
234+
}
235+
216236
export interface FeedbackThemes {
217237
dark: FeedbackTheme;
218238
light: FeedbackTheme;

packages/feedback/src/util/handleFeedbackSubmit.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { sendFeedback } from '../sendFeedback';
2-
import type { FeedbackFormData } from '../types';
2+
import type { FeedbackFormData, SendFeedbackOptions } from '../types';
33
import type { DialogComponent } from '../widget/Dialog';
44

55
/**
6-
*
6+
* Calls `sendFeedback` to send feedback, handles UI behavior of dialog.
77
*/
88
export async function handleFeedbackSubmit(
99
dialog: DialogComponent | null,
1010
feedback: FeedbackFormData,
11+
options?: SendFeedbackOptions
1112
): Promise<Response | false> {
1213
if (!dialog) {
1314
// Not sure when this would happen
@@ -25,7 +26,7 @@ export async function handleFeedbackSubmit(
2526
try {
2627
dialog.hideError();
2728
dialog.setSubmitDisabled();
28-
const resp = await sendFeedback(feedback);
29+
const resp = await sendFeedback(feedback, options);
2930

3031
if (!resp) {
3132
// Errored... re-enable submit button

packages/feedback/src/util/sendFeedbackRequest.ts

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { prepareFeedbackEvent } from './prepareFeedbackEvent';
99
*/
1010
export async function sendFeedbackRequest({
1111
feedback: { message, email, name, replay_id, url },
12+
referrer,
1213
}: SendFeedbackData): Promise<Response | null> {
1314
const hub = getCurrentHub();
1415

@@ -33,6 +34,9 @@ export async function sendFeedbackRequest({
3334
replay_id,
3435
url,
3536
},
37+
tags: {
38+
referrer,
39+
}
3640
// type: 'feedback_event',
3741
};
3842

packages/feedback/src/widget/createWidget.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import { SuccessMessage } from './SuccessMessage';
1111

1212
interface CreateWidgetParams {
1313
shadow: ShadowRoot;
14-
options: FeedbackConfigurationWithDefaults;
14+
options: FeedbackConfigurationWithDefaults & {referrer?: string};
1515
attachTo?: Node;
1616
}
1717

1818
/**
19-
*
19+
* Creates a new widget. Returns public methods that control widget behavior.
2020
*/
2121
export function createWidget({ shadow, options, attachTo }: CreateWidgetParams): Widget {
2222
let actor: ActorComponent | undefined;
@@ -64,7 +64,7 @@ export function createWidget({ shadow, options, attachTo }: CreateWidgetParams):
6464
return;
6565
}
6666

67-
const result = await handleFeedbackSubmit(dialog, feedback);
67+
const result = await handleFeedbackSubmit(dialog, feedback, {referrer: options.referrer});
6868

6969
// Error submitting feedback
7070
if (!result) {

0 commit comments

Comments
 (0)