Merge pull request #25 from delapuente/statistics-about-installations

Statistics about number of Add To Home Screen banners displayed and h…
This commit is contained in:
Salvador de la Puente González 2016-03-17 17:57:00 +01:00
Родитель 6fb458537f 8fa6adc715
Коммит ba5530591f
2 изменённых файлов: 56 добавлений и 4 удалений

Просмотреть файл

@ -168,6 +168,24 @@ class WP_Add_To_Homescreen_Admin {
$overlay_count, 'add-to-homescreen'
), number_format_i18n($overlay_count));
echo '</p>';
$prompted_count = $this->options->get('addtohomescreen_stats_prompted');
echo '<p>';
printf(_n(
'<strong>%s</strong> user has been prompted about adding your site as an application.',
'<strong>%s</strong> users have been prompted about adding your site as an application.',
$prompted_count, 'add-to-homescreen'
), number_format_i18n($prompted_count));
echo '</p>';
$installation_count = $this->options->get('addtohomescreen_stats_installed');
echo '<p>';
printf(_n(
'<strong>%s</strong> user has added your site as an application.',
'<strong>%s</strong> users have added your site as an application.',
$installation_count, 'add-to-homescreen'
), number_format_i18n($installation_count));
echo '</p>';
}
}

Просмотреть файл

@ -12,16 +12,37 @@
storage: localforage.createInstance({ name: PRIVATE_NAME }),
stats: {
registerPrompted: function () {
return wpAddToHomescreen.storage.getItem('prompted')
.then(function (isPrompted) {
if (!isPrompted) {
this.sendEvent('prompted');
}
return Promise.resolve();
}.bind(this));
},
registerInstallation: function () {
return wpAddToHomescreen.storage.getItem('installed')
.then(function (isInstalled) {
if (!isInstalled) {
this.sendEvent('installed');
}
return Promise.resolve();
}.bind(this));
},
sendEvent: function (metric, data) {
data = data || {};
data.metric = metric;
var encodedData = (function () {
var form = new FormData();
Object.keys(data).forEach(key => form.append(key, data[key]));
Object.keys(data).forEach(function (key) { form.append(key, data[key]); });
return form;
})();
var statsRequest = new Request(setup.statsEndPoint, { method: 'POST', body: encodedData });
fetch(statsRequest);
var xhr = new XMLHttpRequest();
xhr.open('POST', setup.statsEndPoint, true);
xhr.send(encodedData);
}
},
@ -41,7 +62,7 @@
_registerHowToWasDisplayed: function () {
wpAddToHomescreen.storage.getItem('how-to-was-displayed')
.then(instructionsDisplayed => {
.then(function (instructionsDisplayed) {
if (!instructionsDisplayed) {
wpAddToHomescreen.stats.sendEvent('how-to-was-displayed');
wpAddToHomescreen.storage.setItem('how-to-was-displayed', true);
@ -55,6 +76,7 @@
if (this.isPlatformSupported()) {
this.overlay.element = this.installOverlay(overlayContainer);
this.installAddToHomescreenButton(buttonContainer);
window.addEventListener('beforeinstallprompt', this._onBeforeInstall.bind(this));
}
},
@ -82,6 +104,18 @@
return overlay;
},
_onBeforeInstall: function (event) {
wpAddToHomescreen.stats.registerPrompted()
.then(wpAddToHomescreen.storage.setItem('prompted', true));
event.userChoice.then(function (choice) {
if (choice.outcome === 'accepted') {
wpAddToHomescreen.stats.registerInstallation()
.then(wpAddToHomescreen.storage.setItem('installed', true));
}
});
},
isPlatformSupported: function () {
return isMobile.any && this.detectBrowser();
},