Skip to content

Commit

Permalink
Merge pull request #225 from GoogleChrome/sendErrorsToAnalytics2
Browse files Browse the repository at this point in the history
Send the exceptions to Analytics
  • Loading branch information
beaufortfrancois committed Apr 1, 2014
2 parents ae677ca + 3202b4e commit 378c592
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
43 changes: 29 additions & 14 deletions js/analytics.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
/**
* @constructor
* @param {boolean} enabled
*/
function Analytics() {
this.service_ = null;
}
function Analytics(enabled) {
this.hasReportedSettings_ = false;
this.setEnabled(enabled);

Analytics.prototype.start = function(settings) {
if (this.service_) {
throw 'Analytics should be started only once per session.';
}

this.service_ = analytics.getService('text_app');
var service = analytics.getService('text_app');
var propertyId = 'UA-48886257-2';
if (chrome.runtime.id === 'mmfbcljfglbokpmkimbfghdkjmjhdgbg') {
propertyId = 'UA-48886257-1';
}
this.tracker_ = this.service_.getTracker(propertyId);

this.reportSettings_(settings);
this.tracker_ = service.getTracker(propertyId);

this.tracker_.sendAppView('main');
$(document).bind('settingschange', this.onSettingsChange_.bind(this));
};

Analytics.prototype.reportSettings_ = function(settings) {
Analytics.prototype.reportSettings = function(settings) {
if (!this.enabled_ || this.hasReportedSettings_)
return; // Settings should be reported only once per session.

this.tracker_.set('dimension1', settings.get('spacestab').toString());
this.tracker_.set('dimension2', settings.get('tabsize').toString());
this.tracker_.set('dimension3',
Math.round(settings.get('fontsize')).toString());
this.tracker_.set('dimension4', settings.get('sidebaropen').toString());
this.tracker_.set('dimension5', settings.get('theme'));

this.tracker_.sendAppView('main', function() {
this.hasReportedSettings_ = true;
}.bind(this));
};

Analytics.prototype.reportError = function(message, error) {
if (this.enabled_)
this.tracker_.sendEvent('error', message, error);
};

Analytics.prototype.setEnabled = function(enabled) {
this.enabled_ = enabled;
};

Analytics.prototype.onSettingsChange_ = function(e, key, value) {
if (key === 'analytics')
this.setEnabled(value);
};
8 changes: 4 additions & 4 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ TextApp.prototype.init = function() {
this.menuController_ = new MenuController(this.tabs_);
this.searchController_ = new SearchController(this.editor_.getSearch());
this.settingsController_ = new SettingsController(this.settings_);
this.windowController_ = new WindowController(this.editor_, this.settings_);
this.windowController_ = new WindowController(
this.editor_, this.settings_, this.analytics_);
this.hotkeysController_ = new HotkeysController(
this.windowController_, this.tabs_, this.editor_, this.settings_);

Expand Down Expand Up @@ -102,9 +103,8 @@ TextApp.prototype.onSettingsReady_ = function() {
this.editor_.replaceTabWithSpaces(this.settings_.get('spacestab'));
this.editor_.setTabSize(this.settings_.get('tabsize'));
this.editor_.setWrapLines(this.settings_.get('wraplines'));
if (this.settings_.get('analytics')) {
this.analytics_.start(this.settings_);
}
this.analytics_.setEnabled(this.settings_.get('analytics'));
this.analytics_.reportSettings(this.settings_);
};

/**
Expand Down
10 changes: 9 additions & 1 deletion js/controllers/window.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/**
* @constructor
*/
function WindowController(editor, settings) {
function WindowController(editor, settings, analytics) {
this.editor_ = editor;
this.settings_ = settings;
this.analytics_ = analytics;
this.currentTab_ = null;
$('#window-close').click(this.close_.bind(this));
$('#window-maximize').click(this.maximize_.bind(this));
$('#toggle-sidebar').click(this.toggleSidebar_.bind(this));
$('#sidebar-resizer').mousedown(this.resizeStart_.bind(this));
$(window).bind('error', this.onError_.bind(this));
$(document).bind('filesystemerror', this.onFileSystemError.bind(this));
$(document).bind('loadingfile', this.onLoadingFile.bind(this));
$(document).bind('switchtab', this.onChangeTab_.bind(this));
Expand Down Expand Up @@ -136,3 +138,9 @@ WindowController.prototype.resizeFinish_ = function(e) {
$(document).css('cursor', 'default');
$('#sidebar').css('-webkit-transition', 'width 0.2s ease-in-out');
};

WindowController.prototype.onError_ = function(event) {
var message = event.originalEvent.message;
var errorStack = event.originalEvent.error.stack;
this.analytics_.reportError(message, errorStack);
};

0 comments on commit 378c592

Please sign in to comment.