Last active
July 25, 2019 10:46
-
-
Save OhkuboSGMS/40b0b7e1d023c7d96aef098a432a549f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Installer = function(root) { | |
let promptEvent; | |
const install = function(e) { | |
if(promptEvent) { | |
promptEvent.prompt(); | |
promptEvent.userChoice | |
.then(function(choiceResult) { | |
// The user actioned the prompt (good or bad). | |
// good is handled in | |
promptEvent = null; | |
ga('send', 'event', 'install', choiceResult); | |
root.classList.remove('available'); | |
}) | |
.catch(function(installError) { | |
// Boo. update the UI. | |
promptEvent = null; | |
ga('send', 'event', 'install', 'errored'); | |
root.classList.remove('available'); | |
}); | |
} | |
}; | |
const installed = function(e) { | |
promptEvent = null; | |
// This fires after onbeforinstallprompt OR after manual add to homescreen. | |
ga('send', 'event', 'install', 'installed'); | |
root.classList.remove('available'); | |
}; | |
const beforeinstallprompt = function(e) { | |
promptEvent = e; | |
promptEvent.preventDefault(); | |
ga('send', 'event', 'install', 'available'); | |
root.classList.add('available'); | |
return false; | |
}; | |
window.addEventListener('beforeinstallprompt', beforeinstallprompt); | |
window.addEventListener('appinstalled', installed); | |
root.addEventListener('click', install.bind(this)); | |
root.addEventListener('touchend', install.bind(this)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment