Skip to content

Commit

Permalink
chore: use SM direct embed code (electron#5217)
Browse files Browse the repository at this point in the history
  • Loading branch information
molant committed Mar 11, 2021
1 parent f41c07f commit 747b1ef
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 308 deletions.
210 changes: 0 additions & 210 deletions lib/surveymonkey.js

This file was deleted.

101 changes: 5 additions & 96 deletions middleware/feedback.js
Original file line number Diff line number Diff line change
@@ -1,104 +1,13 @@
//@ts-check

/**
* Middleware that checks if the feedback CTA needs to be displayed or not
* and adds the right properties to the context to be consumed by partials.
*/

const surveyMonkey = require('../lib/surveymonkey')

/** How long the user will not be bothered with the feedback CTA (90 days) */
const cookieExpiration = 90 * 24 * 3600 * 1000
const cookieName = 'feedbackCTA'
const cookieValue = 'true'

/** Refresh status every 15 minutes */
const freshnessTimeout = 15 * 60 * 1000

let surveyLastRefreshed = -1
let cachedSurvey

const getLastSurveyInformation = async () => {
const isFresh = Date.now() - surveyLastRefreshed < freshnessTimeout

if (!cachedSurvey || !isFresh) {
cachedSurvey = await surveyMonkey.getLatestSurvey()
surveyLastRefreshed = Date.now()
}

return cachedSurvey
}

/**
* Checks if the current survey is opened
* @param {import('../lib/surveymonkey').Survey} survey
* @returns {Boolean}
*/
const isSurveyOpen = (survey) => {
// Even though the doc say it's only "open" or "close", it can also be "new" 😒
return survey.status !== 'close'
}

/**
* Checks if the user has been shown the CTA previously
* @param {any} cookies
* @returns {Boolean}
*/
const userIsNotTainted = (cookies) => {
return !(cookies[cookieName] && cookies[cookieName] === cookieValue)
}

/**
* Randomly selects 8% of the users
* @returns {Boolean}
*/
const shouldShowCTAToUser = () => {
return Math.random() <= 0.08
}

/**
* Returns a JSON object telling if the CTA needs to be shown or not,
* and updating the cookie status so the client knows to now show it.
*
* Even though SM has a way to define the % of visitors that will see
* the pop up for a survey, it is only limited to that particular one.
* We want to make sure users are not bothered for 3 months and if we
* have a new one every 2 weeks that is not possible.
* @param {import("express").Request} req
* @param {import("express").Response} res
*/
module.exports = async (req, res, next) => {
const lastSurvey = await getLastSurveyInformation()
const criteria = [
isSurveyOpen(lastSurvey),
userIsNotTainted(req.cookies),
shouldShowCTAToUser(),
]

const validatesCriteria = criteria.every((check) => check)
const feedbackEmbedCode = process.env.feedbackEmbedCode
const debugging = req.query.debug === 'true'

if (validatesCriteria || debugging) {
// The context object is manually passed to HBS when rendering
// The associated partial (/views/partials/feedback.hbs) will
// use these variables to decide what and when to render
const context = {
showFeedback: true,
feedbackEmbedCode: lastSurvey.embedCode,
// Bypass the popup limitation of showing only once per session: https://stackoverflow.com/a/55789538
// The cookie will clear only if debugging is done in the first page where the cookie was set
debugging,
}

req.context = Object.assign(req.context, context)

// Set only the do-not-bother-me-for-a-while cookie if we are not debugging
if (!debugging) {
res.cookie(cookieName, cookieValue, {
maxAge: cookieExpiration,
})
}
}
req.context = Object.assign(req.context, {
feedbackEmbedCode,
debugging,
})

next()
}
4 changes: 2 additions & 2 deletions views/partials/feedback.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{#if showFeedback}}
{{#if feedbackEmbedCode}}
{{#if debugging}}
<script>document.cookie = 'smcx_0_last_shown_at=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';</script>
{{/if}}
Expand All @@ -23,5 +23,5 @@ body > div.smcx-widget.smcx-modal > div.smcx-modal-content {
height: 200px;
}
</style>
{{{feedbackEmbedCode}}}
<script>(function(t,e,s,o){var n,a,c;t.SMCX=t.SMCX||[],e.getElementById(o)||(n=e.getElementsByTagName(s),a=n[n.length-1],c=e.createElement(s),c.type="text/javascript",c.async=!0,c.id=o,c.src="https://widget.surveymonkey.com/collect/website/js/{{{feedbackEmbedCode}}}.js",a.parentNode.insertBefore(c,a))})(window,document,"script","smcx-sdk");</script>
{{/if}}

0 comments on commit 747b1ef

Please sign in to comment.