Skip to content

Commit

Permalink
added 'Completed Experiment' survey
Browse files Browse the repository at this point in the history
  • Loading branch information
dannycoates committed Sep 21, 2016
1 parent 0b7a905 commit 74b26e6
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions addon/lib/survey.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ if (store.surveyChecks === undefined) store.surveyChecks = {};
});

function init() {
if (checkForCompletedExperiments()) { return; }
// wait about 10 minutes before prompting user for survey
setTimeout(() => {
// Only check/ask for survey if the user has addons installed.
Expand All @@ -40,6 +41,38 @@ function init() {
}, TEN_MINUTES);
}

function checkForCompletedExperiments() {
if (store.installedAddons) {
const now = Date.now();
const ids = Object.keys(store.installedAddons);
const eolSurveys = [];
for (let id of ids) { // eslint-disable-line prefer-const
let x = store.installedAddons[id]; // eslint-disable-line prefer-const
if (x.active &&
(new Date(x.completed)).getTime() < now &&
!(id in store.surveyChecks.eol)) {
eolSurveys.push(x);
}
}
const experiment = eolSurveys[Math.floor(Math.random() * eolSurveys.length)];
if (experiment) {
tabs.once('open', tab => { // eslint-disable-line no-unused-vars
setTimeout(() =>
launchSurvey({
experiment,
interval: 'eol',
label: `The ${experiment.title} experiment has ended. What did you think?`,
persistence: 10
}),
1000
);
});
return true;
}
}
return false;
}

function destroy() {
delete store.surveyChecks;
}
Expand Down Expand Up @@ -75,13 +108,14 @@ function checkInstallDate(installDate, addonId) {
return false;
}

function launchSurvey(experiment, interval) {
showRating({ experiment })
function launchSurvey(options) {
const { experiment, interval } = options;
showRating(options)
.then(
rating => {
if (!rating) { return Promise.resolve(); }
// TODO: add telemetry ping for rating
return showSurveyButton({ experiment })
return showSurveyButton(options)
.then(clicked => {
if (clicked) {
const urlParams = {
Expand Down Expand Up @@ -180,12 +214,15 @@ function showRating(options) {
let experimentRating = null;

const { notifyBox, box } = createNotificationBox({
label: `Please rate ${experiment.title}`,
label: options.label || `Please rate ${experiment.title}`,
image: experiment.thumbnail,
child: win => createRatingUI(win, uiClosed),
persistence: options.persistence,
pulse: true,
callback: () => {
if (options.interval === 'eol' && experimentRating === null) {
store.surveyChecks.eol[experiment.addon_id] = true;
}
clearTimeout(uiTimeout);
resolve(experimentRating);
}
Expand Down

0 comments on commit 74b26e6

Please sign in to comment.