Stackedit/public/res/extensions/googleAnalytics.js

233 lines
6.3 KiB
JavaScript
Raw Normal View History

2013-11-07 23:10:38 +00:00
/*globals _gaq */
2013-06-10 21:22:32 +00:00
define([
"jquery",
"underscore",
2013-11-05 23:03:38 +00:00
"constants",
2013-06-16 18:29:54 +00:00
"utils",
2013-06-22 23:48:57 +00:00
"classes/Extension",
2013-06-10 21:22:32 +00:00
"settings",
2013-11-05 23:03:38 +00:00
], function($, _, constants, utils, Extension, settings) {
2013-06-10 21:22:32 +00:00
2013-06-22 23:48:57 +00:00
var googleAnalytics = new Extension("googleAnalytics", "Google Analytics", true);
googleAnalytics.settingsBlock = '<p>Sends anonymous statistics about usage and errors to help improve StackEdit.</p>';
2013-06-10 21:22:32 +00:00
var isLoaded = false;
var isOffline = false;
2013-11-07 23:10:38 +00:00
window._gaq = [];
2013-06-10 21:22:32 +00:00
var init = function() {
if(isLoaded === false && isOffline === false) {
var gaUrl = "/ga.js";
if(location.search.match(/(\?|&)console/)) {
gaUrl = "/u/ga_debug.js";
}
$.ajax({
2013-10-10 00:04:37 +00:00
url: "https://ssl.google-analytics.com" + gaUrl,
2013-06-10 21:22:32 +00:00
dataType: "script"
}).done(function() {
isLoaded = true;
});
}
};
2013-06-16 18:29:54 +00:00
var lastPageView = 0;
function trackPageView() {
if(utils.currentTime - lastPageView > 180000) {
_gaq.push([
'_trackPageview'
]);
lastPageView = utils.currentTime;
}
}
googleAnalytics.onPeriodicRun = function() {
trackPageView();
};
2013-06-12 00:00:12 +00:00
googleAnalytics.onReady = function() {
// First configure GA
_gaq.push([
'_setAccount',
2013-11-05 23:03:38 +00:00
constants.GOOGLE_ANALYTICS_ACCOUNT_ID
2013-06-12 00:00:12 +00:00
]);
2013-06-16 18:29:54 +00:00
trackPageView();
2013-06-12 00:00:12 +00:00
// Track StackEdit version
_gaq.push([
'_trackEvent',
"About",
'version',
2013-11-05 23:03:38 +00:00
constants.VERSION
]);
2013-06-12 00:00:12 +00:00
// Collect informations about user settings
_gaq.push([
'_trackEvent',
"Settings",
'layoutOrientation',
"" + settings.layoutOrientation
]);
2013-10-13 22:46:14 +00:00
_gaq.push([
'_trackEvent',
"Settings",
'theme',
2013-11-07 23:10:38 +00:00
"" + window.theme
2013-10-13 22:46:14 +00:00
]);
2013-06-12 00:00:12 +00:00
_gaq.push([
'_trackEvent',
"Settings",
'lazyRendering',
"" + settings.lazyRendering
]);
_gaq.push([
'_trackEvent',
"Settings",
2013-06-27 23:10:04 +00:00
'editorFontFamily',
"" + settings.editorFontFamily
2013-06-12 00:00:12 +00:00
]);
2013-06-27 23:10:04 +00:00
_gaq.push([
2013-11-07 23:10:38 +00:00
'_trackEvent',
"Settings",
'editorFontSize',
"" + settings.editorFontSize
]);
2013-06-12 00:00:12 +00:00
// Check if user has removed back links
_gaq.push([
'_trackEvent',
"Settings",
'defaultContent backlink',
2013-11-05 23:03:38 +00:00
"" + (settings.defaultContent.indexOf(constants.MAIN_URL) !== -1)
2013-06-12 00:00:12 +00:00
]);
_gaq.push([
'_trackEvent',
"Settings",
'commitMsg backlink',
2013-11-05 23:03:38 +00:00
"" + (settings.commitMsg.indexOf(constants.MAIN_URL) !== -1)
2013-06-12 00:00:12 +00:00
]);
// Check if user has changed sshProxy
_gaq.push([
'_trackEvent',
"Settings",
'sshProxy unchanged',
2013-11-05 23:03:38 +00:00
"" + (settings.sshProxy == constants.SSH_PROXY_URL)
2013-06-12 00:00:12 +00:00
]);
// Check if extensions have been disabled
_.each(settings.extensionSettings, function(config, extensionId) {
_gaq.push([
'_trackEvent',
"Extensions",
extensionId + " enabled",
"" + (config.enabled === true)
]);
});
// Catch window JavaScript errors
window.onerror = function(message, url, line) {
_gaq.push([
"_trackEvent",
2013-06-12 20:37:43 +00:00
"Error",
2013-06-16 18:29:54 +00:00
message,
url + ":" + line + utils.formatEventList()
2013-06-12 00:00:12 +00:00
]);
};
init();
};
2013-06-10 21:22:32 +00:00
googleAnalytics.onOfflineChanged = function(isOfflineParam) {
isOffline = isOfflineParam;
init();
};
2013-06-10 22:32:34 +00:00
var startTime = 0;
googleAnalytics.onSyncRunning = function(isRunning) {
if(isRunning === true) {
startTime = new Date().getTime();
}
2013-06-10 21:22:32 +00:00
};
2013-06-10 22:32:34 +00:00
googleAnalytics.onPublishRunning = function(isRunning) {
if(isRunning === true) {
startTime = new Date().getTime();
}
2013-06-10 21:22:32 +00:00
};
2013-06-10 22:32:34 +00:00
// Log sync time
2013-06-10 21:22:32 +00:00
googleAnalytics.onSyncSuccess = function() {
2013-06-10 22:32:34 +00:00
var endTime = new Date().getTime();
2013-06-10 21:22:32 +00:00
_gaq.push([
2013-06-10 22:32:34 +00:00
'_trackTiming',
2013-06-10 21:22:32 +00:00
'Sync',
2013-06-10 22:32:34 +00:00
'SyncTime',
endTime - startTime
2013-06-10 21:22:32 +00:00
]);
};
// Log import frequency and provider
googleAnalytics.onSyncImportSuccess = function(fileDescList, provider) {
_gaq.push([
'_trackEvent',
'Sync',
'SyncImport',
]);
_gaq.push([
'_trackEvent',
'Sync',
2013-06-10 22:32:34 +00:00
'SyncImport provider',
2013-06-10 21:22:32 +00:00
provider.providerId
]);
};
// Log export frequency and provider
googleAnalytics.onSyncExportSuccess = function(fileDesc, syncAttributes) {
_gaq.push([
'_trackEvent',
'Sync',
'SyncExport',
]);
_gaq.push([
'_trackEvent',
'Sync',
2013-06-10 22:32:34 +00:00
'SyncExport provider',
2013-06-10 21:22:32 +00:00
syncAttributes.provider.providerId
]);
};
2013-06-10 22:32:34 +00:00
// Log publish time and provider
2013-06-10 21:22:32 +00:00
googleAnalytics.onPublishSuccess = function(fileDesc) {
2013-06-10 22:32:34 +00:00
var endTime = new Date().getTime();
2013-06-10 21:22:32 +00:00
_gaq.push([
2013-06-10 22:32:34 +00:00
'_trackTiming',
2013-06-10 21:22:32 +00:00
'Publish',
2013-06-10 22:32:34 +00:00
'PublishSuccess',
endTime - startTime
2013-06-10 21:22:32 +00:00
]);
_.each(fileDesc.publishLocations, function(publishAttributes) {
_gaq.push([
'_trackEvent',
'Publish',
2013-06-10 22:32:34 +00:00
'PublishSuccess provider',
2013-06-10 21:22:32 +00:00
publishAttributes.provider.providerId
]);
});
};
// Log new publication's provider
googleAnalytics.onNewPublishSuccess = function(fileDesc, publishAttributes) {
_gaq.push([
'_trackEvent',
'Publish',
2013-06-10 22:32:34 +00:00
'NewPublish provider',
2013-06-10 21:22:32 +00:00
publishAttributes.provider.providerId
]);
};
// Log error messages
googleAnalytics.onError = function(error) {
if(_.isString(error) || !error.message) {
return;
}
_gaq.push([
'_trackEvent',
"Error",
"message",
2013-06-16 18:29:54 +00:00
error.message + utils.formatEventList()
]);
};
2013-06-10 21:22:32 +00:00
return googleAnalytics;
});