From 41bfe31d614039d51343ede6aa01170e371b4b70 Mon Sep 17 00:00:00 2001 From: Danny Coates Date: Tue, 19 Feb 2019 10:46:16 -0800 Subject: [PATCH] prefix /api/metrics on android. fixes #1159 --- app/api.js | 11 +++++++++++ app/metrics.js | 33 ++++++++++++++------------------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/app/api.js b/app/api.js index 09f21f41..98bc7bdb 100644 --- a/app/api.js +++ b/app/api.js @@ -390,3 +390,14 @@ export async function setFileList(bearerToken, data) { }); return response.ok; } + +export function sendMetrics(blob) { + if (!navigator.sendBeacon) { + return; + } + try { + navigator.sendBeacon(getApiUrl('/api/metrics'), blob); + } catch (e) { + console.error(e); + } +} diff --git a/app/metrics.js b/app/metrics.js index 0dcabe10..d22d007a 100644 --- a/app/metrics.js +++ b/app/metrics.js @@ -1,5 +1,6 @@ import storage from './storage'; import { platform } from './utils'; +import { sendMetrics } from './api'; let appState = null; // let experiment = null; @@ -31,27 +32,21 @@ function submitEvents() { if (navigator.doNotTrack === '1') { return; } - const data = new Blob( - [ - JSON.stringify({ - now: Date.now(), - session_id, - lang, - platform: platform(), - events - }) - ], - { type: 'text/plain' } // see http://crbug.com/490015 + sendMetrics( + new Blob( + [ + JSON.stringify({ + now: Date.now(), + session_id, + lang, + platform: platform(), + events + }) + ], + { type: 'text/plain' } // see http://crbug.com/490015 + ) ); events.splice(0); - if (!navigator.sendBeacon) { - return; - } - try { - navigator.sendBeacon('/api/metrics', data); - } catch (e) { - console.error(e); - } } async function addEvent(event_type, event_properties) {