@product7/feedback-sdk 1.5.0 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@product7/feedback-sdk",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "description": "JavaScript SDK for integrating Product7 feedback widgets into any website",
5
5
  "main": "dist/feedback-sdk.js",
6
6
  "module": "src/index.js",
@@ -15,11 +15,20 @@ export class SurveyService {
15
15
  return { success: true, data: MOCK_SURVEYS };
16
16
  }
17
17
 
18
+ const respondent = this._getRespondentContext(context);
19
+
18
20
  const params = {
19
21
  url:
20
22
  context.url ||
21
23
  (typeof window !== 'undefined' ? window.location.href : ''),
22
24
  ...getDeviceInfo(),
25
+ ...(respondent.respondent_id && {
26
+ respondent_id: respondent.respondent_id,
27
+ }),
28
+ ...(respondent.email && { email: respondent.email }),
29
+ ...(context.includeEligibility !== undefined && {
30
+ include_eligibility: context.includeEligibility,
31
+ }),
23
32
  ...(context.userProperties && {
24
33
  user_properties: context.userProperties,
25
34
  }),
@@ -37,6 +46,19 @@ export class SurveyService {
37
46
  });
38
47
  }
39
48
 
49
+ _getRespondentContext(context = {}) {
50
+ const userContext =
51
+ typeof this.api.getUserContext === 'function'
52
+ ? this.api.getUserContext() || {}
53
+ : {};
54
+
55
+ return {
56
+ respondent_id:
57
+ context.respondentId || context.userId || userContext.user_id || null,
58
+ email: context.email || userContext.email || null,
59
+ };
60
+ }
61
+
40
62
  async submitSurveyResponse(surveyId, responseData) {
41
63
  if (!surveyId) throw new APIError(400, 'Survey ID is required');
42
64
 
@@ -55,10 +77,16 @@ export class SurveyService {
55
77
  };
56
78
  }
57
79
 
80
+ const respondent = this._getRespondentContext(responseData);
81
+
58
82
  const payload = {
59
83
  rating: responseData.rating,
60
84
  feedback: responseData.feedback || '',
61
85
  answers: responseData.answers || {},
86
+ ...(respondent.respondent_id && {
87
+ respondent_id: respondent.respondent_id,
88
+ }),
89
+ ...(respondent.email && { email: respondent.email }),
62
90
  };
63
91
 
64
92
  return this.api._handleAuthRetry(async () => {
@@ -94,7 +94,11 @@ export class FeedbackSDK {
94
94
 
95
95
  try {
96
96
  const result = await this.apiService.getActiveSurveys(context);
97
- return result.data || [];
97
+ const surveys = result.data || [];
98
+ if (context.includeIneligible) {
99
+ return surveys;
100
+ }
101
+ return surveys.filter((survey) => this._isSurveyEligible(survey));
98
102
  } catch (error) {
99
103
  this.eventBus.emit('sdk:error', { error });
100
104
  throw new SDKError(
@@ -111,7 +115,12 @@ export class FeedbackSDK {
111
115
  );
112
116
  }
113
117
 
114
- const surveys = await this.getActiveSurveys();
118
+ const { context = {}, ...displayOptions } = options;
119
+ const surveys = await this.getActiveSurveys({
120
+ ...context,
121
+ includeEligibility: true,
122
+ includeIneligible: true,
123
+ });
115
124
  const surveyConfig = surveys.find((s) => s.id === surveyId);
116
125
 
117
126
  if (!surveyConfig) {
@@ -120,6 +129,15 @@ export class FeedbackSDK {
120
129
  );
121
130
  }
122
131
 
132
+ if (!this._isSurveyEligible(surveyConfig)) {
133
+ this.eventBus.emit('survey:suppressed', {
134
+ surveyId,
135
+ reason: this._getSurveyIneligibilityReason(surveyConfig),
136
+ survey: surveyConfig,
137
+ });
138
+ return null;
139
+ }
140
+
123
141
  return this.showSurvey({
124
142
  surveyId: surveyConfig.id,
125
143
  surveyType: surveyConfig.type,
@@ -128,7 +146,7 @@ export class FeedbackSDK {
128
146
  lowLabel: surveyConfig.low_label,
129
147
  highLabel: surveyConfig.high_label,
130
148
  customQuestions: surveyConfig.questions,
131
- ...options,
149
+ ...displayOptions,
132
150
  });
133
151
  }
134
152
 
@@ -139,6 +157,15 @@ export class FeedbackSDK {
139
157
  );
140
158
  }
141
159
 
160
+ if (!this._isSurveyEligible(options)) {
161
+ this.eventBus.emit('survey:suppressed', {
162
+ surveyId: options.surveyId || options.id || null,
163
+ reason: this._getSurveyIneligibilityReason(options),
164
+ survey: options,
165
+ });
166
+ return null;
167
+ }
168
+
142
169
  const surveyWidget = this.createWidget('survey', {
143
170
  surveyId: options.surveyId,
144
171
  surveyType: options.surveyType || options.type || 'nps',
@@ -159,6 +186,56 @@ export class FeedbackSDK {
159
186
  return surveyWidget;
160
187
  }
161
188
 
189
+ _isSurveyEligible(survey = {}) {
190
+ const shouldShow = this._getSurveyField(survey, ['shouldShow', 'should_show']);
191
+ if (typeof shouldShow === 'boolean') {
192
+ return shouldShow;
193
+ }
194
+
195
+ const eligible = this._getSurveyField(survey, [
196
+ 'eligible',
197
+ 'isEligible',
198
+ 'is_eligible',
199
+ ]);
200
+ if (typeof eligible === 'boolean') {
201
+ return eligible;
202
+ }
203
+
204
+ const isAnswered = this._getSurveyField(survey, ['isAnswered', 'is_answered']);
205
+ if (typeof isAnswered === 'boolean') {
206
+ return !isAnswered;
207
+ }
208
+
209
+ return true;
210
+ }
211
+
212
+ _getSurveyIneligibilityReason(survey = {}) {
213
+ const explicitReason = this._getSurveyField(survey, [
214
+ 'reason',
215
+ 'suppressionReason',
216
+ 'suppression_reason',
217
+ ]);
218
+ if (explicitReason) {
219
+ return explicitReason;
220
+ }
221
+
222
+ const isAnswered = this._getSurveyField(survey, ['isAnswered', 'is_answered']);
223
+ if (isAnswered === true) {
224
+ return 'already_answered';
225
+ }
226
+
227
+ return 'ineligible';
228
+ }
229
+
230
+ _getSurveyField(survey, fields) {
231
+ for (const field of fields) {
232
+ if (survey[field] !== undefined && survey[field] !== null) {
233
+ return survey[field];
234
+ }
235
+ }
236
+ return null;
237
+ }
238
+
162
239
  showChangelog(options = {}) {
163
240
  if (!this.initialized) {
164
241
  throw new SDKError(