Skip to content

Instantly share code, notes, and snippets.

@rickychilcott
Created August 25, 2021 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickychilcott/0128fcb439e546be74c7a6848ba29a03 to your computer and use it in GitHub Desktop.
Save rickychilcott/0128fcb439e546be74c7a6848ba29a03 to your computer and use it in GitHub Desktop.
Trigger a newsletter signup popup every 30 days
<link rel="stylesheet" href="https://s.electerious.com/basicLightbox/dist/basicLightbox.min.css" />
<script src="https://s.electerious.com/basicLightbox/dist/basicLightbox.min.js"></script>
<script type="text/javascript">
const storage = window.localStorage
const NEWSLETTER_KEY = "newsletter-signup-trigger"
const DAYS_IN_MS = 86_400_000
const RESHOW_DAYS = 30 * DAYS_IN_MS
const DELAY_POP_SECONDS = 20 * 1_000
const lightboxInstance = basicLightbox.create('<iframe src="https://cdn.forms-content.sg-form.com/d89f3c03-a796-11eb-ad16-0a2b9780b90d" style="border-width: 0" height="600px" width="600px"></iframe>')
const openLightbox = () => {
lightboxInstance.show()
plausible('Blog Popup: Show')
storage.setItem(NEWSLETTER_KEY, Date.now())
}
const shouldTrigger = () => {
const value = storage.getItem(NEWSLETTER_KEY)
if (!value) return true
if (!parseInt(value)) return true
const parsedValue = parseInt(value)
if (Date.now() > (parsedValue + RESHOW_DAYS)) return true
return false
}
const checkIfTriggered = (func) => (shouldTrigger()) ? func() : null
window.resetNewsletterLightbox = () => storage.removeItem(NEWSLETTER_KEY)
document.addEventListener("DOMContentLoaded", () => {
setTimeout(
() => { checkIfTriggered(openLightbox) }
, DELAY_POP_SECONDS)
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment