Commit fe9a60d4 authored by Yuli Huang's avatar Yuli Huang Committed by Commit Bot

Use Google Analytics for metrics collection.

1. Implement metrics.js and add google-analytics-bundle.js lib.
2. Use metricsPrivate to check if reporting is enabled.
3. Send 'launch' event for active users metrics.

BUG=b:117816926
NOPRESUBMIT=true
TEST=Tested by launching app and checking GA dashboard.

Change-Id: If3cf413a3dfcb4b3a19421d5b22f8d60f3698f94
Reviewed-on: https://chromium-review.googlesource.com/c/1481140
Commit-Queue: yuli <yuli@chromium.org>
Reviewed-by: default avatarSheng-hao Tsao <shenghao@google.com>
Cr-Commit-Position: refs/heads/master@{#634182}
parent 3a6c6372
......@@ -111,7 +111,9 @@ RESOURCES = \
src/images/spinner.svg \
src/js/background.js \
src/js/gallerybutton.js \
src/js/google-analytics-bundle.js \
src/js/main.js \
src/js/metrics.js \
src/js/models/gallery.js \
src/js/models/file_system.js \
src/js/nav.js \
......
......@@ -133,6 +133,7 @@ cca.App.prototype.start = function() {
// Prompt to migrate pictures if needed.
var message = chrome.i18n.getMessage('migrate_pictures_msg');
return cca.nav.open('dialog', message, false).then((acked) => {
cca.state.set('migrate-prompted', true);
if (!acked) {
throw new Error('no-migrate');
}
......@@ -152,6 +153,8 @@ cca.App.prototype.start = function() {
return;
}
cca.nav.open('warning', 'filesystem-failure');
}).finally(() => {
cca.metrics.log(cca.metrics.Type.LAUNCH);
});
};
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* Namespace for the Camera app.
*/
var cca = cca || {};
/**
* Namespace for metrics.
*/
cca.metrics = cca.metrics || {};
/**
* Event builder for basic metrics.
* @type {analytics.EventBuilder}
* @private
*/
cca.metrics.base_ = null;
/**
* Promise for Google Analytics tracker.
* @type {Promise<analytics.Tracker>}
* @private
*/
cca.metrics.ga_ = (function() {
const analytics = window['analytics'] || {};
const id = 'UA-134822711-2'; // TODO(yuli): Use prod id.
const service = analytics.getService('chrome-camera-app');
var getConfig = () =>
new Promise((resolve) => service.getConfig().addCallback(resolve));
var checkEnabled = () => {
return new Promise((resolve) => {
try {
chrome.metricsPrivate.getIsCrashReportingEnabled(resolve);
} catch (e) {
resolve(false); // Disable reporting by default.
}
});
};
var initBuilder = () => {
return new Promise((resolve) => {
try {
chrome.chromeosInfoPrivate.get(['board'],
(values) => resolve(values['board']));
} catch (e) {
resolve('');
}
}).then((board) => {
var boardName = /^(x86-)?(\w*)/.exec(board)[0];
var match = navigator.appVersion.match(/CrOS\s+\S+\s+([\d.]+)/);
var osVer = match ? match[1] : '';
cca.metrics.base_ = analytics.EventBuilder.builder()
.dimension(1, boardName).dimension(2, osVer);
});
};
return Promise.all([getConfig(), checkEnabled(), initBuilder()])
.then(([config, enabled]) => {
config.setTrackingPermitted(enabled);
return service.getTracker(id);
});
})();
/**
* Tries to fetch the given states and returns the first existing state.
* @param {Array<string>} states States to be fetched.
* @return {string} The first existing state among the given states.
* @private
*/
cca.metrics.state_ = function(states) {
return states.find((state) => cca.state.get(state)) || '';
};
/**
* Returns event builder for the metrics type: launch.
* @return {analytics.EventBuilder}
* @private
*/
cca.metrics.launchType_ = function() {
return cca.metrics.base_.category('launch').action('launch-app')
.label(cca.metrics.state_(['migrate-prompted']));
};
/**
* Metrics types.
* @enum {function(*=)}
*/
cca.metrics.Type = {
LAUNCH: cca.metrics.launchType_,
};
/**
* Logs the given metrics.
* @param {cca.metrics.Type} type Metrics type.
* @param {...*} args Optional rest parameters for logging metrics.
*/
cca.metrics.log = function(type, ...args) {
cca.metrics.ga_.then((tracker) => tracker.send(type(...args)));
};
......@@ -156,24 +156,6 @@ cca.util.orientPhoto = function(blob, onSuccess, onFailure) {
});
};
/**
* Checks if the current device is in the given device list.
* @param {Array<string>} ids Device ids.
* @return {!Promise<boolean>} Promise for the result.
*/
cca.util.isChromeOSDevice = function(ids) {
return new Promise((resolve) => {
if (!chrome.chromeosInfoPrivate) {
resolve(false);
return;
}
chrome.chromeosInfoPrivate.get(['customizationId'], (values) => {
var device = values['customizationId'];
resolve(device && ids.indexOf(device) >= 0);
});
});
};
/**
* Returns true if current installed Chrome version is larger than or equal to
* the given version.
......
......@@ -17,9 +17,11 @@
"unlimitedStorage",
"idle",
"chromeosInfoPrivate",
"metricsPrivate",
"fileManagerPrivate",
"fileSystem.requestDownloads",
{"fileSystem": ["write", "directory"]}
{"fileSystem": ["write", "directory"]},
"https://www.google-analytics.com/"
],
"app": {
"background": {
......
......@@ -9,6 +9,8 @@
<title>&#xfeff;</title>
<meta charset="utf-8">
<link rel="stylesheet" href="../css/main.css">
<script src="../js/google-analytics-bundle.js"></script>
<script src="../js/metrics.js"></script>
<script src="../js/util.js"></script>
<script src="../js/toast.js"></script>
<script src="../js/tooltip.js"></script>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment