Progressive Web Application Development by Example
上QQ阅读APP看书,第一时间看更新

Tracking homescreen installs

Once the homescreen install prompt displays, the user can choose to add the PWA to their homescreen, or ignore the prompt. Businesses should track and measure everything possible to make better decisions. Knowing how many homescreen installs there have been and what rate customers install their PWA provides insight into their marketing and technology investments.

Chrome supports the beforeinstallprompt event, which can be used to track this activity. You can add a handler to this event and log each user's choice:

window.addEventListener('beforeinstallprompt', function(event) { 
  event.userChoice.then(function(result) {                                  
 
if(result.outcome == 'dismissed') {                                         
  // They dismissed, send to analytics 
}else { 
  // User accepted! Send to analytics 
} 
});  
}); 

You can POST the user's choice to your analytics system. This could be a custom API to your internal analytics or even tied to your third-party service, like Google Analytics.

The beforeinstallprompt is part of the web manifest specification, but at the time of writing this book, it is only supported by Chrome. Hopefully, other browsers will add support soon.

Browsers that don't support beforeinstallprompt can also provide feedback. The web manifest's start_url can be set either to a special start URL, or a custom querystring value appended to the default URL. You will need to add logic to your log analyzer to track this behavior. Besides just knowing how many homescreen installs you have, you can also track how many times users have launched your PWA as opposed to those who have not installed your PWA.