@product7/product7-js 0.4.8 → 0.5.0

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/product7-js",
3
- "version": "0.4.8",
3
+ "version": "0.5.0",
4
4
  "description": "JavaScript SDK for integrating Product7 feedback widgets into any website",
5
5
  "main": "dist/product7-js.js",
6
6
  "module": "src/index.js",
@@ -81,9 +81,8 @@ export class SurveyService {
81
81
  const contact = this.api.getContactIdentity?.() || null;
82
82
 
83
83
  const payload = {
84
- rating: responseData.rating,
85
- feedback: responseData.feedback || '',
86
- answers: responseData.answers || {},
84
+ answers: Array.isArray(responseData.answers) ? responseData.answers : [],
85
+ ...(responseData.feedback && { feedback: responseData.feedback }),
87
86
  ...(respondent.respondent_id && {
88
87
  respondent_id: respondent.respondent_id,
89
88
  }),
@@ -912,28 +912,18 @@ export class SurveyWidget extends BaseWidget {
912
912
  this._setSubmitLoading(true);
913
913
 
914
914
  const respondent = this._getRespondentContext();
915
- const normalizedPageAnswers = this._normalizePageAnswersForSubmit();
916
- const mergedAnswers = {
917
- ...this.surveyState.customAnswers,
918
- ...(Object.keys(normalizedPageAnswers).length > 0 && {
919
- page_answers: normalizedPageAnswers,
920
- }),
921
- };
922
915
 
923
916
  const responseData = {
924
- rating: this._getSubmissionRating(),
925
- feedback: this.surveyState.feedback,
926
- answers: mergedAnswers,
917
+ answers: this._normalizePageAnswersForSubmit(),
918
+ ...(this.surveyState.feedback && { feedback: this.surveyState.feedback }),
927
919
  ...(respondent.respondentId && { respondentId: respondent.respondentId }),
928
920
  ...(respondent.email && { email: respondent.email }),
929
921
  };
930
922
 
931
923
  const response = {
932
924
  type: type,
933
- score: this._getSubmissionRating(),
925
+ answers: responseData.answers,
934
926
  feedback: this.surveyState.feedback,
935
- customAnswers: mergedAnswers,
936
- pageAnswers: normalizedPageAnswers,
937
927
  timestamp: new Date().toISOString(),
938
928
  };
939
929
 
@@ -943,11 +933,11 @@ export class SurveyWidget extends BaseWidget {
943
933
  await this.apiService.submitSurveyResponse(surveyId, responseData);
944
934
 
945
935
  this.sdk.eventBus.emit('survey:submitted', { widget: this, response });
946
- this._showThankYouScreen();
947
-
948
- if (this.surveyOptions.onSubmit) {
949
- this.surveyOptions.onSubmit(response);
950
- }
936
+ this._showThankYouScreen(() => {
937
+ if (this.surveyOptions.onSubmit) {
938
+ this.surveyOptions.onSubmit(response);
939
+ }
940
+ });
951
941
  } catch (error) {
952
942
  console.error('[SurveyWidget] Failed to submit survey:', error);
953
943
  this._showError('Something went wrong. Please try again.');
@@ -1117,27 +1107,58 @@ export class SurveyWidget extends BaseWidget {
1117
1107
  }
1118
1108
 
1119
1109
  _normalizePageAnswersForSubmit() {
1120
- const output = {};
1121
- for (const [pageId, answer] of Object.entries(
1122
- this.surveyState.pageAnswers
1123
- )) {
1124
- if (answer == null) continue;
1125
- if (Array.isArray(answer.values) && answer.values.length > 0) {
1126
- output[pageId] = answer.values;
1127
- continue;
1128
- }
1129
- if (answer.value != null && answer.value !== '') {
1130
- output[pageId] = answer.value;
1131
- continue;
1110
+ // Single-rating surveys (NPS / CSAT / CES / star / emoji) have no pages
1111
+ if (!this._isMultiPageSurvey()) {
1112
+ if (typeof this.surveyState.score === 'number') {
1113
+ return [{ page_id: 'p1', value: this.surveyState.score }];
1132
1114
  }
1133
- if (typeof answer.rating === 'number') {
1134
- output[pageId] = answer.rating;
1135
- continue;
1115
+ return [];
1116
+ }
1117
+
1118
+ const output = [];
1119
+
1120
+ for (const page of this.surveyOptions.pages) {
1121
+ const pageId =
1122
+ page.id || `page_${this.surveyOptions.pages.indexOf(page)}`;
1123
+ const answer = this.surveyState.pageAnswers[pageId];
1124
+ if (answer == null) continue;
1125
+
1126
+ let value = null;
1127
+
1128
+ if (page.type === 'rating' && typeof answer.rating === 'number') {
1129
+ value = answer.rating;
1130
+ } else if (page.type === 'multiple_choice') {
1131
+ const config =
1132
+ page.multipleChoiceConfig || page.multiple_choice_config || {};
1133
+ const allowMultiple =
1134
+ config.allow_multiple === true ||
1135
+ config.multiple === true ||
1136
+ config.allow_multiple_selection === true;
1137
+
1138
+ if (
1139
+ allowMultiple &&
1140
+ Array.isArray(answer.values) &&
1141
+ answer.values.length > 0
1142
+ ) {
1143
+ value = answer.values;
1144
+ } else if (answer.value != null && answer.value !== '') {
1145
+ value = answer.value;
1146
+ }
1147
+ } else if (
1148
+ page.type === 'text' &&
1149
+ typeof answer.text === 'string' &&
1150
+ answer.text.trim()
1151
+ ) {
1152
+ value = answer.text.trim();
1153
+ } else if (page.type === 'link' && typeof answer.clicked === 'boolean') {
1154
+ value = answer.clicked;
1136
1155
  }
1137
- if (typeof answer.text === 'string' && answer.text.trim()) {
1138
- output[pageId] = answer.text.trim();
1156
+
1157
+ if (value !== null) {
1158
+ output.push({ page_id: pageId, value });
1139
1159
  }
1140
1160
  }
1161
+
1141
1162
  return output;
1142
1163
  }
1143
1164
 
@@ -1197,7 +1218,7 @@ export class SurveyWidget extends BaseWidget {
1197
1218
  setTimeout(() => error.remove(), 3000);
1198
1219
  }
1199
1220
 
1200
- _showThankYouScreen() {
1221
+ _showThankYouScreen(onAfterClose) {
1201
1222
  if (!this.surveyElement) return;
1202
1223
 
1203
1224
  const config = this.surveyOptions.thankYouConfig || {};
@@ -1233,6 +1254,7 @@ export class SurveyWidget extends BaseWidget {
1233
1254
  setTimeout(
1234
1255
  () => {
1235
1256
  if (this.surveyElement) this._closeSurvey();
1257
+ if (onAfterClose) onAfterClose();
1236
1258
  },
1237
1259
  buttonText ? 6000 : 3500
1238
1260
  );