Skip to content

Commit

Permalink
Merge pull request #779 from alphagov/survey-test
Browse files Browse the repository at this point in the history
Create new survey test - decrease frequency that survey shows for 48 hours
  • Loading branch information
dsingleton committed Apr 29, 2016
2 parents 6cb89ac + be98fd6 commit 2d5bb5b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 73 deletions.
28 changes: 11 additions & 17 deletions app/assets/javascripts/user-satisfaction-survey.js
Expand Up @@ -14,14 +14,6 @@
' </div>' +
'</section>',

TEST_TEMPLATE: '<section id="user-satisfaction-survey" class="visible" aria-hidden="false">' +
' <div class="wrapper">' +
' <h1>Are you visiting GOV.UK for professional or personal reasons?</h1>' +
' <p class="right"><a href="#survey-no-thanks" id="survey-no-thanks">No thanks</a></p>' +
' <p><a href="javascript:void()" id="take-survey" target="_blank">Take the 1 question survey</a> This will open a short survey on another website</p>' +
' </div>' +
'</section>',

cookieNameTakenSurvey: "govuk_takenUserSatisfactionSurvey",
trackEvent: function (action, label) {
GOVUK.analytics.trackEvent('user_satisfaction_survey', action, {
Expand Down Expand Up @@ -58,8 +50,7 @@
// Hide global bar if one is showing
$('#global-bar').hide();

var template = userSatisfaction.inTestPeriod() ? userSatisfaction.TEST_TEMPLATE : userSatisfaction.TEMPLATE;
$("#user-satisfaction-survey-container").append(template);
$("#user-satisfaction-survey-container").append(userSatisfaction.TEMPLATE);

userSatisfaction.setEventHandlers();
userSatisfaction.setSurveyUrl();
Expand All @@ -72,27 +63,30 @@
if ($('#user-satisfaction-survey-container').length <= 0) {
return;
}
if (Math.floor(Math.random() * 50) === 0) {
if (Math.floor(Math.random() * userSatisfaction.surveyFrequency()) === 0) {
userSatisfaction.showSurveyBar();
}
},
surveyFrequency: function() {
if (userSatisfaction.inTestPeriod()) {
return 100;
}

return 50;
},
setSurveyUrl: function(href) {
var $surveyLink = $('#take-survey');
var surveyUrl = $('#user-satisfaction-survey-container').data('survey-url');

if (userSatisfaction.inTestPeriod()) {
surveyUrl = 'https://www.surveymonkey.co.uk/r/D668G5Z';
}

if (surveyUrl.indexOf('?c=') === -1) {
surveyUrl += "?c=" + root.location.pathname;
}

$surveyLink.attr('href', surveyUrl);
},
inTestPeriod: function() {
var starts = new Date("March 2, 2016").getTime();
var ends = new Date("March 3, 2016 23:59:59").getTime();
var starts = new Date("May 4, 2016").getTime();
var ends = new Date("May 5, 2016 23:59:59").getTime();

return userSatisfaction.currentDate() >= starts &&
userSatisfaction.currentDate() <= ends;
Expand Down
77 changes: 21 additions & 56 deletions spec/javascripts/user-satisfaction-survey-spec.js
Expand Up @@ -37,42 +37,6 @@ describe("User Satisfaction Survey", function () {
expect(survey.currentDate()).not.toBe(undefined);
});

it("uses the temporary survey URL in the test period", function() {
spyOn(GOVUK.userSatisfaction, 'inTestPeriod').and.returnValue(true);
survey.showSurveyBar();
expect($('#take-survey').attr('href')).toMatch("https://www.surveymonkey.co.uk/r/D668G5Z?");
});

it("uses the original survey URL outside the test period", function() {
spyOn(GOVUK.userSatisfaction, 'inTestPeriod').and.returnValue(false);
survey.showSurveyBar();
expect($('#take-survey').attr('href')).toMatch("https://www.surveymonkey.com/r/some-survey-id?");
});

it("uses the temporary survey heading in the test period", function() {
spyOn(GOVUK.userSatisfaction, 'inTestPeriod').and.returnValue(true);
survey.showSurveyBar();
expect($('#user-satisfaction-survey h1').text()).toBe("Are you visiting GOV.UK for professional or personal reasons?");
});

it("uses the original survey heading outside the test period", function() {
spyOn(GOVUK.userSatisfaction, 'inTestPeriod').and.returnValue(false);
survey.showSurveyBar();
expect($('#user-satisfaction-survey h1').text()).toBe("Tell us what you think of GOV.UK");
});

it("uses the temporary survey link in the test period", function() {
spyOn(GOVUK.userSatisfaction, 'inTestPeriod').and.returnValue(true);
survey.showSurveyBar();
expect($('#user-satisfaction-survey a#take-survey').text()).toBe("Take the 1 question survey");
});

it("uses the original survey link outside the test period", function() {
spyOn(GOVUK.userSatisfaction, 'inTestPeriod').and.returnValue(false);
survey.showSurveyBar();
expect($('#user-satisfaction-survey a#take-survey').text()).toBe("Take the 3 minute survey");
});

it("should set the take survey link's href to the survey monkey's url as defined by the wrapper's data-survey-url, appending the page's current path when not already specified", function() {
spyOn(GOVUK.userSatisfaction, 'inTestPeriod').and.returnValue(false);
$("#user-satisfaction-survey-container").data('survey-url', 'https://www.surveymonkey.com/r/some-survey-id');
Expand All @@ -87,20 +51,21 @@ describe("User Satisfaction Survey", function () {
expect($('#take-survey').attr('href')).toBe("https://www.surveymonkey.com/r/some-survey-id?c=/somewhere");
});

it("should randomly display the user satisfaction div", function () {
pending(); //Fails randomly, disabling.
var counter = 0;
for (var i = 0; i < 100; i++) {
$('#user-satisfaction-survey').remove();
survey.randomlyShowSurveyBar();
it("should show the survey approximately 1 in every 50 pageviews", function () {
spyOn(GOVUK.userSatisfaction, 'inTestPeriod').and.returnValue(false);
expect(survey.surveyFrequency()).toBe(50);
});

if ($('#user-satisfaction-survey').length > 0) {
counter += 1;
}
}
it("should decrease the frequency to 1 in every 100 pageviews during test period", function () {
spyOn(GOVUK.userSatisfaction, 'inTestPeriod').and.returnValue(true);
expect(survey.surveyFrequency()).toBe(100);
});

expect(counter).toBeGreaterThan(0);
expect(counter).toBeLessThan(5);
it("should display the survey when the random number matches", function () {
expect($('#user-satisfaction-survey').length).toBe(0);
spyOn(GOVUK.userSatisfaction, 'surveyFrequency').and.returnValue(1);
survey.randomlyShowSurveyBar();
expect($('#user-satisfaction-survey').length).toBe(1);
});

it("should not display the user satisfaction div if another notification banner is visible", function() {
Expand Down Expand Up @@ -154,23 +119,23 @@ describe("User Satisfaction Survey", function () {
});

describe("inTestPeriod", function () {
it("should be false on 1st March 2016", function() {
spyOn(survey, 'currentDate').and.returnValue(new Date("March 1, 2016 23:50:00").getTime());
it("should be false on 3 May 2016", function() {
spyOn(survey, 'currentDate').and.returnValue(new Date("May 3, 2016 23:50:00").getTime());
expect(survey.inTestPeriod()).toBe(false);
});

it("should be true on 2nd March 2016", function() {
spyOn(survey, 'currentDate').and.returnValue(new Date("March 2, 2016 00:01:00").getTime());
it("should be true on 4 May 2016", function() {
spyOn(survey, 'currentDate').and.returnValue(new Date("May 4, 2016 00:01:00").getTime());
expect(survey.inTestPeriod()).toBe(true);
});

it("should be true on 2nd March 2016", function() {
spyOn(survey, 'currentDate').and.returnValue(new Date("March 3, 2016 23:50:00").getTime());
it("should be true on 5 May 2016", function() {
spyOn(survey, 'currentDate').and.returnValue(new Date("May 5, 2016 23:50:00").getTime());
expect(survey.inTestPeriod()).toBe(true);
});

it("should be false on 4th March 2016", function() {
spyOn(survey, 'currentDate').and.returnValue(new Date("March 4, 2016 00:01:00").getTime());
it("should be false on 6 May 2016", function() {
spyOn(survey, 'currentDate').and.returnValue(new Date("May 6, 2016 00:01:00").getTime());
expect(survey.inTestPeriod()).toBe(false);
});
});
Expand Down

0 comments on commit 2d5bb5b

Please sign in to comment.