From f7dd25c4eb020d227f0fe1f655daefad08903ff7 Mon Sep 17 00:00:00 2001 From: kenobi Date: Tue, 10 Mar 2015 10:02:51 -0700 Subject: Update the chrome platform analytics package to 1.6.0. BUG=461946 Review URL: https://codereview.chromium.org/986393002 Cr-Commit-Position: refs/heads/master@{#319905} --- .../file_manager/background/js/background.js | 5 --- ui/file_manager/file_manager/common/js/metrics.js | 41 ++++++++++++++-------- .../file_manager/common/js/metrics_unittest.js | 39 ++++++++++++++++++-- 3 files changed, 63 insertions(+), 22 deletions(-) (limited to 'ui') diff --git a/ui/file_manager/file_manager/background/js/background.js b/ui/file_manager/file_manager/background/js/background.js index b415ad0..13531be 100644 --- a/ui/file_manager/file_manager/background/js/background.js +++ b/ui/file_manager/file_manager/background/js/background.js @@ -627,11 +627,6 @@ FileBrowserBackground.prototype.initContextMenu_ = function() { }); }; -// Ensure that the user gets a new analytics ID each time Files.app starts. -// NOTE: This must be called before FileBrowserBackground is instantiated. -// TODO(kenobi): Revisit this once users have the ability to do this themselves. -metrics.clearUserId(); - /** * Singleton instance of Background. * NOTE: This must come after the call to metrics.clearUserId. diff --git a/ui/file_manager/file_manager/common/js/metrics.js b/ui/file_manager/file_manager/common/js/metrics.js index a96d827..fa84d7e5 100644 --- a/ui/file_manager/file_manager/common/js/metrics.js +++ b/ui/file_manager/file_manager/common/js/metrics.js @@ -28,6 +28,9 @@ metrics.convertName_ = function(name) { return 'FileBrowser.' + name; }; +/** @private {analytics.GoogleAnalytics} */ +metrics.analytics_ = null; + /** @private {analytics.Tracker} */ metrics.tracker_ = null; @@ -47,39 +50,49 @@ metrics.getTracker = function() { * @private */ metrics.createTracker_ = function() { - var analyticsService = analytics.getService('Files app'); + metrics.analytics_ = analytics.getService('Files app'); // Create a tracker, add a filter that only enables analytics when UMA is // enabled. - metrics.tracker_ = analyticsService.getTracker(metrics.TRACKING_ID); + metrics.tracker_ = metrics.analytics_.getTracker(metrics.TRACKING_ID); metrics.tracker_.addFilter(metrics.umaEnabledFilter_); }; /** * Queries the chrome UMA enabled setting, and filters hits based on that. * @param {!analytics.Tracker.Hit} hit + * @return {!goog.async.Deferred} A deferred indicating when the filter has + * completed running. * @private */ metrics.umaEnabledFilter_ = function(hit) { + // TODO(kenobi): Change this to use Promises when analytics supports it. + var deferred = new goog.async.Deferred(); + chrome.fileManagerPrivate.isUMAEnabled( - /** @param {boolean} enabled */ function(enabled) { + if (!enabled) { + // If UMA was just toggled, reset the analytics ID. + if (metrics.enabled_) { + metrics.clearUserId_(); + } + hit.cancel(); + } metrics.enabled_ = enabled; + deferred.callback(enabled); }); - if (!metrics.enabled_) { - hit.cancel(); - } + + return deferred; }; /** * Clears the previously set analytics user id. + * @return {!Promise} Resolves when the analytics ID has been reset. */ -metrics.clearUserId = function() { - chrome.storage.local.remove( - 'google-analytics.analytics.user-id', - function () { - if (chrome.runtime.lastError) { - throw new Error(chrome.runtime.lastError.message); - } - }); +metrics.clearUserId_ = function() { + return metrics.analytics_.getConfig().then( + /** @param {!analytics.Config} config */ + function(config) { + config.resetUserId(); + }); }; diff --git a/ui/file_manager/file_manager/common/js/metrics_unittest.js b/ui/file_manager/file_manager/common/js/metrics_unittest.js index 36a9807..47a0b61 100644 --- a/ui/file_manager/file_manager/common/js/metrics_unittest.js +++ b/ui/file_manager/file_manager/common/js/metrics_unittest.js @@ -14,16 +14,16 @@ var tracker; function setUp() { setupFakeChromeAPIs(); + // Set a fake tracking ID so the tests aren't actually sending analytics. + metrics.TRACKING_ID = 'UA-XXXXX-XX'; tracker = metrics.getTracker(); - // Make a filter that logs the last received hit. Cancel the hit so that - // running tests doesn't actually send any analytics. + // Make a filter that logs the last received hit. tracker.addFilter( /** @param {!analytics.Tracker.Hit} hit */ function(hit) { // Log the hit. lastHit = hit; - hit.cancel(); }); // Reset the last logged hit. lastHit = null; @@ -55,6 +55,39 @@ function testUMADisabled(callback) { callback); } +// Verifies that toggling the UMA setting causes the user's analytics ID to be +// reset. +function testResetAnalyticsOnUMAToggle(callback) { + var id0 = null; + var id1 = null; + + // Simulate UMA enabled, and send a hit. + chrome.fileManagerPrivate.umaEnabled = true; + reportPromise( + tracker.sendAppView('Test0') + .then( + function() { + // Log the analytics ID that got sent. + id0 = lastHit.getParameters().toObject()['clientId']; + // Simulate UMA disabled, send another hit. + chrome.fileManagerPrivate.umaEnabled = false; + return tracker.sendAppView('Test1'); + }) + .then( + function() { + // Re-enable analytics, send another hit. + chrome.fileManagerPrivate.umaEnabled = true; + return tracker.sendAppView('Test2'); + }) + .then( + function() { + // Check that a subsequent hit has a different analytics ID. + id1 = lastHit.getParameters().toObject()['clientId']; + assertTrue(id0 !== id1); + }), + callback); +} + function setupFakeChromeAPIs() { chrome = { runtime: { -- cgit v1.1