Course contentsShow
← Web Development
Lesson 2358 of 2,98242. Progressive Web AppsFree lesson

Prompt User Choice Handling

Handle the user's response to the install prompt and track whether they accepted or dismissed it.

Prompt User Choice Handling

You'll learn how to track and respond to whether a user accepts or dismisses your custom PWA install prompt.

When you call prompt() on the beforeinstallprompt event (as you learned in the previous lesson), the browser shows the install dialog. But what happens next? Did the user tap "Install" or "Cancel"? Your app needs to know so you can hide your custom button, update analytics, or adjust your messaging strategy.

Reading the User's Decision

The prompt() method returns a Promise that resolves to an object containing the user's choice. This object has an outcome property with one of two string values:

  • "accepted" – the user agreed to install
  • "dismissed" – the user declined

Here's how you capture and handle it:

let deferredPrompt;

window.addEventListener('beforeinstallprompt', (e) => {
  e.preventDefault();
  deferredPrompt = e;
  document.getElementById('installBtn').style.display = 'block';
});

document.getElementById('installBtn').addEventListener('click', async () => {
  if (!deferredPrompt) return;
  
  deferredPrompt.prompt();
  
  const choiceResult = await deferredPrompt.userChoice;
  
  if (choiceResult.outcome === 'accepted') {
    console.log('User installed the PWA');
  } else {
    console.log('User dismissed the install prompt');
  }
  
  // Clean up - prompt can only be used once
  deferredPrompt = null;
  document.getElementById('installBtn').style.display = 'none';
});

Think of it like asking someone on a date: you ask the question, then you wait for their answer before deciding your next move. The userChoice promise is that answer.

Key Takeaway: Always await deferredPrompt.userChoice after calling prompt() to capture whether the user accepted or dismissed the install, allowing you to update your UI and track installation funnel metrics accordingly.