I have create a service worker file and manifeast file and register service worker file in my index.html file. I want to track that how many users can see "Add to home screen button" and how many users have click "Add to home screen" . MY application has meet all the criteria of pwa. I have used "beforeinstallprompt" event in service file but its not fire when "Add to home screen" shows.
My service worker code are given below.
var doCache = true;
var version = 481;
// Name our cache
var CACHE_NAME = 'health-cache-v='+version;
var filesToCache = [
'index.html'
];
let deferredPrompt;
self.addEventListener('beforeinstallprompt', (event) => {
onsole.log('add to home screen popup show');
event.userChoice.then(choiceResult => {
if (choiceResult.outcome === 'accepted') {
console.log('User accepted the add to home screen popup');
} else {
console.log('User dismissed the add to home screen popup');
}
});
});
// Delete old caches that are not our current one!
self.addEventListener("activate", event => {
const cacheWhitelist = [CACHE_NAME];
event.waitUntil(
caches.keys()
.then(keyList =>
Promise.all(keyList.map(key => {
if (!cacheWhitelist.includes(key)) {
console.log('Deleting cache: ' + key)
return caches.delete(key);
}
}))
)
);
});
self.addEventListener('install', function(event) {
if (doCache) {
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
cache.addAll(filesToCache);
console.log('cached');
})
);
}
});
self.addEventListener('fetch', function(event) {
if (doCache) {
event.respondWith(
caches.match(event.request).then(function(response) {
return response || fetch(event.request);
})
);
}
});